diff --git a/example/src/App.js b/example/src/App.js index d36d01d..ed432e0 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -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'] ); diff --git a/src/api.ts b/src/api.ts index 66b32fb..af3f990 100644 --- a/src/api.ts +++ b/src/api.ts @@ -134,6 +134,11 @@ export interface CommonEventProperties { context?: Array | null; } +export interface WebViewEvent { + properties: AtomicProperties; + event?: SelfDescribingEvent; +} + /** Interface for communicating with the Android mobile tracker */ export type SnowplowWebInterface = { trackSelfDescribingEvent: ( diff --git a/src/index.ts b/src/index.ts index 2f011b9..a7fa18e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,7 @@ import { ScreenView, SelfDescribingJson, ReactNativeInterface, - AtomicProperties, + WebViewEvent, } from './api'; function withAndroidInterface(callback: (_: SnowplowWebInterface) => void) { @@ -70,7 +70,7 @@ function serializeContext(context?: Array | null) { } } -function serializeSelfDescribingEvent(event?: SelfDescribingEvent | null) { +function serializeSelfDescribingEvent(event?: SelfDescribingEvent) { if (event) { return JSON.stringify(event); } else { @@ -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 | null, - trackers?: Array | null + event: WebViewEvent & CommonEventProperties, + trackers?: Array ) { - 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 ); }); @@ -126,7 +124,7 @@ export function trackWebViewEvent( atomicProperties: stringifiedAtomicProperties, selfDescribingEventData: stringifiedEvent, entities: stringifiedEntities, - trackers: trackers, + trackers: trackers || null, }; }; @@ -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, }; }; diff --git a/test/androidV2.test.ts b/test/androidV2.test.ts index f2c469e..82bb318 100644 --- a/test/androidV2.test.ts +++ b/test/androidV2.test.ts @@ -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), @@ -61,7 +61,7 @@ describe('Android interface', () => { }, }; - trackWebViewEvent(atomic, event, null, null); + trackWebViewEvent({ properties: atomic, event: event }); expect(trackWebViewStub).toHaveBeenCalledWith( JSON.stringify(atomic), @@ -79,7 +79,7 @@ describe('Android interface', () => { }, }; - trackWebViewEvent({}, null, [entity], null); + trackWebViewEvent({ properties: {}, context: [entity] }); expect(trackWebViewStub).toHaveBeenCalledWith( '{}', diff --git a/test/iosV2.test.ts b/test/iosV2.test.ts index 891f381..867bcdd 100644 --- a/test/iosV2.test.ts +++ b/test/iosV2.test.ts @@ -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), @@ -65,7 +65,7 @@ describe('iOS interface', () => { }, }; - trackWebViewEvent(atomic, event, null, null); + trackWebViewEvent({ properties: atomic, event: event }); expect(messageHandler).toHaveBeenCalledWith({ atomicProperties: JSON.stringify(atomic), @@ -83,7 +83,7 @@ describe('iOS interface', () => { }, }; - trackWebViewEvent({}, null, [entity], null); + trackWebViewEvent({ properties: {}, context: [entity] }); expect(messageHandler).toHaveBeenCalledWith({ atomicProperties: '{}', diff --git a/test/reactNative.test.ts b/test/reactNative.test.ts index e279108..227ae2c 100644 --- a/test/reactNative.test.ts +++ b/test/reactNative.test.ts @@ -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', @@ -61,7 +60,6 @@ describe('React Native interface', () => { minYOffset: 40, maxYOffset: 50, }, - context: null, trackers: ['ns1', 'ns2'], }) ); @@ -81,7 +79,7 @@ describe('React Native interface', () => { }, }; - trackWebViewEvent(atomic, event, null, null); + trackWebViewEvent({ properties: atomic, event: event }); expect(messageHandler).toHaveBeenCalledWith( JSON.stringify({ @@ -91,8 +89,6 @@ describe('React Native interface', () => { eventName: 'ue', trackerVersion: 'webview', }, - context: null, - trackers: null, }) ); }); @@ -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, }) ); });