@chordcommerce/analytics 1.17.1 → 1.18.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.
Files changed (48) hide show
  1. package/README.md +8 -0
  2. package/dist/cjs/client/cdp.d.ts +3 -0
  3. package/dist/cjs/client/cdp.js +123 -0
  4. package/dist/cjs/client/consent/index.d.ts +11 -0
  5. package/dist/cjs/client/consent/index.js +241 -0
  6. package/dist/cjs/client/consent/ketch.d.ts +16 -0
  7. package/dist/cjs/client/consent/ketch.js +53 -0
  8. package/dist/cjs/client/consent/onetrust.d.ts +22 -0
  9. package/dist/cjs/client/consent/onetrust.js +61 -0
  10. package/dist/cjs/client/consent/segment.d.ts +17 -0
  11. package/dist/cjs/client/consent/segment.js +155 -0
  12. package/dist/cjs/client/consent/shopify.d.ts +21 -0
  13. package/dist/cjs/client/consent/shopify.js +51 -0
  14. package/dist/cjs/client/index.d.ts +111 -0
  15. package/dist/cjs/client/index.js +861 -0
  16. package/dist/cjs/client/utils.d.ts +1 -0
  17. package/dist/cjs/client/utils.js +12 -0
  18. package/dist/cjs/index.d.ts +2 -0
  19. package/dist/cjs/index.js +18 -0
  20. package/dist/cjs/setupTests.d.ts +1 -0
  21. package/dist/cjs/setupTests.js +6 -0
  22. package/dist/cjs/types/client.d.ts +96 -0
  23. package/dist/cjs/types/client.js +2 -0
  24. package/dist/cjs/types/consent.d.ts +11 -0
  25. package/dist/cjs/types/consent.js +2 -0
  26. package/dist/cjs/types/formatters.d.ts +114 -0
  27. package/dist/cjs/types/formatters.js +2 -0
  28. package/dist/cjs/types/index.d.ts +7 -0
  29. package/dist/cjs/types/index.js +22 -0
  30. package/dist/cjs/types/input.d.ts +396 -0
  31. package/dist/cjs/types/input.js +2 -0
  32. package/dist/cjs/types/objects.d.ts +379 -0
  33. package/dist/cjs/types/objects.js +2 -0
  34. package/dist/cjs/types/typewriter.d.ts +6502 -0
  35. package/dist/cjs/types/typewriter.js +5 -0
  36. package/dist/cjs/utils.d.ts +2 -0
  37. package/dist/cjs/utils.js +25 -0
  38. package/dist/cjs/validators/custom.d.ts +43 -0
  39. package/dist/cjs/validators/custom.js +18 -0
  40. package/dist/cjs/validators/index.d.ts +4 -0
  41. package/dist/cjs/validators/index.js +20 -0
  42. package/dist/cjs/validators/objects.d.ts +696 -0
  43. package/dist/cjs/validators/objects.js +128 -0
  44. package/dist/cjs/validators/tracking-plan-map.d.ts +6603 -0
  45. package/dist/cjs/validators/tracking-plan-map.js +72 -0
  46. package/dist/cjs/validators/tracking-plan.d.ts +9275 -0
  47. package/dist/cjs/validators/tracking-plan.js +1682 -0
  48. package/package.json +4 -2
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.segmentConsentAdapter = void 0;
51
+ var getApi = function () { return window.segmentConsent; };
52
+ /**
53
+ * Waits for the Segment consent API to be populated with retry logic.
54
+ */
55
+ var waitForApi = function (maxRetries, baseDelay) {
56
+ if (maxRetries === void 0) { maxRetries = 10; }
57
+ if (baseDelay === void 0) { baseDelay = 100; }
58
+ return __awaiter(void 0, void 0, void 0, function () {
59
+ var attempt, checkAndWait;
60
+ return __generator(this, function (_a) {
61
+ attempt = 0;
62
+ checkAndWait = function () { return __awaiter(void 0, void 0, void 0, function () {
63
+ var api, delay_1;
64
+ return __generator(this, function (_a) {
65
+ switch (_a.label) {
66
+ case 0:
67
+ api = getApi();
68
+ if (api &&
69
+ typeof api === 'object' &&
70
+ api.customPreferences &&
71
+ typeof api.customPreferences === 'object' &&
72
+ Object.keys(api.customPreferences).length > 0) {
73
+ return [2 /*return*/, api];
74
+ }
75
+ if (!(attempt < maxRetries)) return [3 /*break*/, 2];
76
+ delay_1 = baseDelay * Math.pow(2, attempt);
77
+ return [4 /*yield*/, new Promise(function (resolve) {
78
+ setTimeout(resolve, delay_1);
79
+ })];
80
+ case 1:
81
+ _a.sent();
82
+ attempt++;
83
+ return [2 /*return*/, checkAndWait()];
84
+ case 2: return [2 /*return*/, null];
85
+ }
86
+ });
87
+ }); };
88
+ return [2 /*return*/, checkAndWait()];
89
+ });
90
+ });
91
+ };
92
+ /**
93
+ * Returns whether the user can currently be tracked.
94
+ */
95
+ var userCanBeTracked = function () { return __awaiter(void 0, void 0, void 0, function () {
96
+ var api;
97
+ return __generator(this, function (_a) {
98
+ switch (_a.label) {
99
+ case 0: return [4 /*yield*/, waitForApi()];
100
+ case 1:
101
+ api = _a.sent();
102
+ return [2 /*return*/, (!!api &&
103
+ !!(api.customPreferences.functional &&
104
+ api.customPreferences.marketingAndAnalytics))];
105
+ }
106
+ });
107
+ }); };
108
+ /**
109
+ * Returns the user's current consent configuration.
110
+ */
111
+ var getCurrentConsent = function () { return __awaiter(void 0, void 0, void 0, function () {
112
+ var api;
113
+ var _a;
114
+ return __generator(this, function (_b) {
115
+ switch (_b.label) {
116
+ case 0: return [4 /*yield*/, waitForApi()];
117
+ case 1:
118
+ api = _b.sent();
119
+ if (!api)
120
+ return [2 /*return*/, {}];
121
+ _a = {
122
+ consentCategories: __assign(__assign({}, api.customPreferences), api.destinationPreferences)
123
+ };
124
+ return [4 /*yield*/, userCanBeTracked()];
125
+ case 2: return [2 /*return*/, (_a.dontSend = !(_b.sent()),
126
+ _a)];
127
+ }
128
+ });
129
+ }); };
130
+ /**
131
+ * Handles subsequent changes to consent configuration.
132
+ */
133
+ var handleConsentUpdates = function (updateCdpConsent) {
134
+ document.addEventListener('chord-cdp:consent-updated', function () { return __awaiter(void 0, void 0, void 0, function () {
135
+ var _a;
136
+ return __generator(this, function (_b) {
137
+ switch (_b.label) {
138
+ case 0:
139
+ _a = updateCdpConsent;
140
+ return [4 /*yield*/, getCurrentConsent()];
141
+ case 1:
142
+ _a.apply(void 0, [_b.sent()]);
143
+ return [2 /*return*/];
144
+ }
145
+ });
146
+ }); });
147
+ };
148
+ var segmentConsentAdapter = function () {
149
+ return {
150
+ getCurrentConsent: getCurrentConsent,
151
+ handleConsentUpdates: handleConsentUpdates,
152
+ userCanBeTracked: userCanBeTracked,
153
+ };
154
+ };
155
+ exports.segmentConsentAdapter = segmentConsentAdapter;
@@ -0,0 +1,21 @@
1
+ import type { ConsentConfiguration } from '../../types/index.js';
2
+ declare global {
3
+ interface Window {
4
+ Shopify?: {
5
+ customerPrivacy?: ShopifyCustomerPrivacyApi;
6
+ };
7
+ }
8
+ }
9
+ interface ShopifyCustomerPrivacyApi {
10
+ getRegion: () => string;
11
+ analyticsProcessingAllowed: () => boolean;
12
+ marketingAllowed: () => boolean;
13
+ preferencesProcessingAllowed: () => boolean;
14
+ saleOfDataAllowed: () => boolean;
15
+ }
16
+ export declare const shopifyConsentAdapter: () => {
17
+ getCurrentConsent: () => ConsentConfiguration;
18
+ handleConsentUpdates: (updateCdpConsent: (consent: ConsentConfiguration) => void) => void;
19
+ userCanBeTracked: () => boolean;
20
+ };
21
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shopifyConsentAdapter = void 0;
4
+ /**
5
+ * Returns Shopify's Customer Privacy API.
6
+ */
7
+ var getApi = function () { var _a; return (_a = window.Shopify) === null || _a === void 0 ? void 0 : _a.customerPrivacy; };
8
+ /**
9
+ * Returns whether the user can currently be tracked.
10
+ */
11
+ var userCanBeTracked = function () {
12
+ var api = getApi();
13
+ if (!api)
14
+ return false;
15
+ return !!(api.getRegion() &&
16
+ api.analyticsProcessingAllowed() &&
17
+ api.marketingAllowed());
18
+ };
19
+ /**
20
+ * Returns the user's current consent configuration.
21
+ */
22
+ var getCurrentConsent = function () {
23
+ var api = getApi();
24
+ if (!api)
25
+ return {};
26
+ return {
27
+ consentCategories: {
28
+ analytics: api.analyticsProcessingAllowed(),
29
+ marketing: api.marketingAllowed(),
30
+ preferences: api.preferencesProcessingAllowed(),
31
+ sale_of_data: api.saleOfDataAllowed(),
32
+ },
33
+ dontSend: !userCanBeTracked(),
34
+ };
35
+ };
36
+ /**
37
+ * Handles subsequent changes to consent configuration.
38
+ */
39
+ var handleConsentUpdates = function (updateCdpConsent) {
40
+ document.addEventListener('visitorConsentCollected', function () {
41
+ updateCdpConsent(getCurrentConsent());
42
+ });
43
+ };
44
+ var shopifyConsentAdapter = function () {
45
+ return {
46
+ getCurrentConsent: getCurrentConsent,
47
+ handleConsentUpdates: handleConsentUpdates,
48
+ userCanBeTracked: userCanBeTracked,
49
+ };
50
+ };
51
+ exports.shopifyConsentAdapter = shopifyConsentAdapter;
@@ -0,0 +1,111 @@
1
+ import type { AnyAnalytics, EventProperties, Options, ValidateResult, ChordAnalyticsOptions, AnyOptions, CartViewedInput, IdentifyTraits, CheckoutStartedInput, CheckoutStepCompletedInput, CheckoutStepViewedInput, EmailCapturedInput, ProductListViewedInput, ProductListFilteredInput, ProductAddedInput, ProductClickedInput, VariantClickedInput, ProductRemovedInput, ProductViewedInput, ProductsSearchedInput, CouponAppliedInput, CouponDeniedInput, CouponEnteredInput, CouponRemovedInput, SignedInInput, SignedOutInput, SignedUpInput, LoginStartedInput, ObjectTypes, SubscriptionCancelledInput, NavigationClickedInput, PaymentInfoEnteredInput } from '../types/index.js';
2
+ import { AnalyticsMiddleware } from '../types/client.js';
3
+ export interface ChordAnalyticsSnippet {
4
+ _loadOptions: ChordAnalyticsOptions<ObjectTypes>;
5
+ invoked: boolean;
6
+ methods: string[];
7
+ queue: any[];
8
+ factory: (method: string) => () => ChordAnalytics;
9
+ load: () => void;
10
+ SNIPPET_VERSION: string;
11
+ }
12
+ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
13
+ /**
14
+ * Allows snippet.js to detect whether this library has been initialized yet.
15
+ */
16
+ initialize: true;
17
+ /**
18
+ * Allows snippet.js to detect whether the snippet has started running yet.
19
+ */
20
+ invoked: true;
21
+ /**
22
+ * Options for ChordAnalytics.
23
+ */
24
+ options: ChordAnalyticsOptions<T>;
25
+ /**
26
+ * Middleware to be applied to the event properties
27
+ */
28
+ middleware: any[];
29
+ /**
30
+ * Promise that resolves when CDP is ready
31
+ */
32
+ private cdpReadyPromise;
33
+ constructor(options: ChordAnalyticsOptions<T>);
34
+ /**
35
+ * Initialize CDP snippet and wait for it to be ready
36
+ */
37
+ private initializeCdp;
38
+ /**
39
+ * Set debug mode for Chord Analytics
40
+ * @param enabled - Whether debug mode should be enabled
41
+ */
42
+ setDebugMode: (enabled: boolean) => void;
43
+ cdp: () => AnyAnalytics;
44
+ ccdp: () => AnyAnalytics;
45
+ logger: (message?: any, ...optionalParams: any[]) => void;
46
+ /**
47
+ * Validate the body of a `track` event against the Chord tracking plan.
48
+ */
49
+ validate: (event: string, props: EventProperties) => ValidateResult[];
50
+ /**
51
+ * Generate the event `meta` property.
52
+ */
53
+ meta: () => {
54
+ ownership: {
55
+ oms_id: string;
56
+ store_id: string;
57
+ tenant_id: string;
58
+ };
59
+ version: {
60
+ major: number;
61
+ minor: number;
62
+ patch: number;
63
+ };
64
+ i18n: import("../types/client.js").i18nMetadata;
65
+ platform: import("../types/client.js").platformMetadata;
66
+ store: import("../types/client.js").storeMetadata;
67
+ };
68
+ addSourceMiddleware: (func: AnalyticsMiddleware) => void;
69
+ /**
70
+ * Send a `track` event to the CDP with any event name and properties.
71
+ */
72
+ track: (event: string, props?: EventProperties, options?: Options) => Promise<void>;
73
+ /**
74
+ * Send an `identify` event to the CDP with user id and traits.
75
+ */
76
+ identify: {
77
+ (userId?: string, traits?: IdentifyTraits, options?: AnyOptions): void;
78
+ (traits?: IdentifyTraits, options?: AnyOptions): void;
79
+ };
80
+ /**
81
+ * Send a `page` event to the CDP.
82
+ */
83
+ page: {
84
+ (props?: Record<string, any>): void;
85
+ };
86
+ reset: () => void;
87
+ trackCartViewed: (props: CartViewedInput<T>, options?: AnyOptions) => Promise<void>;
88
+ trackCheckoutStarted: (props: CheckoutStartedInput<T>, options?: AnyOptions) => Promise<void>;
89
+ trackCheckoutStepCompleted: (props: CheckoutStepCompletedInput, options?: AnyOptions) => Promise<void>;
90
+ trackCheckoutStepViewed: (props: CheckoutStepViewedInput, options?: AnyOptions) => Promise<void>;
91
+ trackCouponApplied: (props: CouponAppliedInput, options?: AnyOptions) => Promise<void>;
92
+ trackCouponDenied: (props: CouponDeniedInput, options?: AnyOptions) => Promise<void>;
93
+ trackCouponEntered: (props: CouponEnteredInput, options?: AnyOptions) => Promise<void>;
94
+ trackCouponRemoved: (props: CouponRemovedInput, options?: AnyOptions) => Promise<void>;
95
+ trackEmailCaptured: (props: EmailCapturedInput, options?: AnyOptions) => Promise<void>;
96
+ trackProductAdded: (props: ProductAddedInput<T>, options?: AnyOptions) => Promise<void>;
97
+ trackProductClicked: (props: ProductClickedInput<T>, options?: AnyOptions) => Promise<void>;
98
+ trackVariantClicked: (props: VariantClickedInput<T>, options?: AnyOptions) => Promise<void>;
99
+ trackProductListFiltered: (props: ProductListFilteredInput, options?: AnyOptions) => Promise<void>;
100
+ trackProductListViewed: (props: ProductListViewedInput<T>, options?: AnyOptions) => Promise<void>;
101
+ trackProductRemoved: (props: ProductRemovedInput<T>, options?: AnyOptions) => Promise<void>;
102
+ trackProductViewed: (props: ProductViewedInput<T>, options?: AnyOptions) => Promise<void>;
103
+ trackProductsSearched: (props: ProductsSearchedInput, options?: AnyOptions) => Promise<void>;
104
+ trackSignedIn: (props: SignedInInput, options?: AnyOptions) => Promise<void>;
105
+ trackSignedOut: (props?: SignedOutInput, options?: AnyOptions) => Promise<void>;
106
+ trackSignedUp: (props?: SignedUpInput, options?: AnyOptions) => Promise<void>;
107
+ trackLoginStarted: (props: LoginStartedInput, options?: AnyOptions) => Promise<void>;
108
+ trackSubscriptionCancelled: (props: SubscriptionCancelledInput, options?: AnyOptions) => Promise<void>;
109
+ trackNavigationClicked: (props: NavigationClickedInput, options?: AnyOptions) => Promise<void>;
110
+ trackPaymentInfoEntered: (props: PaymentInfoEnteredInput, options?: AnyOptions) => Promise<void>;
111
+ }