Skip to content

Commit

Permalink
Add WebView tracker integration to the React Native tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Dec 10, 2024
1 parent d6a87a0 commit 15af8b6
Show file tree
Hide file tree
Showing 20 changed files with 741 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@snowplow/react-native-tracker](./react-native-tracker.md) &gt; [getAllTrackers](./react-native-tracker.getalltrackers.md)

## getAllTrackers() function

Retrieves all initialized trackers

<b>Signature:</b>

```typescript
export declare function getAllTrackers(): ReactNativeTracker[];
```
<b>Returns:</b>

[ReactNativeTracker](./react-native-tracker.reactnativetracker.md)<!-- -->\[\]

All initialized trackers

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@snowplow/react-native-tracker](./react-native-tracker.md) &gt; [getWebViewCallback](./react-native-tracker.getwebviewcallback.md)

## getWebViewCallback() function

Enables tracking events from apps rendered in react-native-webview components. The apps need to use the Snowplow WebView tracker to track the events.

To subscribe for the events, set the `onMessage` attribute: &lt;<!-- -->WebView onMessage=<!-- -->{<!-- -->getWebViewCallback()<!-- -->} ... /<!-- -->&gt;

<b>Signature:</b>

```typescript
export declare function getWebViewCallback(): (message: {
nativeEvent: {
data: string;
};
}) => void;
```
<b>Returns:</b>

(message: { nativeEvent: { data: string; }; }) =&gt; void

Callback to subscribe for events from Web views tracked using the Snowplow WebView tracker.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

| Function | Description |
| --- | --- |
| [getAllTrackers()](./react-native-tracker.getalltrackers.md) | Retrieves all initialized trackers |
| [getTracker(trackerNamespace)](./react-native-tracker.gettracker.md) | Retrieves an initialized tracker given its namespace |
| [getWebViewCallback()](./react-native-tracker.getwebviewcallback.md) | Enables tracking events from apps rendered in react-native-webview components. The apps need to use the Snowplow WebView tracker to track the events.<!-- -->To subscribe for the events, set the <code>onMessage</code> attribute: &lt;<!-- -->WebView onMessage=<!-- -->{<!-- -->getWebViewCallback()<!-- -->} ... /<!-- -->&gt; |
| [newTracker(configuration)](./react-native-tracker.newtracker.md) | Creates a new tracker instance with the given configuration |
| [removeAllTrackers()](./react-native-tracker.removealltrackers.md) | Removes all initialized trackers |
| [removeTracker(trackerNamespace)](./react-native-tracker.removetracker.md) | Removes a tracker given its namespace |
Expand Down Expand Up @@ -46,7 +48,6 @@
| [RuleSet](./react-native-tracker.ruleset.md) | A ruleset has accept or reject properties that contain rules for matching Iglu schema URIs |
| [SessionConfiguration](./react-native-tracker.sessionconfiguration.md) | Configuration for session tracking |
| [SessionState](./react-native-tracker.sessionstate.md) | Current session state that is tracked in events. |
| [StructuredEvent](./react-native-tracker.structuredevent.md) | A Structured Event A classic style of event tracking, allows for easier movement between analytics systems. A loosely typed event, creating a Self Describing event is preferred, but useful for interoperability. |
| [SubjectConfiguration](./react-native-tracker.subjectconfiguration.md) | Configuration of subject properties tracked with events |
| [TrackerConfiguration](./react-native-tracker.trackerconfiguration.md) | The configuration object for initialising the tracker |
| [TrackerCore](./react-native-tracker.trackercore.md) | Export interface containing all Core functions |
Expand Down Expand Up @@ -84,7 +85,8 @@
| [ScreenSize](./react-native-tracker.screensize.md) | Screen size in pixels |
| [ScreenViewProps](./react-native-tracker.screenviewprops.md) | ScreenView event properties schema: iglu:com.snowplowanalytics.mobile/screen\_view/jsonschema/1-0-0 |
| [ScrollChangedProps](./react-native-tracker.scrollchangedprops.md) | Event tracked when a scroll view's scroll position changes. If screen engagement tracking is enabled, the scroll changed events will be aggregated into a <code>screen_summary</code> entity.<!-- -->Schema: <code>iglu:com.snowplowanalytics.mobile/scroll_changed/jsonschema/1-0-0</code> |
| [SelfDescribingJson](./react-native-tracker.selfdescribingjson.md) | Export interface for any Self-Describing JSON such as context or Self Describing events |
| [SelfDescribing](./react-native-tracker.selfdescribing.md) | Interface for any self-describing JSON such as context entities or self-describing events |
| [StructuredProps](./react-native-tracker.structuredprops.md) | A Structured Event A classic style of event tracking, allows for easier movement between analytics systems. A loosely typed event, creating a Self Describing event is preferred, but useful for interoperability. |
| [Timestamp](./react-native-tracker.timestamp.md) | Algebraic datatype representing possible timestamp type choice |
| [TimingProps](./react-native-tracker.timingprops.md) | Timing event properties |
| [Trigger](./react-native-tracker.trigger.md) | Trigger for MessageNotification event |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ The ReactNativeTracker type
```typescript
export declare type ReactNativeTracker = {
namespace: string;
readonly trackSelfDescribingEvent: <T extends Record<string, unknown> = Record<string, unknown>>(argmap: SelfDescribingJson<T>, contexts?: EventContext[]) => void;
readonly trackSelfDescribingEvent: <T extends Record<string, unknown> = Record<string, unknown>>(argmap: SelfDescribing<T>, contexts?: EventContext[]) => void;
readonly trackScreenViewEvent: (argmap: ScreenViewProps, contexts?: EventContext[]) => void;
readonly trackScrollChangedEvent: (argmap: ScrollChangedProps, contexts?: EventContext[]) => void;
readonly trackListItemViewEvent: (argmap: ListItemViewProps, contexts?: EventContext[]) => void;
readonly trackStructuredEvent: (argmap: StructuredEvent, contexts?: EventContext[]) => void;
readonly trackStructuredEvent: (argmap: StructuredProps, contexts?: EventContext[]) => void;
readonly trackPageViewEvent: (argmap: PageViewEvent, contexts?: EventContext[]) => void;
readonly trackTimingEvent: (argmap: TimingProps, contexts?: EventContext[]) => void;
readonly trackDeepLinkReceivedEvent: (argmap: DeepLinkReceivedProps, contexts?: EventContext[]) => void;
Expand Down Expand Up @@ -47,5 +47,5 @@ export declare type ReactNativeTracker = {
readonly refreshPlatformContext: () => Promise<void>;
};
```
<b>References:</b> [EventContext](./react-native-tracker.eventcontext.md)<!-- -->, [ScreenViewProps](./react-native-tracker.screenviewprops.md)<!-- -->, [ScrollChangedProps](./react-native-tracker.scrollchangedprops.md)<!-- -->, [ListItemViewProps](./react-native-tracker.listitemviewprops.md)<!-- -->, [TimingProps](./react-native-tracker.timingprops.md)<!-- -->, [DeepLinkReceivedProps](./react-native-tracker.deeplinkreceivedprops.md)<!-- -->, [MessageNotificationProps](./react-native-tracker.messagenotificationprops.md)<!-- -->, [ScreenSize](./react-native-tracker.screensize.md)<!-- -->, [SubjectConfiguration](./react-native-tracker.subjectconfiguration.md)<!-- -->, [SessionState](./react-native-tracker.sessionstate.md)
<b>References:</b> [SelfDescribing](./react-native-tracker.selfdescribing.md)<!-- -->, [EventContext](./react-native-tracker.eventcontext.md)<!-- -->, [ScreenViewProps](./react-native-tracker.screenviewprops.md)<!-- -->, [ScrollChangedProps](./react-native-tracker.scrollchangedprops.md)<!-- -->, [ListItemViewProps](./react-native-tracker.listitemviewprops.md)<!-- -->, [StructuredProps](./react-native-tracker.structuredprops.md)<!-- -->, [TimingProps](./react-native-tracker.timingprops.md)<!-- -->, [DeepLinkReceivedProps](./react-native-tracker.deeplinkreceivedprops.md)<!-- -->, [MessageNotificationProps](./react-native-tracker.messagenotificationprops.md)<!-- -->, [ScreenSize](./react-native-tracker.screensize.md)<!-- -->, [SubjectConfiguration](./react-native-tracker.subjectconfiguration.md)<!-- -->, [SessionState](./react-native-tracker.sessionstate.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@snowplow/react-native-tracker](./react-native-tracker.md) &gt; [SelfDescribing](./react-native-tracker.selfdescribing.md)

## SelfDescribing type

Interface for any self-describing JSON such as context entities or self-describing events

<b>Signature:</b>

```typescript
export declare type SelfDescribing<T = Record<string, unknown>> = SelfDescribingJson<T>;
```

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@snowplow/react-native-tracker](./react-native-tracker.md) &gt; [StructuredProps](./react-native-tracker.structuredprops.md)

## StructuredProps type

A Structured Event A classic style of event tracking, allows for easier movement between analytics systems. A loosely typed event, creating a Self Describing event is preferred, but useful for interoperability.

<b>Signature:</b>

```typescript
export declare type StructuredProps = StructuredEvent;
```
36 changes: 18 additions & 18 deletions api-docs/docs/react-native-tracker/react-native-tracker.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type ConditionalContextProvider = FilterProvider | RuleSetProvider;
// @public
export type ContextFilter = (args?: ContextEvent) => boolean;

// Warning: (ae-forgotten-export) The symbol "SelfDescribingJson" needs to be exported by the entry point index.d.ts
//
// @public
export type ContextGenerator = (args?: ContextEvent) => SelfDescribingJson | SelfDescribingJson[] | undefined;

Expand Down Expand Up @@ -189,9 +191,19 @@ export interface FormFocusOrChangeEvent {
value: string | null;
}

// @public
export function getAllTrackers(): ReactNativeTracker[];

// @public
export function getTracker(trackerNamespace: string): ReactNativeTracker | undefined;

// @public
export function getWebViewCallback(): (message: {
nativeEvent: {
data: string;
};
}) => void;

// @public
export type JsonProcessor = (payloadBuilder: PayloadBuilder, jsonForProcessing: EventJson, contextEntitiesForProcessing: SelfDescribingJson[]) => void;

Expand Down Expand Up @@ -346,11 +358,11 @@ export interface PlatformContextRetriever {
// @public
export type ReactNativeTracker = {
namespace: string;
readonly trackSelfDescribingEvent: <T extends Record<string, unknown> = Record<string, unknown>>(argmap: SelfDescribingJson<T>, contexts?: EventContext[]) => void;
readonly trackSelfDescribingEvent: <T extends Record<string, unknown> = Record<string, unknown>>(argmap: SelfDescribing<T>, contexts?: EventContext[]) => void;
readonly trackScreenViewEvent: (argmap: ScreenViewProps, contexts?: EventContext[]) => void;
readonly trackScrollChangedEvent: (argmap: ScrollChangedProps, contexts?: EventContext[]) => void;
readonly trackListItemViewEvent: (argmap: ListItemViewProps, contexts?: EventContext[]) => void;
readonly trackStructuredEvent: (argmap: StructuredEvent, contexts?: EventContext[]) => void;
readonly trackStructuredEvent: (argmap: StructuredProps, contexts?: EventContext[]) => void;
readonly trackPageViewEvent: (argmap: PageViewEvent, contexts?: EventContext[]) => void;
readonly trackTimingEvent: (argmap: TimingProps, contexts?: EventContext[]) => void;
readonly trackDeepLinkReceivedEvent: (argmap: DeepLinkReceivedProps, contexts?: EventContext[]) => void;
Expand Down Expand Up @@ -435,10 +447,7 @@ export type ScrollChangedProps = {
};

// @public
export type SelfDescribingJson<T = Record<string, unknown>> = {
schema: string;
data: T extends any[] ? never : T extends {} ? T : never;
};
export type SelfDescribing<T = Record<string, unknown>> = SelfDescribingJson<T>;

// @public
export interface SessionConfiguration {
Expand All @@ -458,19 +467,10 @@ export interface SessionState {
userId: string;
}

// Warning: (ae-forgotten-export) The symbol "StructuredEvent" needs to be exported by the entry point index.d.ts
//
// @public
export interface StructuredEvent {
// (undocumented)
action: string;
// (undocumented)
category: string;
// (undocumented)
label?: string;
// (undocumented)
property?: string;
// (undocumented)
value?: number;
}
export type StructuredProps = StructuredEvent;

// @public
export interface SubjectConfiguration {
Expand Down
5 changes: 2 additions & 3 deletions trackers/react-native-tracker/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import {
buildStructEvent,
PageViewEvent,
SelfDescribingJson,
StructuredEvent,
TrackerCore,
} from '@snowplow/tracker-core';
import { EventContext, MessageNotificationProps, TimingProps } from './types';
import { EventContext, MessageNotificationProps, StructuredProps, TimingProps } from './types';

export function newTrackEventFunctions(core: TrackerCore) {
const trackSelfDescribingEvent = <T extends Record<string, unknown> = Record<string, unknown>>(
Expand All @@ -17,7 +16,7 @@ export function newTrackEventFunctions(core: TrackerCore) {
core.track(buildSelfDescribingEvent({ event: argmap }), contexts);
};

const trackStructuredEvent = (argmap: StructuredEvent, contexts?: EventContext[]) => {
const trackStructuredEvent = (argmap: StructuredProps, contexts?: EventContext[]) => {
return core.track(buildStructEvent(argmap), contexts)?.eid;
};

Expand Down
3 changes: 2 additions & 1 deletion trackers/react-native-tracker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
import 'react-native-get-random-values';

export * from './types';
export * from './tracker';
export { newTracker, getTracker, getAllTrackers, removeTracker, removeAllTrackers } from './tracker';
export * from './web_view_interface';
25 changes: 25 additions & 0 deletions trackers/react-native-tracker/src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,31 @@ export function getTracker(trackerNamespace: string): ReactNativeTracker | undef
return initializedTrackers[trackerNamespace]?.tracker;
}

/**
* Retrieves all initialized trackers
* @returns All initialized trackers
*/
export function getAllTrackers(): ReactNativeTracker[] {
return Object.values(initializedTrackers).map(({ tracker }) => tracker);
}

/**
* Internal function to retrieve the tracker core given its namespace
* @param trackerNamespace - Tracker namespace
* @returns Tracker core if exists
*/
export function getTrackerCore(trackerNamespace: string): TrackerCore | undefined {
return initializedTrackers[trackerNamespace]?.core;
}

/**
* Internal function to retrieve all initialized tracker cores
* @returns All initialized tracker cores
*/
export function getAllTrackerCores(): TrackerCore[] {
return Object.values(initializedTrackers).map(({ core }) => core);
}

/**
* Removes a tracker given its namespace
*
Expand Down
Loading

0 comments on commit 15af8b6

Please sign in to comment.