forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
138 lines (117 loc) · 3.93 KB
/
Copy pathapi.ts
File metadata and controls
138 lines (117 loc) · 3.93 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
import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@snowplow/browser-tracker-core';
import { buildConsentEvent, buildCmpVisibleEvent } from './core';
import { CommonConsentEventProperties, Consent, CmpVisible } from './types';
const _trackers: Record<string, BrowserTracker> = {};
type ConsentEventProperties = Omit<Consent, 'eventType'> & CommonConsentEventProperties;
/**
* Adds consent tracking
*/
export function EnhancedConsentPlugin(): BrowserPlugin {
let trackerId: string;
return {
activateBrowserPlugin: (tracker) => {
trackerId = tracker.id;
_trackers[trackerId] = tracker;
},
};
}
/**
* Track the loadtime of a CMP banner
*/
export function trackCmpVisible(
cmpVisible: CmpVisible & CommonConsentEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { context, timestamp, elapsedTime } = cmpVisible;
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
t.core.track(
buildCmpVisibleEvent({
elapsedTime,
}),
context,
timestamp
);
});
}
// Generic consent action
function trackConsentAction(
consent: Consent & CommonConsentEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { context, timestamp, ...consentAttributes } = consent;
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
t.core.track(buildConsentEvent(consentAttributes), context, timestamp);
});
}
/**
* Track an allow consent event
*
* @param consent - The consent information
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackConsentAllow(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) {
trackConsentAction({ ...consent, eventType: 'allow_all' }, trackers);
}
/**
* Track an allow selected consent event
*
* @param consent - The consent information
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackConsentSelected(
consent: ConsentEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
trackConsentAction({ ...consent, eventType: 'allow_selected' }, trackers);
}
/**
* Track a consent pending event
*
* @param consent - The consent information
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackConsentPending(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) {
trackConsentAction({ ...consent, eventType: 'pending' }, trackers);
}
/**
* Track an implicit consent event
*
* @param consent - The consent information
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackConsentImplicit(
consent: ConsentEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
trackConsentAction({ ...consent, eventType: 'implicit_consent' }, trackers);
}
/**
* Track a deny consent event
*
* @param consent - The consent information
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackConsentDeny(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) {
trackConsentAction({ ...consent, eventType: 'deny_all' }, trackers);
}
/**
* Track a consent expired event
*
* @param consent - The consent information
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackConsentExpired(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) {
trackConsentAction({ ...consent, eventType: 'expired' }, trackers);
}
/**
* Track a consent withdrawn event
*
* @param consent - The consent information
* @param trackers - The tracker identifiers which the event will be sent to
*/
export function trackConsentWithdrawn(
consent: ConsentEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
trackConsentAction({ ...consent, eventType: 'withdrawn' }, trackers);
}