@chordcommerce/analytics 1.11.0 → 1.12.0

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.
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { ketchConsentAdapter } from './ketch.js';
11
+ import { onetrustConsentAdapter } from './onetrust.js';
11
12
  import { shopifyConsentAdapter } from './shopify.js';
12
13
  export const addCdpConsent = (options) => {
13
14
  let adapter;
@@ -19,6 +20,8 @@ export const addCdpConsent = (options) => {
19
20
  switch ((_a = options.consent) === null || _a === void 0 ? void 0 : _a.toLowerCase()) {
20
21
  case 'ketch':
21
22
  return ketchConsentAdapter();
23
+ case 'onetrust':
24
+ return onetrustConsentAdapter();
22
25
  case 'shopify':
23
26
  return shopifyConsentAdapter();
24
27
  default:
@@ -0,0 +1,22 @@
1
+ import type { ConsentConfiguration } from '../../types/index.js';
2
+ declare global {
3
+ interface Window {
4
+ OneTrust?: OneTrustApi;
5
+ OnetrustActiveGroups?: string;
6
+ }
7
+ }
8
+ interface OneTrustApi {
9
+ OnConsentChanged: (callback: () => void) => void;
10
+ GetDomainData: () => {
11
+ Groups: OneTrustGroup[];
12
+ };
13
+ }
14
+ interface OneTrustGroup {
15
+ CustomGroupId: string;
16
+ }
17
+ export declare const onetrustConsentAdapter: () => {
18
+ getCurrentConsent: () => ConsentConfiguration;
19
+ handleConsentUpdates: (updateCdpConsent: (consent: ConsentConfiguration) => void) => void;
20
+ userCanBeTracked: () => boolean;
21
+ };
22
+ export {};
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Returns OneTrust's API.
3
+ */
4
+ const getApi = () => {
5
+ const oneTrust = window.OneTrust;
6
+ if (oneTrust &&
7
+ typeof oneTrust === 'object' &&
8
+ 'OnConsentChanged' in oneTrust &&
9
+ 'GetDomainData' in oneTrust) {
10
+ return oneTrust;
11
+ }
12
+ return undefined;
13
+ };
14
+ /**
15
+ * Returns whether the user can currently be tracked.
16
+ */
17
+ const userCanBeTracked = () => {
18
+ // Always loads, a la `consentModel: 'opt-out'` in the Segment wrapper.
19
+ const api = getApi();
20
+ return !!api;
21
+ };
22
+ /**
23
+ * Returns the user's current consent configuration.
24
+ */
25
+ const getCurrentConsent = () => {
26
+ const api = getApi();
27
+ const activeGroups = window.OnetrustActiveGroups;
28
+ if (!api || !activeGroups || typeof activeGroups !== 'string')
29
+ return {};
30
+ const activeGroupIds = new Set(activeGroups.trim().split(',').filter(Boolean));
31
+ return {
32
+ consentCategories: api
33
+ .GetDomainData()
34
+ .Groups.reduce((acc, { CustomGroupId }) => {
35
+ const groupId = CustomGroupId.trim();
36
+ acc[groupId] = activeGroupIds.has(groupId);
37
+ return acc;
38
+ }, {}),
39
+ dontSend: !userCanBeTracked(),
40
+ };
41
+ };
42
+ /**
43
+ * Handles subsequent changes to consent configuration.
44
+ */
45
+ const handleConsentUpdates = (updateCdpConsent) => {
46
+ getApi().OnConsentChanged(() => {
47
+ updateCdpConsent(getCurrentConsent());
48
+ });
49
+ };
50
+ export const onetrustConsentAdapter = () => {
51
+ return {
52
+ getCurrentConsent,
53
+ handleConsentUpdates,
54
+ userCanBeTracked,
55
+ };
56
+ };
@@ -30,7 +30,7 @@ export interface ChordAnalyticsOptions<T extends ObjectTypes = ObjectTypes> {
30
30
  cdp?: AnyAnalytics | (() => AnyAnalytics);
31
31
  cdpDomain?: string;
32
32
  cdpWriteKey?: string;
33
- consent?: 'ketch' | 'shopify';
33
+ consent?: 'ketch' | 'onetrust' | 'shopify';
34
34
  debug?: boolean;
35
35
  enableLogging?: boolean;
36
36
  formatters: ChordAnalyticsFormatters<T>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chordcommerce/analytics",
3
3
  "description": "Chord Commerce event tracking",
4
- "version": "1.11.0",
4
+ "version": "1.12.0",
5
5
  "author": "Chord Commerce <help@chord.co> (https://www.chord.co)",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",