@chordcommerce/analytics 1.22.0-beta.3 → 1.22.1-beta.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/cjs/client/cdp.js +15 -9
- package/dist/cjs/client/index.d.ts +0 -13
- package/dist/cjs/client/index.js +24 -38
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +0 -1
- package/dist/cjs/types/client.d.ts +0 -13
- package/dist/client/cdp.js +15 -8
- package/dist/client/index.d.ts +0 -13
- package/dist/client/index.js +98 -111
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/types/client.d.ts +0 -13
- package/package.json +1 -1
- package/dist/cjs/client/identifiers.d.ts +0 -55
- package/dist/cjs/client/identifiers.js +0 -197
- package/dist/client/identifiers.d.ts +0 -55
- package/dist/client/identifiers.js +0 -135
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shopify Identifier Sync
|
|
3
|
-
*
|
|
4
|
-
* Syncs CDP anonymous ID and Google Client ID to Shopify cart attributes.
|
|
5
|
-
* This enables server-side events (webhooks) to correlate with client-side tracking.
|
|
6
|
-
*/
|
|
7
|
-
export interface ShopifyCart {
|
|
8
|
-
attributes?: Record<string, unknown>;
|
|
9
|
-
[key: string]: unknown;
|
|
10
|
-
}
|
|
11
|
-
export interface IdentifierSyncOptions {
|
|
12
|
-
/**
|
|
13
|
-
* Base URL for Shopify AJAX API calls. Defaults to window.location.origin
|
|
14
|
-
*/
|
|
15
|
-
baseUrl?: string;
|
|
16
|
-
/**
|
|
17
|
-
* Locale path for Shopify routes. Defaults to window.Shopify.routes.root or '/'
|
|
18
|
-
*/
|
|
19
|
-
localePath?: string;
|
|
20
|
-
/**
|
|
21
|
-
* Custom identifier getters. By default, reads CDP anonymous ID and Google Client ID.
|
|
22
|
-
*/
|
|
23
|
-
identifiers?: () => Record<string, string>;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Get a cookie value by name
|
|
27
|
-
*/
|
|
28
|
-
export declare const getCookie: (name: string) => string | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* Get default identifiers (CDP anonymous ID and Google Client ID)
|
|
31
|
-
*/
|
|
32
|
-
export declare const getDefaultIdentifiers: () => Record<string, string>;
|
|
33
|
-
/**
|
|
34
|
-
* Fetch the current Shopify cart
|
|
35
|
-
*/
|
|
36
|
-
export declare const getShopifyCart: (baseUrl?: string, localePath?: string) => Promise<ShopifyCart>;
|
|
37
|
-
/**
|
|
38
|
-
* Update Shopify cart attributes
|
|
39
|
-
*/
|
|
40
|
-
export declare const updateShopifyCartAttributes: (attributes: Record<string, string>, baseUrl?: string, localePath?: string) => Promise<ShopifyCart>;
|
|
41
|
-
/**
|
|
42
|
-
* Sync CDP and analytics identifiers to Shopify cart attributes.
|
|
43
|
-
*
|
|
44
|
-
* By default, syncs the Chord CDP anonymous ID (`__eventn_id` cookie) and
|
|
45
|
-
* Google Analytics client ID (`_ga` cookie) to the cart. These attributes
|
|
46
|
-
* are included in Shopify order webhooks, enabling server-side events to
|
|
47
|
-
* correlate with client-side tracking sessions.
|
|
48
|
-
*
|
|
49
|
-
* Smart deduplication: only updates the cart if identifiers are missing or
|
|
50
|
-
* have changed, avoiding unnecessary API calls on repeat page loads.
|
|
51
|
-
*
|
|
52
|
-
* @param options - Optional configuration for base URL, locale path, or custom identifier getter
|
|
53
|
-
* @returns The updated cart, or undefined if no update was needed or an error occurred
|
|
54
|
-
*/
|
|
55
|
-
export declare const syncIdentifiersToCart: (options?: IdentifierSyncOptions) => Promise<ShopifyCart | undefined>;
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Shopify Identifier Sync
|
|
4
|
-
*
|
|
5
|
-
* Syncs CDP anonymous ID and Google Client ID to Shopify cart attributes.
|
|
6
|
-
* This enables server-side events (webhooks) to correlate with client-side tracking.
|
|
7
|
-
*/
|
|
8
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
18
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
19
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
20
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
21
|
-
function step(op) {
|
|
22
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
23
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
24
|
-
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;
|
|
25
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
26
|
-
switch (op[0]) {
|
|
27
|
-
case 0: case 1: t = op; break;
|
|
28
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
29
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
30
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
31
|
-
default:
|
|
32
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
33
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
34
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
35
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
36
|
-
if (t[2]) _.ops.pop();
|
|
37
|
-
_.trys.pop(); continue;
|
|
38
|
-
}
|
|
39
|
-
op = body.call(thisArg, _);
|
|
40
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
41
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
-
exports.syncIdentifiersToCart = exports.updateShopifyCartAttributes = exports.getShopifyCart = exports.getDefaultIdentifiers = exports.getCookie = void 0;
|
|
46
|
-
/**
|
|
47
|
-
* Get a cookie value by name
|
|
48
|
-
*/
|
|
49
|
-
var getCookie = function (name) {
|
|
50
|
-
var _a;
|
|
51
|
-
if (typeof document === 'undefined')
|
|
52
|
-
return undefined;
|
|
53
|
-
return (_a = document.cookie
|
|
54
|
-
.split('; ')
|
|
55
|
-
.find(function (row) { return row.startsWith("".concat(name, "=")); })) === null || _a === void 0 ? void 0 : _a.split('=')[1];
|
|
56
|
-
};
|
|
57
|
-
exports.getCookie = getCookie;
|
|
58
|
-
/**
|
|
59
|
-
* Get default identifiers (CDP anonymous ID and Google Client ID)
|
|
60
|
-
*/
|
|
61
|
-
var getDefaultIdentifiers = function () {
|
|
62
|
-
var attributes = {};
|
|
63
|
-
try {
|
|
64
|
-
// CDP anonymous ID (Jitsu/Chord CDP)
|
|
65
|
-
var anonymousId = (0, exports.getCookie)('__eventn_id');
|
|
66
|
-
if (anonymousId) {
|
|
67
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
68
|
-
attributes._cdpAnonymousId = anonymousId;
|
|
69
|
-
}
|
|
70
|
-
// Google Client ID (strip the "GA1.1." or similar prefix)
|
|
71
|
-
var gaCookie = (0, exports.getCookie)('_ga');
|
|
72
|
-
if (gaCookie) {
|
|
73
|
-
var googleClientId = gaCookie.substring(6);
|
|
74
|
-
if (googleClientId) {
|
|
75
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
76
|
-
attributes._googleClientId = googleClientId;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
catch (e) {
|
|
81
|
-
// eslint-disable-next-line no-console
|
|
82
|
-
console.error('[Chord Analytics]: Error getting identifiers:', e);
|
|
83
|
-
}
|
|
84
|
-
return attributes;
|
|
85
|
-
};
|
|
86
|
-
exports.getDefaultIdentifiers = getDefaultIdentifiers;
|
|
87
|
-
/**
|
|
88
|
-
* Construct a URL for Shopify AJAX API calls
|
|
89
|
-
*/
|
|
90
|
-
var constructUrl = function (path, baseUrl, localePath) {
|
|
91
|
-
var _a, _b;
|
|
92
|
-
var defaultBaseUrl = typeof window !== 'undefined' ? (_a = window.location) === null || _a === void 0 ? void 0 : _a.origin : undefined;
|
|
93
|
-
// Access Shopify.routes.root for locale-aware URLs (cast needed as Window.Shopify type is partial)
|
|
94
|
-
var shopify = window === null || window === void 0 ? void 0 : window.Shopify;
|
|
95
|
-
var defaultLocalePath = ((_b = shopify === null || shopify === void 0 ? void 0 : shopify.routes) === null || _b === void 0 ? void 0 : _b.root) || '/';
|
|
96
|
-
var resolvedBaseUrl = baseUrl !== null && baseUrl !== void 0 ? baseUrl : defaultBaseUrl;
|
|
97
|
-
var resolvedLocalePath = localePath !== null && localePath !== void 0 ? localePath : defaultLocalePath;
|
|
98
|
-
var pathWithLocale = "".concat(resolvedLocalePath).concat(path);
|
|
99
|
-
if (!resolvedBaseUrl) {
|
|
100
|
-
return pathWithLocale.startsWith('/')
|
|
101
|
-
? pathWithLocale
|
|
102
|
-
: "/".concat(pathWithLocale);
|
|
103
|
-
}
|
|
104
|
-
return new URL(pathWithLocale, resolvedBaseUrl);
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* Fetch the current Shopify cart
|
|
108
|
-
*/
|
|
109
|
-
var getShopifyCart = function (baseUrl, localePath) { return __awaiter(void 0, void 0, void 0, function () {
|
|
110
|
-
var url, response;
|
|
111
|
-
return __generator(this, function (_a) {
|
|
112
|
-
switch (_a.label) {
|
|
113
|
-
case 0:
|
|
114
|
-
url = constructUrl('cart.js', baseUrl, localePath);
|
|
115
|
-
return [4 /*yield*/, fetch(url)];
|
|
116
|
-
case 1:
|
|
117
|
-
response = _a.sent();
|
|
118
|
-
if (!response.ok) {
|
|
119
|
-
throw new Error("Failed to fetch cart: ".concat(response.status));
|
|
120
|
-
}
|
|
121
|
-
return [2 /*return*/, response.json()];
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}); };
|
|
125
|
-
exports.getShopifyCart = getShopifyCart;
|
|
126
|
-
/**
|
|
127
|
-
* Update Shopify cart attributes
|
|
128
|
-
*/
|
|
129
|
-
var updateShopifyCartAttributes = function (attributes, baseUrl, localePath) { return __awaiter(void 0, void 0, void 0, function () {
|
|
130
|
-
var url, response;
|
|
131
|
-
return __generator(this, function (_a) {
|
|
132
|
-
switch (_a.label) {
|
|
133
|
-
case 0:
|
|
134
|
-
url = constructUrl('cart/update.js', baseUrl, localePath);
|
|
135
|
-
return [4 /*yield*/, fetch(url, {
|
|
136
|
-
method: 'POST',
|
|
137
|
-
headers: {
|
|
138
|
-
'Content-Type': 'application/json',
|
|
139
|
-
},
|
|
140
|
-
body: JSON.stringify({ attributes: attributes }),
|
|
141
|
-
})];
|
|
142
|
-
case 1:
|
|
143
|
-
response = _a.sent();
|
|
144
|
-
if (!response.ok) {
|
|
145
|
-
throw new Error("Failed to update cart attributes: ".concat(response.status));
|
|
146
|
-
}
|
|
147
|
-
return [2 /*return*/, response.json()];
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
}); };
|
|
151
|
-
exports.updateShopifyCartAttributes = updateShopifyCartAttributes;
|
|
152
|
-
/**
|
|
153
|
-
* Sync CDP and analytics identifiers to Shopify cart attributes.
|
|
154
|
-
*
|
|
155
|
-
* By default, syncs the Chord CDP anonymous ID (`__eventn_id` cookie) and
|
|
156
|
-
* Google Analytics client ID (`_ga` cookie) to the cart. These attributes
|
|
157
|
-
* are included in Shopify order webhooks, enabling server-side events to
|
|
158
|
-
* correlate with client-side tracking sessions.
|
|
159
|
-
*
|
|
160
|
-
* Smart deduplication: only updates the cart if identifiers are missing or
|
|
161
|
-
* have changed, avoiding unnecessary API calls on repeat page loads.
|
|
162
|
-
*
|
|
163
|
-
* @param options - Optional configuration for base URL, locale path, or custom identifier getter
|
|
164
|
-
* @returns The updated cart, or undefined if no update was needed or an error occurred
|
|
165
|
-
*/
|
|
166
|
-
var syncIdentifiersToCart = function (options) {
|
|
167
|
-
if (options === void 0) { options = {}; }
|
|
168
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
169
|
-
var baseUrl, localePath, _a, identifiers, cart_1, attributesToAdd_1, identifiersAlreadyExist, updatedCart, e_1;
|
|
170
|
-
return __generator(this, function (_b) {
|
|
171
|
-
switch (_b.label) {
|
|
172
|
-
case 0:
|
|
173
|
-
_b.trys.push([0, 3, , 4]);
|
|
174
|
-
baseUrl = options.baseUrl, localePath = options.localePath, _a = options.identifiers, identifiers = _a === void 0 ? exports.getDefaultIdentifiers : _a;
|
|
175
|
-
return [4 /*yield*/, (0, exports.getShopifyCart)(baseUrl, localePath)];
|
|
176
|
-
case 1:
|
|
177
|
-
cart_1 = _b.sent();
|
|
178
|
-
attributesToAdd_1 = identifiers();
|
|
179
|
-
identifiersAlreadyExist = Object.keys(attributesToAdd_1).every(function (key) { var _a; return ((_a = cart_1 === null || cart_1 === void 0 ? void 0 : cart_1.attributes) === null || _a === void 0 ? void 0 : _a[key]) === attributesToAdd_1[key]; });
|
|
180
|
-
if (identifiersAlreadyExist) {
|
|
181
|
-
return [2 /*return*/, undefined];
|
|
182
|
-
}
|
|
183
|
-
return [4 /*yield*/, (0, exports.updateShopifyCartAttributes)(attributesToAdd_1, baseUrl, localePath)];
|
|
184
|
-
case 2:
|
|
185
|
-
updatedCart = _b.sent();
|
|
186
|
-
return [2 /*return*/, updatedCart];
|
|
187
|
-
case 3:
|
|
188
|
-
e_1 = _b.sent();
|
|
189
|
-
// eslint-disable-next-line no-console
|
|
190
|
-
console.error('[Chord Analytics]: Error syncing identifiers to cart:', e_1);
|
|
191
|
-
return [2 /*return*/, undefined];
|
|
192
|
-
case 4: return [2 /*return*/];
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
};
|
|
197
|
-
exports.syncIdentifiersToCart = syncIdentifiersToCart;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shopify Identifier Sync
|
|
3
|
-
*
|
|
4
|
-
* Syncs CDP anonymous ID and Google Client ID to Shopify cart attributes.
|
|
5
|
-
* This enables server-side events (webhooks) to correlate with client-side tracking.
|
|
6
|
-
*/
|
|
7
|
-
export interface ShopifyCart {
|
|
8
|
-
attributes?: Record<string, unknown>;
|
|
9
|
-
[key: string]: unknown;
|
|
10
|
-
}
|
|
11
|
-
export interface IdentifierSyncOptions {
|
|
12
|
-
/**
|
|
13
|
-
* Base URL for Shopify AJAX API calls. Defaults to window.location.origin
|
|
14
|
-
*/
|
|
15
|
-
baseUrl?: string;
|
|
16
|
-
/**
|
|
17
|
-
* Locale path for Shopify routes. Defaults to window.Shopify.routes.root or '/'
|
|
18
|
-
*/
|
|
19
|
-
localePath?: string;
|
|
20
|
-
/**
|
|
21
|
-
* Custom identifier getters. By default, reads CDP anonymous ID and Google Client ID.
|
|
22
|
-
*/
|
|
23
|
-
identifiers?: () => Record<string, string>;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Get a cookie value by name
|
|
27
|
-
*/
|
|
28
|
-
export declare const getCookie: (name: string) => string | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* Get default identifiers (CDP anonymous ID and Google Client ID)
|
|
31
|
-
*/
|
|
32
|
-
export declare const getDefaultIdentifiers: () => Record<string, string>;
|
|
33
|
-
/**
|
|
34
|
-
* Fetch the current Shopify cart
|
|
35
|
-
*/
|
|
36
|
-
export declare const getShopifyCart: (baseUrl?: string, localePath?: string) => Promise<ShopifyCart>;
|
|
37
|
-
/**
|
|
38
|
-
* Update Shopify cart attributes
|
|
39
|
-
*/
|
|
40
|
-
export declare const updateShopifyCartAttributes: (attributes: Record<string, string>, baseUrl?: string, localePath?: string) => Promise<ShopifyCart>;
|
|
41
|
-
/**
|
|
42
|
-
* Sync CDP and analytics identifiers to Shopify cart attributes.
|
|
43
|
-
*
|
|
44
|
-
* By default, syncs the Chord CDP anonymous ID (`__eventn_id` cookie) and
|
|
45
|
-
* Google Analytics client ID (`_ga` cookie) to the cart. These attributes
|
|
46
|
-
* are included in Shopify order webhooks, enabling server-side events to
|
|
47
|
-
* correlate with client-side tracking sessions.
|
|
48
|
-
*
|
|
49
|
-
* Smart deduplication: only updates the cart if identifiers are missing or
|
|
50
|
-
* have changed, avoiding unnecessary API calls on repeat page loads.
|
|
51
|
-
*
|
|
52
|
-
* @param options - Optional configuration for base URL, locale path, or custom identifier getter
|
|
53
|
-
* @returns The updated cart, or undefined if no update was needed or an error occurred
|
|
54
|
-
*/
|
|
55
|
-
export declare const syncIdentifiersToCart: (options?: IdentifierSyncOptions) => Promise<ShopifyCart | undefined>;
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shopify Identifier Sync
|
|
3
|
-
*
|
|
4
|
-
* Syncs CDP anonymous ID and Google Client ID to Shopify cart attributes.
|
|
5
|
-
* This enables server-side events (webhooks) to correlate with client-side tracking.
|
|
6
|
-
*/
|
|
7
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Get a cookie value by name
|
|
18
|
-
*/
|
|
19
|
-
export const getCookie = (name) => {
|
|
20
|
-
var _a;
|
|
21
|
-
if (typeof document === 'undefined')
|
|
22
|
-
return undefined;
|
|
23
|
-
return (_a = document.cookie
|
|
24
|
-
.split('; ')
|
|
25
|
-
.find((row) => row.startsWith(`${name}=`))) === null || _a === void 0 ? void 0 : _a.split('=')[1];
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Get default identifiers (CDP anonymous ID and Google Client ID)
|
|
29
|
-
*/
|
|
30
|
-
export const getDefaultIdentifiers = () => {
|
|
31
|
-
const attributes = {};
|
|
32
|
-
try {
|
|
33
|
-
// CDP anonymous ID (Jitsu/Chord CDP)
|
|
34
|
-
const anonymousId = getCookie('__eventn_id');
|
|
35
|
-
if (anonymousId) {
|
|
36
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
37
|
-
attributes._cdpAnonymousId = anonymousId;
|
|
38
|
-
}
|
|
39
|
-
// Google Client ID (strip the "GA1.1." or similar prefix)
|
|
40
|
-
const gaCookie = getCookie('_ga');
|
|
41
|
-
if (gaCookie) {
|
|
42
|
-
const googleClientId = gaCookie.substring(6);
|
|
43
|
-
if (googleClientId) {
|
|
44
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
45
|
-
attributes._googleClientId = googleClientId;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
catch (e) {
|
|
50
|
-
// eslint-disable-next-line no-console
|
|
51
|
-
console.error('[Chord Analytics]: Error getting identifiers:', e);
|
|
52
|
-
}
|
|
53
|
-
return attributes;
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Construct a URL for Shopify AJAX API calls
|
|
57
|
-
*/
|
|
58
|
-
const constructUrl = (path, baseUrl, localePath) => {
|
|
59
|
-
var _a, _b;
|
|
60
|
-
const defaultBaseUrl = typeof window !== 'undefined' ? (_a = window.location) === null || _a === void 0 ? void 0 : _a.origin : undefined;
|
|
61
|
-
// Access Shopify.routes.root for locale-aware URLs (cast needed as Window.Shopify type is partial)
|
|
62
|
-
const shopify = window === null || window === void 0 ? void 0 : window.Shopify;
|
|
63
|
-
const defaultLocalePath = ((_b = shopify === null || shopify === void 0 ? void 0 : shopify.routes) === null || _b === void 0 ? void 0 : _b.root) || '/';
|
|
64
|
-
const resolvedBaseUrl = baseUrl !== null && baseUrl !== void 0 ? baseUrl : defaultBaseUrl;
|
|
65
|
-
const resolvedLocalePath = localePath !== null && localePath !== void 0 ? localePath : defaultLocalePath;
|
|
66
|
-
const pathWithLocale = `${resolvedLocalePath}${path}`;
|
|
67
|
-
if (!resolvedBaseUrl) {
|
|
68
|
-
return pathWithLocale.startsWith('/')
|
|
69
|
-
? pathWithLocale
|
|
70
|
-
: `/${pathWithLocale}`;
|
|
71
|
-
}
|
|
72
|
-
return new URL(pathWithLocale, resolvedBaseUrl);
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* Fetch the current Shopify cart
|
|
76
|
-
*/
|
|
77
|
-
export const getShopifyCart = (baseUrl, localePath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
78
|
-
const url = constructUrl('cart.js', baseUrl, localePath);
|
|
79
|
-
const response = yield fetch(url);
|
|
80
|
-
if (!response.ok) {
|
|
81
|
-
throw new Error(`Failed to fetch cart: ${response.status}`);
|
|
82
|
-
}
|
|
83
|
-
return response.json();
|
|
84
|
-
});
|
|
85
|
-
/**
|
|
86
|
-
* Update Shopify cart attributes
|
|
87
|
-
*/
|
|
88
|
-
export const updateShopifyCartAttributes = (attributes, baseUrl, localePath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
-
const url = constructUrl('cart/update.js', baseUrl, localePath);
|
|
90
|
-
const response = yield fetch(url, {
|
|
91
|
-
method: 'POST',
|
|
92
|
-
headers: {
|
|
93
|
-
'Content-Type': 'application/json',
|
|
94
|
-
},
|
|
95
|
-
body: JSON.stringify({ attributes }),
|
|
96
|
-
});
|
|
97
|
-
if (!response.ok) {
|
|
98
|
-
throw new Error(`Failed to update cart attributes: ${response.status}`);
|
|
99
|
-
}
|
|
100
|
-
return response.json();
|
|
101
|
-
});
|
|
102
|
-
/**
|
|
103
|
-
* Sync CDP and analytics identifiers to Shopify cart attributes.
|
|
104
|
-
*
|
|
105
|
-
* By default, syncs the Chord CDP anonymous ID (`__eventn_id` cookie) and
|
|
106
|
-
* Google Analytics client ID (`_ga` cookie) to the cart. These attributes
|
|
107
|
-
* are included in Shopify order webhooks, enabling server-side events to
|
|
108
|
-
* correlate with client-side tracking sessions.
|
|
109
|
-
*
|
|
110
|
-
* Smart deduplication: only updates the cart if identifiers are missing or
|
|
111
|
-
* have changed, avoiding unnecessary API calls on repeat page loads.
|
|
112
|
-
*
|
|
113
|
-
* @param options - Optional configuration for base URL, locale path, or custom identifier getter
|
|
114
|
-
* @returns The updated cart, or undefined if no update was needed or an error occurred
|
|
115
|
-
*/
|
|
116
|
-
export const syncIdentifiersToCart = (options = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
|
-
try {
|
|
118
|
-
const { baseUrl, localePath, identifiers = getDefaultIdentifiers } = options;
|
|
119
|
-
const cart = yield getShopifyCart(baseUrl, localePath);
|
|
120
|
-
const attributesToAdd = identifiers();
|
|
121
|
-
// Check if all identifiers already exist with correct values
|
|
122
|
-
const identifiersAlreadyExist = Object.keys(attributesToAdd).every((key) => { var _a; return ((_a = cart === null || cart === void 0 ? void 0 : cart.attributes) === null || _a === void 0 ? void 0 : _a[key]) === attributesToAdd[key]; });
|
|
123
|
-
if (identifiersAlreadyExist) {
|
|
124
|
-
return undefined;
|
|
125
|
-
}
|
|
126
|
-
// Update cart with new identifiers
|
|
127
|
-
const updatedCart = yield updateShopifyCartAttributes(attributesToAdd, baseUrl, localePath);
|
|
128
|
-
return updatedCart;
|
|
129
|
-
}
|
|
130
|
-
catch (e) {
|
|
131
|
-
// eslint-disable-next-line no-console
|
|
132
|
-
console.error('[Chord Analytics]: Error syncing identifiers to cart:', e);
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
});
|