forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
41 lines (40 loc) · 1.34 KB
/
Copy pathindex.ts
File metadata and controls
41 lines (40 loc) · 1.34 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
import type { CorePluginConfiguration, TrackerCore } from '@snowplow/tracker-core';
import { buildSelfDescribingEvent } from '@snowplow/tracker-core';
import { APPLICATION_INSTALL_EVENT_SCHEMA } from '../../constants';
import type { AppLifecycleConfiguration, AsyncStorage, TrackerConfiguration } from '../../types';
/**
* Tracks an application install event on the first run of the app.
* Stores the install event in AsyncStorage to prevent tracking on subsequent runs.
*
* Event schema: `iglu:com.snowplowanalytics.mobile/application_install/jsonschema/1-0-0`
*/
export function newAppInstallPlugin(
{
asyncStorage,
namespace,
installAutotracking = false,
}: TrackerConfiguration & AppLifecycleConfiguration & { asyncStorage: AsyncStorage },
core: TrackerCore
): CorePluginConfiguration {
if (installAutotracking) {
// Track install event on first run
const key = `snowplow_${namespace}_install`;
setTimeout(async () => {
const installEvent = await asyncStorage.getItem(key);
if (!installEvent) {
core.track(
buildSelfDescribingEvent({
event: {
schema: APPLICATION_INSTALL_EVENT_SCHEMA,
data: {},
},
})
);
await asyncStorage.setItem(key, new Date().toISOString());
}
}, 0);
}
return {
plugin: {},
};
}