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
68 lines (57 loc) · 2.03 KB
/
Copy pathindex.ts
File metadata and controls
68 lines (57 loc) · 2.03 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
import { buildSelfDescribingEvent, CorePluginConfiguration, PayloadBuilder, TrackerCore } from '@snowplow/tracker-core';
import { DeepLinkConfiguration, DeepLinkReceivedProps, EventContext } from '../../types';
import { DEEP_LINK_ENTITY_SCHEMA, DEEP_LINK_RECEIVED_EVENT_SCHEMA, PAGE_REFERRER_PROPERTY, PAGE_URL_PROPERTY, SCREEN_VIEW_EVENT_SCHEMA } from '../../constants';
import { getUsefulSchema } from '../../utils';
interface DeepLinksPlugin extends CorePluginConfiguration {
trackDeepLinkReceivedEvent: (argmap: DeepLinkReceivedProps, contexts?: EventContext[]) => void;
}
export function newDeepLinksPlugin(
{ deepLinkContext = true }: DeepLinkConfiguration,
core: TrackerCore
): DeepLinksPlugin {
let lastDeepLink: DeepLinkReceivedProps | undefined;
const beforeTrack = (payloadBuilder: PayloadBuilder) => {
const schema = getUsefulSchema(payloadBuilder);
if (schema == SCREEN_VIEW_EVENT_SCHEMA && lastDeepLink) {
const { url, referrer } = lastDeepLink;
if (url) {
payloadBuilder.add(PAGE_URL_PROPERTY, url);
}
if (referrer) {
payloadBuilder.add(PAGE_REFERRER_PROPERTY, referrer);
}
if (deepLinkContext) {
payloadBuilder.addContextEntity({
schema: DEEP_LINK_ENTITY_SCHEMA,
data: lastDeepLink,
});
}
// Clear the last deep link since we only add it to the first screen view event
lastDeepLink = undefined;
}
};
const trackDeepLinkReceivedEvent = (argmap: DeepLinkReceivedProps, contexts?: EventContext[]) => {
lastDeepLink = argmap;
const payload = buildSelfDescribingEvent({
event: {
schema: DEEP_LINK_RECEIVED_EVENT_SCHEMA,
data: argmap,
},
});
// Add atomic event properties
const { url, referrer } = argmap;
if (url) {
payload.add(PAGE_URL_PROPERTY, url);
}
if (referrer) {
payload.add(PAGE_REFERRER_PROPERTY, referrer);
}
core.track(payload, contexts);
};
return {
trackDeepLinkReceivedEvent,
plugin: {
beforeTrack,
},
};
}