|
| 1 | +import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@snowplow/browser-tracker-core'; |
| 2 | +import { buildConsentEvent, buildCmpVisibleEvent } from './core'; |
| 3 | +import { CommonConsentEventProperties, Consent, CmpVisible } from './types'; |
| 4 | + |
| 5 | +const _trackers: Record<string, BrowserTracker> = {}; |
| 6 | + |
| 7 | +type ConsentEventProperties = Omit<Consent, 'eventType'> & CommonConsentEventProperties; |
| 8 | + |
| 9 | +/** |
| 10 | + * Adds consent tracking |
| 11 | + */ |
| 12 | + |
| 13 | +export function EnhancedConsentPlugin(): BrowserPlugin { |
| 14 | + let trackerId: string; |
| 15 | + return { |
| 16 | + activateBrowserPlugin: (tracker) => { |
| 17 | + trackerId = tracker.id; |
| 18 | + _trackers[trackerId] = tracker; |
| 19 | + }, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Track the loadtime of a CMP banner |
| 25 | + */ |
| 26 | + |
| 27 | +export function trackCmpVisible( |
| 28 | + cmpVisible: CmpVisible & CommonConsentEventProperties, |
| 29 | + trackers: Array<string> = Object.keys(_trackers) |
| 30 | +) { |
| 31 | + const { context, timestamp, elapsedTime } = cmpVisible; |
| 32 | + dispatchToTrackersInCollection(trackers, _trackers, (t) => { |
| 33 | + t.core.track( |
| 34 | + buildCmpVisibleEvent({ |
| 35 | + elapsedTime, |
| 36 | + }), |
| 37 | + context, |
| 38 | + timestamp |
| 39 | + ); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +// Generic consent action |
| 44 | +function trackConsentAction( |
| 45 | + consent: Consent & CommonConsentEventProperties, |
| 46 | + trackers: Array<string> = Object.keys(_trackers) |
| 47 | +) { |
| 48 | + const { context, timestamp, ...consentAttributes } = consent; |
| 49 | + dispatchToTrackersInCollection(trackers, _trackers, (t) => { |
| 50 | + t.core.track(buildConsentEvent(consentAttributes), context, timestamp); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Track an allow consent event |
| 56 | + * |
| 57 | + * @param consent - The consent information |
| 58 | + * @param trackers - The tracker identifiers which the event will be sent to |
| 59 | + */ |
| 60 | + |
| 61 | +export function trackConsentAllow(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) { |
| 62 | + trackConsentAction({ ...consent, eventType: 'allow_all' }, trackers); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Track an allow selected consent event |
| 67 | + * |
| 68 | + * @param consent - The consent information |
| 69 | + * @param trackers - The tracker identifiers which the event will be sent to |
| 70 | + */ |
| 71 | + |
| 72 | +export function trackConsentSelected( |
| 73 | + consent: ConsentEventProperties, |
| 74 | + trackers: Array<string> = Object.keys(_trackers) |
| 75 | +) { |
| 76 | + trackConsentAction({ ...consent, eventType: 'allow_selected' }, trackers); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Track a consent pending event |
| 81 | + * |
| 82 | + * @param consent - The consent information |
| 83 | + * @param trackers - The tracker identifiers which the event will be sent to |
| 84 | + */ |
| 85 | + |
| 86 | +export function trackConsentPending(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) { |
| 87 | + trackConsentAction({ ...consent, eventType: 'pending' }, trackers); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Track an implicit consent event |
| 92 | + * |
| 93 | + * @param consent - The consent information |
| 94 | + * @param trackers - The tracker identifiers which the event will be sent to |
| 95 | + */ |
| 96 | + |
| 97 | +export function trackConsentImplicit( |
| 98 | + consent: ConsentEventProperties, |
| 99 | + trackers: Array<string> = Object.keys(_trackers) |
| 100 | +) { |
| 101 | + trackConsentAction({ ...consent, eventType: 'implicit_consent' }, trackers); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Track a deny consent event |
| 106 | + * |
| 107 | + * @param consent - The consent information |
| 108 | + * @param trackers - The tracker identifiers which the event will be sent to |
| 109 | + */ |
| 110 | + |
| 111 | +export function trackConsentDeny(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) { |
| 112 | + trackConsentAction({ ...consent, eventType: 'deny_all' }, trackers); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Track a consent expired event |
| 117 | + * |
| 118 | + * @param consent - The consent information |
| 119 | + * @param trackers - The tracker identifiers which the event will be sent to |
| 120 | + */ |
| 121 | + |
| 122 | +export function trackConsentExpired(consent: ConsentEventProperties, trackers: Array<string> = Object.keys(_trackers)) { |
| 123 | + trackConsentAction({ ...consent, eventType: 'expired' }, trackers); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Track a consent withdrawn event |
| 128 | + * |
| 129 | + * @param consent - The consent information |
| 130 | + * @param trackers - The tracker identifiers which the event will be sent to |
| 131 | + */ |
| 132 | + |
| 133 | +export function trackConsentWithdrawn( |
| 134 | + consent: ConsentEventProperties, |
| 135 | + trackers: Array<string> = Object.keys(_trackers) |
| 136 | +) { |
| 137 | + trackConsentAction({ ...consent, eventType: 'withdrawn' }, trackers); |
| 138 | +} |
0 commit comments