Skip to content

Commit

Permalink
Update method signature to match the others
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Jan 2, 2025
1 parent a02ad43 commit 89e79c0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
33 changes: 18 additions & 15 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ import { v4 as uuidv4 } from 'uuid';
function trackEvents() {
trackWebViewEvent(
{
eventName: 'ue',
trackerVersion: 'webview',
useragent: 'useragent',
},
{
schema:
'iglu:com.snowplowanalytics.snowplow/button_click/jsonschema/1-0-0',
data: {
label: 'webview test',
properties: {
eventName: 'ue',
trackerVersion: 'webview',
useragent: 'useragent',
},
},
[
{
schema: 'iglu:com.apple.swiftui/open_immersive_space/jsonschema/1-0-0',
data: {},
event: {
schema:
'iglu:com.snowplowanalytics.snowplow/button_click/jsonschema/1-0-0',
data: {
label: 'webview test',
},
},
],
context: [
{
schema:
'iglu:com.apple.swiftui/open_immersive_space/jsonschema/1-0-0',
data: {},
},
],
},
['sp1']
);

Expand Down
5 changes: 5 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ export interface CommonEventProperties {
context?: Array<SelfDescribingJson> | null;
}

export interface WebViewEvent {
properties: AtomicProperties;
event?: SelfDescribingEvent;
}

/** Interface for communicating with the Android mobile tracker */
export type SnowplowWebInterface = {
trackSelfDescribingEvent: (
Expand Down
26 changes: 12 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ScreenView,
SelfDescribingJson,
ReactNativeInterface,
AtomicProperties,
WebViewEvent,
} from './api';

function withAndroidInterface(callback: (_: SnowplowWebInterface) => void) {
Expand Down Expand Up @@ -70,7 +70,7 @@ function serializeContext(context?: Array<SelfDescribingJson> | null) {
}
}

function serializeSelfDescribingEvent(event?: SelfDescribingEvent | null) {
function serializeSelfDescribingEvent(event?: SelfDescribingEvent) {
if (event) {
return JSON.stringify(event);
} else {
Expand Down Expand Up @@ -103,21 +103,19 @@ export function hasMobileInterface(): boolean {
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackWebViewEvent(
atomicProperties: AtomicProperties,
event?: SelfDescribingEvent | null,
entities?: Array<SelfDescribingJson> | null,
trackers?: Array<string> | null
event: WebViewEvent & CommonEventProperties,
trackers?: Array<string>
) {
const stringifiedAtomicProperties = JSON.stringify(atomicProperties);
const stringifiedEvent = serializeSelfDescribingEvent(event);
const stringifiedEntities = serializeContext(entities);
const stringifiedAtomicProperties = JSON.stringify(event.properties);
const stringifiedEvent = serializeSelfDescribingEvent(event.event);
const stringifiedEntities = serializeContext(event.context);

withAndroidInterfaceV2((webInterface) => {
webInterface.trackWebViewEvent(
stringifiedAtomicProperties,
stringifiedEvent,
stringifiedEntities,
trackers
trackers || null
);
});

Expand All @@ -126,7 +124,7 @@ export function trackWebViewEvent(
atomicProperties: stringifiedAtomicProperties,
selfDescribingEventData: stringifiedEvent,
entities: stringifiedEntities,
trackers: trackers,
trackers: trackers || null,
};
};

Expand All @@ -138,10 +136,10 @@ export function trackWebViewEvent(
return {
command: 'trackWebViewEvent',
event: {
selfDescribingEventData: event,
...atomicProperties,
selfDescribingEventData: event.event,
...event.properties,
},
context: entities,
context: event.context,
trackers: trackers,
};
};
Expand Down
6 changes: 3 additions & 3 deletions test/androidV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Android interface', () => {
action: 'act',
};

trackWebViewEvent(atomic, null, null, ['ns1', 'ns2']);
trackWebViewEvent({ properties: atomic }, ['ns1', 'ns2']);

expect(trackWebViewStub).toHaveBeenCalledWith(
JSON.stringify(atomic),
Expand All @@ -61,7 +61,7 @@ describe('Android interface', () => {
},
};

trackWebViewEvent(atomic, event, null, null);
trackWebViewEvent({ properties: atomic, event: event });

expect(trackWebViewStub).toHaveBeenCalledWith(
JSON.stringify(atomic),
Expand All @@ -79,7 +79,7 @@ describe('Android interface', () => {
},
};

trackWebViewEvent({}, null, [entity], null);
trackWebViewEvent({ properties: {}, context: [entity] });

expect(trackWebViewStub).toHaveBeenCalledWith(
'{}',
Expand Down
6 changes: 3 additions & 3 deletions test/iosV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('iOS interface', () => {
title: 'test title',
};

trackWebViewEvent(atomic, null, null, ['ns1', 'ns2']);
trackWebViewEvent({ properties: atomic }, ['ns1', 'ns2']);

expect(messageHandler).toHaveBeenCalledWith({
atomicProperties: JSON.stringify(atomic),
Expand All @@ -65,7 +65,7 @@ describe('iOS interface', () => {
},
};

trackWebViewEvent(atomic, event, null, null);
trackWebViewEvent({ properties: atomic, event: event });

expect(messageHandler).toHaveBeenCalledWith({
atomicProperties: JSON.stringify(atomic),
Expand All @@ -83,7 +83,7 @@ describe('iOS interface', () => {
},
};

trackWebViewEvent({}, null, [entity], null);
trackWebViewEvent({ properties: {}, context: [entity] });

expect(messageHandler).toHaveBeenCalledWith({
atomicProperties: '{}',
Expand Down
16 changes: 5 additions & 11 deletions test/reactNative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ describe('React Native interface', () => {
maxYOffset: 50,
};

trackWebViewEvent(atomic, null, null, ['ns1', 'ns2']);
trackWebViewEvent({ properties: atomic }, ['ns1', 'ns2']);

expect(messageHandler).toHaveBeenCalledWith(
JSON.stringify({
command: 'trackWebViewEvent',
event: {
selfDescribingEventData: null,
eventName: 'pp',
trackerVersion: 'webview',
url: 'http://test.com',
Expand All @@ -61,7 +60,6 @@ describe('React Native interface', () => {
minYOffset: 40,
maxYOffset: 50,
},
context: null,
trackers: ['ns1', 'ns2'],
})
);
Expand All @@ -81,7 +79,7 @@ describe('React Native interface', () => {
},
};

trackWebViewEvent(atomic, event, null, null);
trackWebViewEvent({ properties: atomic, event: event });

expect(messageHandler).toHaveBeenCalledWith(
JSON.stringify({
Expand All @@ -91,8 +89,6 @@ describe('React Native interface', () => {
eventName: 'ue',
trackerVersion: 'webview',
},
context: null,
trackers: null,
})
);
});
Expand All @@ -105,16 +101,14 @@ describe('React Native interface', () => {
},
};

trackWebViewEvent({}, null, [entity], null);
trackWebViewEvent({ properties: {}, context: [entity] });

expect(messageHandler).toHaveBeenCalledWith(
JSON.stringify({
command: 'trackWebViewEvent',
event: {
selfDescribingEventData: null,
},
event: {},
context: [entity],
trackers: null,
trackers: undefined,
})
);
});
Expand Down

0 comments on commit 89e79c0

Please sign in to comment.