@chordcommerce/analytics 1.13.0 → 1.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/cdp.js +3 -1
- package/dist/client/consent/index.js +26 -7
- package/dist/client/utils.d.ts +1 -0
- package/dist/client/utils.js +8 -0
- package/package.json +1 -1
package/dist/client/cdp.js
CHANGED
|
@@ -8,15 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { addCdpConsent } from './consent/index.js';
|
|
11
|
+
import { normalizeCDPDomain } from './utils.js';
|
|
11
12
|
/*
|
|
12
13
|
* Adds the Chord CDP snippet to the page.
|
|
13
14
|
*/
|
|
14
15
|
export const addCdpSnippet = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
16
|
if (!document || typeof document.createElement === 'undefined')
|
|
16
17
|
return;
|
|
18
|
+
const normalizedCDPDomain = normalizeCDPDomain(options.cdpDomain);
|
|
17
19
|
const s = document.createElement('script');
|
|
18
20
|
s.async = true;
|
|
19
|
-
s.src = `${
|
|
21
|
+
s.src = `${normalizedCDPDomain}/p.js`;
|
|
20
22
|
s.setAttribute('data-debug', `${!!options.debug}`);
|
|
21
23
|
s.setAttribute('data-init-only', 'true');
|
|
22
24
|
s.setAttribute('data-namespace', `_${options.namespace}`);
|
|
@@ -12,6 +12,7 @@ import { onetrustConsentAdapter } from './onetrust.js';
|
|
|
12
12
|
import { shopifyConsentAdapter } from './shopify.js';
|
|
13
13
|
export const addCdpConsent = (options) => {
|
|
14
14
|
let adapter = null;
|
|
15
|
+
let lastProcessedConsent = null;
|
|
15
16
|
const POLLING_INTERVAL_MS = 50;
|
|
16
17
|
const TIMEOUT_MS = 60000;
|
|
17
18
|
/**
|
|
@@ -56,8 +57,22 @@ export const addCdpConsent = (options) => {
|
|
|
56
57
|
/**
|
|
57
58
|
* Updates consent for all future events.
|
|
58
59
|
*/
|
|
59
|
-
const updateCdpConsent = (updatedConsent) => {
|
|
60
|
+
const updateCdpConsent = (updatedConsent) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
if (lastProcessedConsent &&
|
|
62
|
+
JSON.stringify(lastProcessedConsent) === JSON.stringify(updatedConsent)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
lastProcessedConsent = updatedConsent;
|
|
66
|
+
const chordAnalytics = window[options.namespace];
|
|
60
67
|
const cdp = window[`_${options.namespace}`];
|
|
68
|
+
const shouldTrackConsentUpdate = chordAnalytics &&
|
|
69
|
+
typeof chordAnalytics.track === 'function' &&
|
|
70
|
+
updatedConsent.consentCategories !== undefined;
|
|
71
|
+
if (shouldTrackConsentUpdate &&
|
|
72
|
+
// we have to send this BEFORE the CDP is reconfigured or else the track will not be sent, usually in the case of a user opting out
|
|
73
|
+
updatedConsent.dontSend === true) {
|
|
74
|
+
yield chordAnalytics.track('Consent Preferences Updated', updatedConsent);
|
|
75
|
+
}
|
|
61
76
|
if (cdp && typeof cdp.configure === 'function') {
|
|
62
77
|
cdp.configure({ privacy: updatedConsent });
|
|
63
78
|
}
|
|
@@ -65,21 +80,25 @@ export const addCdpConsent = (options) => {
|
|
|
65
80
|
// CDP has not yet loaded via the snippet.
|
|
66
81
|
window.jitsuConfig = { privacy: updatedConsent };
|
|
67
82
|
}
|
|
68
|
-
|
|
83
|
+
if (shouldTrackConsentUpdate &&
|
|
84
|
+
// we have to send this AFTER the CDP is reconfigured to ensure the track is sent, usually in the case of a user opting in
|
|
85
|
+
updatedConsent.dontSend === false) {
|
|
86
|
+
yield chordAnalytics.track('Consent Preferences Updated', updatedConsent);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
69
89
|
/**
|
|
70
90
|
* Sets up consent management by initializing the adapter and handling consent updates.
|
|
71
91
|
*/
|
|
72
92
|
const setupConsentManagement = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
73
93
|
if (!adapter)
|
|
74
94
|
return;
|
|
75
|
-
|
|
76
|
-
if (!isReady)
|
|
77
|
-
return;
|
|
78
|
-
const initialConsent = adapter.getCurrentConsent();
|
|
79
|
-
updateCdpConsent(initialConsent);
|
|
95
|
+
// Set up the consent update handler first to catch any immediate updates
|
|
80
96
|
adapter.handleConsentUpdates((updatedConsent) => {
|
|
81
97
|
updateCdpConsent(updatedConsent);
|
|
82
98
|
});
|
|
99
|
+
// Process initial consent - this will be skipped if the adapter already triggered an update
|
|
100
|
+
const initialConsent = adapter.getCurrentConsent();
|
|
101
|
+
updateCdpConsent(initialConsent);
|
|
83
102
|
});
|
|
84
103
|
/**
|
|
85
104
|
* Initializes consent management.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function normalizeCDPDomain(cdpDomain: string): string;
|
package/package.json
CHANGED