This repository was archived by the owner on Jan 15, 2026. It is now read-only.
forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.ts
More file actions
233 lines (202 loc) · 7.49 KB
/
Copy pathtracker.ts
File metadata and controls
233 lines (202 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { trackerCore, PayloadBuilder, version, EmitterConfiguration, TrackerCore } from '@snowplow/tracker-core';
import { newEmitter } from '@snowplow/tracker-core';
import { newReactNativeEventStore } from './event_store';
import { newTrackEventFunctions } from './events';
import { newSubject } from './subject';
import {
ScreenTrackingConfiguration,
ScreenTrackingPlugin,
trackListItemView,
trackScreenView,
trackScrollChanged,
} from '@snowplow/browser-plugin-screen-tracking';
import {
DeepLinkConfiguration,
AppLifecycleConfiguration,
EventContext,
EventStoreConfiguration,
ListItemViewProps,
PlatformContextConfiguration,
ReactNativeTracker,
ScreenViewProps,
ScrollChangedProps,
SessionConfiguration,
SubjectConfiguration,
TrackerConfiguration,
} from './types';
import { newSessionPlugin } from './plugins/session';
import { newDeepLinksPlugin } from './plugins/deep_links';
import { newPlugins } from './plugins';
import { newPlatformContextPlugin } from './plugins/platform_context';
import { newAppLifecyclePlugin } from './plugins/app_lifecycle';
import { newAppInstallPlugin } from './plugins/app_install';
import { newAppContextPlugin } from './plugins/app_context';
import DefaultAsyncStorage from '@react-native-async-storage/async-storage';
const initializedTrackers: Record<string, { tracker: ReactNativeTracker; core: TrackerCore }> = {};
type SetPropertiesAsNonNullable<Obj, Properties extends keyof Obj> = Omit<Obj, Properties> & {
[K in Properties]-?: NonNullable<Obj[K]>;
};
type Configuration = TrackerConfiguration &
EmitterConfiguration &
SessionConfiguration &
SubjectConfiguration &
EventStoreConfiguration &
ScreenTrackingConfiguration &
PlatformContextConfiguration &
DeepLinkConfiguration &
AppLifecycleConfiguration;
type NormalizedConfiguration = SetPropertiesAsNonNullable<
Configuration,
'asyncStorage' | 'devicePlatform' | 'encodeBase64' | 'eventStore' | 'plugins'
>;
const normalizeTrackerConfiguration = async (configuration: Configuration): Promise<NormalizedConfiguration> => {
const eventStore = configuration.eventStore ?? (await newReactNativeEventStore(configuration));
const asyncStorage = configuration.asyncStorage ?? DefaultAsyncStorage;
const plugins = configuration.plugins ?? [];
const devicePlatform = configuration.devicePlatform ?? 'mob';
const encodeBase64 = configuration.encodeBase64 ?? false;
return {
...configuration,
devicePlatform,
encodeBase64,
asyncStorage,
eventStore,
plugins,
};
};
/**
* Creates a new tracker instance with the given configuration
* @param configuration - Configuration for the tracker
* @returns Tracker instance
*/
export async function newTracker(configuration: Configuration): Promise<ReactNativeTracker> {
const normalizedConfiguration = await normalizeTrackerConfiguration(configuration);
const { namespace, appId, encodeBase64 } = normalizedConfiguration;
const emitter = newEmitter(normalizedConfiguration);
const callback = (payload: PayloadBuilder): void => {
emitter.input(payload.build());
};
const core = trackerCore({ base64: encodeBase64, callback });
core.setPlatform(normalizedConfiguration.devicePlatform);
core.setTrackerVersion('rn-' + version);
core.setTrackerNamespace(namespace);
if (appId) {
core.setAppId(appId);
}
const { addPlugin } = newPlugins(namespace, core);
const sessionPlugin = await newSessionPlugin(normalizedConfiguration);
addPlugin(sessionPlugin);
const deepLinksPlugin = newDeepLinksPlugin(normalizedConfiguration, core);
addPlugin(deepLinksPlugin);
const subject = newSubject(core, normalizedConfiguration);
addPlugin(subject.subjectPlugin);
const screenPlugin = ScreenTrackingPlugin(normalizedConfiguration);
addPlugin({ plugin: screenPlugin });
const platformContextPlugin = await newPlatformContextPlugin(normalizedConfiguration);
addPlugin(platformContextPlugin);
const lifecyclePlugin = await newAppLifecyclePlugin(normalizedConfiguration, core);
addPlugin(lifecyclePlugin);
const installPlugin = newAppInstallPlugin(normalizedConfiguration, core);
addPlugin(installPlugin);
const appContextPlugin = newAppContextPlugin(normalizedConfiguration);
addPlugin(appContextPlugin);
normalizedConfiguration.plugins.forEach((plugin) => addPlugin({ plugin }));
const tracker: ReactNativeTracker = {
...newTrackEventFunctions(core),
...subject.properties,
namespace,
setAppId: core.setAppId,
setPlatform: core.setPlatform,
flush: emitter.flush,
addGlobalContexts: core.addGlobalContexts,
removeGlobalContexts: core.removeGlobalContexts,
clearGlobalContexts: core.clearGlobalContexts,
enablePlatformContext: platformContextPlugin.enablePlatformContext,
disablePlatformContext: platformContextPlugin.disablePlatformContext,
refreshPlatformContext: platformContextPlugin.refreshPlatformContext,
getSessionId: sessionPlugin.getSessionId,
getSessionIndex: sessionPlugin.getSessionIndex,
getSessionUserId: sessionPlugin.getSessionUserId,
getSessionState: sessionPlugin.getSessionState,
addPlugin,
trackScreenViewEvent: (argmap: ScreenViewProps, context?: EventContext[]) =>
trackScreenView(
{
...argmap,
context,
},
[namespace]
),
trackScrollChangedEvent: (argmap: ScrollChangedProps, context?: EventContext[]) =>
trackScrollChanged(
{
...argmap,
context,
},
[namespace]
),
trackListItemViewEvent: (argmap: ListItemViewProps, context?: EventContext[]) =>
trackListItemView(
{
...argmap,
context,
},
[namespace]
),
trackDeepLinkReceivedEvent: deepLinksPlugin.trackDeepLinkReceivedEvent,
getIsInBackground: lifecyclePlugin.getIsInBackground,
getBackgroundIndex: lifecyclePlugin.getBackgroundIndex,
getForegroundIndex: lifecyclePlugin.getForegroundIndex,
};
initializedTrackers[namespace] = { tracker, core };
return tracker;
}
/**
* Retrieves an initialized tracker given its namespace
* @param trackerNamespace - Tracker namespace
* @returns Tracker instance if exists
*/
export function getTracker(trackerNamespace: string): ReactNativeTracker | undefined {
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
*
* @param trackerNamespace - Tracker namespace
*/
export function removeTracker(trackerNamespace: string): void {
if (initializedTrackers[trackerNamespace]) {
initializedTrackers[trackerNamespace]?.core.deactivate();
delete initializedTrackers[trackerNamespace];
}
}
/**
* Removes all initialized trackers
*
* @returns - A boolean promise
*/
export function removeAllTrackers(): void {
Object.keys(initializedTrackers).forEach(removeTracker);
}