@fire-spark/analytics 0.21.1 → 0.23.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.
- package/dist/index.d.ts +453 -8
- package/dist/index.js +2939 -1375
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -1,1010 +1,1122 @@
|
|
|
1
1
|
import { Big } from "big.js";
|
|
2
2
|
import { StorefrontHttpError, logError, storefrontJson } from "@fire-spark/core";
|
|
3
|
-
//#region src/
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const options = Intl.DateTimeFormat().resolvedOptions();
|
|
9
|
-
return {
|
|
10
|
-
locale: options.locale,
|
|
11
|
-
timezone: options.timeZone
|
|
12
|
-
};
|
|
13
|
-
} catch {
|
|
14
|
-
return {};
|
|
15
|
-
}
|
|
3
|
+
//#region ../../packages/constants/src/tracking-plan-catalog/currency.ts
|
|
4
|
+
const ISO_4217_CODE = /^[A-Z]{3}$/;
|
|
5
|
+
const USD_AMOUNT_PARAM_SUFFIX = "_in_usd";
|
|
6
|
+
const isUsdAmountParam = (name) => {
|
|
7
|
+
return name.endsWith(USD_AMOUNT_PARAM_SUFFIX);
|
|
16
8
|
};
|
|
17
|
-
const
|
|
9
|
+
const dropUsdDeep = (value) => {
|
|
10
|
+
if (Array.isArray(value)) return value.map(dropUsdDeep);
|
|
11
|
+
if (!value || typeof value !== "object") return value;
|
|
12
|
+
if (value instanceof Date) return value;
|
|
18
13
|
const next = {};
|
|
19
|
-
for (const [key,
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
if (value) next[key] = value;
|
|
14
|
+
for (const [key, nested] of Object.entries(value)) {
|
|
15
|
+
if (isUsdAmountParam(key)) continue;
|
|
16
|
+
next[key] = dropUsdDeep(nested);
|
|
23
17
|
}
|
|
24
18
|
return next;
|
|
25
19
|
};
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
const dropUsdAmountParams = (params) => {
|
|
21
|
+
return dropUsdDeep(params);
|
|
22
|
+
};
|
|
23
|
+
/** Upper-case ISO 4217 code, or `undefined` when `code` is not one. */
|
|
24
|
+
const isoCurrencyCode = (code) => {
|
|
25
|
+
if (typeof code !== "string") return void 0;
|
|
26
|
+
const normalized = code.trim().toUpperCase();
|
|
27
|
+
if (!ISO_4217_CODE.test(normalized)) return void 0;
|
|
28
|
+
return normalized;
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region ../../packages/constants/src/tracking-plan-catalog/params.ts
|
|
32
|
+
const valueParam = (required = true) => {
|
|
31
33
|
return {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
name: "value",
|
|
35
|
+
type: "currency",
|
|
36
|
+
required,
|
|
37
|
+
scope: "ecommerce"
|
|
34
38
|
};
|
|
35
39
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
detail: issue.event
|
|
44
|
-
};
|
|
45
|
-
const logged = logError(issue.error ?? issue.message, opts);
|
|
46
|
-
Promise.resolve(logged).catch((failure) => {
|
|
47
|
-
console.error(`${PREFIX} Masivo error was not logged`, failure);
|
|
48
|
-
});
|
|
49
|
-
} catch (failure) {
|
|
50
|
-
console.error(`${PREFIX} Masivo error was not logged`, failure);
|
|
51
|
-
}
|
|
40
|
+
const priceParam = (required = false) => {
|
|
41
|
+
return {
|
|
42
|
+
name: "price",
|
|
43
|
+
type: "currency",
|
|
44
|
+
required,
|
|
45
|
+
scope: "item"
|
|
46
|
+
};
|
|
52
47
|
};
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
const quantityParam = (required = false) => {
|
|
49
|
+
return {
|
|
50
|
+
name: "quantity",
|
|
51
|
+
type: "integer",
|
|
52
|
+
required,
|
|
53
|
+
scope: "item"
|
|
54
|
+
};
|
|
59
55
|
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
callHook(hook, issue);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const detail = {
|
|
70
|
-
provider: issue.provider,
|
|
71
|
-
event: issue.event
|
|
56
|
+
const itemsParam = () => {
|
|
57
|
+
return {
|
|
58
|
+
name: "items",
|
|
59
|
+
type: "array<object>",
|
|
60
|
+
required: false,
|
|
61
|
+
scope: "ecommerce"
|
|
72
62
|
};
|
|
73
|
-
const args = [
|
|
74
|
-
`${PREFIX} ${issue.message}`,
|
|
75
|
-
detail,
|
|
76
|
-
issue.error
|
|
77
|
-
];
|
|
78
|
-
if (error) console.error(...args);
|
|
79
|
-
else console.warn(...args);
|
|
80
63
|
};
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
64
|
+
const transactionIdParam = () => {
|
|
65
|
+
return {
|
|
66
|
+
name: "transaction_id",
|
|
67
|
+
type: "string",
|
|
68
|
+
required: false,
|
|
69
|
+
scope: "event"
|
|
70
|
+
};
|
|
84
71
|
};
|
|
85
72
|
//#endregion
|
|
86
|
-
//#region src/
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
];
|
|
93
|
-
const INTERACTION_EVENTS = ["product_visited", "category_visited"];
|
|
94
|
-
const BUILTIN_DESTINATION_IDS = [
|
|
95
|
-
"ga4",
|
|
96
|
-
"meta",
|
|
97
|
-
"masivo",
|
|
98
|
-
"tiktok"
|
|
99
|
-
];
|
|
100
|
-
const isFunnelEventName = (name) => {
|
|
101
|
-
return FUNNEL_EVENTS.includes(name);
|
|
102
|
-
};
|
|
103
|
-
const isInteractionEventName = (name) => {
|
|
104
|
-
return INTERACTION_EVENTS.includes(name);
|
|
73
|
+
//#region ../../packages/constants/src/tracking-plan-catalog/events.ts
|
|
74
|
+
const both = (name) => {
|
|
75
|
+
return {
|
|
76
|
+
web: name,
|
|
77
|
+
reactNative: name
|
|
78
|
+
};
|
|
105
79
|
};
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
80
|
+
const none = () => {
|
|
81
|
+
return {
|
|
82
|
+
web: null,
|
|
83
|
+
reactNative: null
|
|
84
|
+
};
|
|
111
85
|
};
|
|
112
|
-
const
|
|
113
|
-
return
|
|
86
|
+
const meta = (web, reactNative) => {
|
|
87
|
+
return {
|
|
88
|
+
web,
|
|
89
|
+
reactNative
|
|
90
|
+
};
|
|
114
91
|
};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (typeof amount === "number") {
|
|
121
|
-
if (!Number.isFinite(amount)) return void 0;
|
|
122
|
-
return amount;
|
|
123
|
-
}
|
|
124
|
-
if (typeof amount !== "string") return void 0;
|
|
125
|
-
const trimmed = amount.trim();
|
|
126
|
-
if (!trimmed) return void 0;
|
|
127
|
-
try {
|
|
128
|
-
new Big(trimmed);
|
|
129
|
-
} catch {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
return trimmed;
|
|
92
|
+
const webOnly = (name) => {
|
|
93
|
+
return {
|
|
94
|
+
web: name,
|
|
95
|
+
reactNative: null
|
|
96
|
+
};
|
|
133
97
|
};
|
|
134
|
-
|
|
135
|
-
const forwardRevenue = (revenue) => {
|
|
136
|
-
if (!revenue) return void 0;
|
|
137
|
-
if (typeof revenue.currency !== "string") return void 0;
|
|
138
|
-
const currency = revenue.currency.trim();
|
|
139
|
-
if (!currency) return void 0;
|
|
140
|
-
const amount = decimalAmount(revenue.amount);
|
|
141
|
-
if (amount === void 0) return void 0;
|
|
98
|
+
const nativeOnly = (name) => {
|
|
142
99
|
return {
|
|
143
|
-
|
|
144
|
-
|
|
100
|
+
web: null,
|
|
101
|
+
reactNative: name
|
|
145
102
|
};
|
|
146
103
|
};
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
104
|
+
const ecommerceParams = (valueRequired = true) => {
|
|
105
|
+
return [
|
|
106
|
+
valueParam(valueRequired),
|
|
107
|
+
itemsParam(),
|
|
108
|
+
transactionIdParam()
|
|
109
|
+
];
|
|
154
110
|
};
|
|
155
111
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* including two adapters that share a built-in id.
|
|
112
|
+
* Native events only. Custom events are merchant-authored and never shipped
|
|
113
|
+
* as catalog suggestions or plan defaults.
|
|
159
114
|
*/
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
115
|
+
const CATALOG_EVENTS = [
|
|
116
|
+
{
|
|
117
|
+
id: "add_to_cart",
|
|
118
|
+
name: "add_to_cart",
|
|
119
|
+
kind: "native",
|
|
120
|
+
category: "E-commerce",
|
|
121
|
+
runtimes: ["web", "react-native"],
|
|
122
|
+
recommendedParams: [
|
|
123
|
+
{
|
|
124
|
+
name: "product_id",
|
|
125
|
+
type: "string",
|
|
126
|
+
required: true,
|
|
127
|
+
scope: "event"
|
|
128
|
+
},
|
|
129
|
+
quantityParam(true),
|
|
130
|
+
...ecommerceParams(false)
|
|
131
|
+
],
|
|
132
|
+
destinations: {
|
|
133
|
+
"fire-spark": both("add_to_cart"),
|
|
134
|
+
ga4: both("add_to_cart"),
|
|
135
|
+
meta: meta("AddToCart", "fb_mobile_add_to_cart"),
|
|
136
|
+
tiktok: both("AddToCart"),
|
|
137
|
+
masivo: both("ADD_TO_CART")
|
|
174
138
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
id: "begin_checkout",
|
|
142
|
+
name: "begin_checkout",
|
|
143
|
+
kind: "native",
|
|
144
|
+
category: "E-commerce",
|
|
145
|
+
runtimes: ["web", "react-native"],
|
|
146
|
+
recommendedParams: ecommerceParams(),
|
|
147
|
+
destinations: {
|
|
148
|
+
"fire-spark": both("begin_checkout"),
|
|
149
|
+
ga4: both("begin_checkout"),
|
|
150
|
+
meta: meta("InitiateCheckout", "fb_mobile_initiated_checkout"),
|
|
151
|
+
tiktok: both("InitiateCheckout"),
|
|
152
|
+
masivo: both("begin_checkout")
|
|
179
153
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
id: "purchase",
|
|
157
|
+
name: "purchase",
|
|
158
|
+
kind: "native",
|
|
159
|
+
category: "E-commerce",
|
|
160
|
+
runtimes: ["web", "react-native"],
|
|
161
|
+
recommendedParams: [{
|
|
162
|
+
name: "order_id",
|
|
163
|
+
type: "string",
|
|
164
|
+
required: true,
|
|
165
|
+
scope: "event"
|
|
166
|
+
}, ...ecommerceParams(false)],
|
|
167
|
+
destinations: {
|
|
168
|
+
"fire-spark": both("purchase"),
|
|
169
|
+
ga4: both("purchase"),
|
|
170
|
+
meta: meta("Purchase", "fb_mobile_purchase"),
|
|
171
|
+
tiktok: both("Purchase"),
|
|
172
|
+
masivo: both("PURCHASE")
|
|
185
173
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
id: "view_item",
|
|
177
|
+
name: "view_item",
|
|
178
|
+
kind: "native",
|
|
179
|
+
category: "E-commerce",
|
|
180
|
+
runtimes: ["web", "react-native"],
|
|
181
|
+
recommendedParams: [
|
|
182
|
+
valueParam(false),
|
|
183
|
+
itemsParam(),
|
|
184
|
+
priceParam(),
|
|
185
|
+
quantityParam()
|
|
186
|
+
],
|
|
187
|
+
destinations: {
|
|
188
|
+
"fire-spark": both("product_viewed"),
|
|
189
|
+
ga4: both("view_item"),
|
|
190
|
+
meta: meta("ViewContent", "fb_mobile_content_view"),
|
|
191
|
+
tiktok: both("ViewContent"),
|
|
192
|
+
masivo: both("view_item")
|
|
190
193
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
id: "sign_up",
|
|
197
|
+
name: "sign_up",
|
|
198
|
+
kind: "native",
|
|
199
|
+
category: "Account",
|
|
200
|
+
runtimes: ["web", "react-native"],
|
|
201
|
+
recommendedParams: [{
|
|
202
|
+
name: "method",
|
|
203
|
+
type: "string",
|
|
204
|
+
required: false,
|
|
205
|
+
scope: "event"
|
|
206
|
+
}],
|
|
207
|
+
destinations: {
|
|
208
|
+
"fire-spark": both("sign_up"),
|
|
209
|
+
ga4: both("sign_up"),
|
|
210
|
+
meta: meta("CompleteRegistration", "fb_mobile_complete_registration"),
|
|
211
|
+
tiktok: both("Registration"),
|
|
212
|
+
masivo: both("sign_up")
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
id: "login",
|
|
217
|
+
name: "login",
|
|
218
|
+
kind: "native",
|
|
219
|
+
category: "Account",
|
|
220
|
+
runtimes: ["web", "react-native"],
|
|
221
|
+
recommendedParams: [{
|
|
222
|
+
name: "method",
|
|
223
|
+
type: "string",
|
|
224
|
+
required: false,
|
|
225
|
+
scope: "event"
|
|
226
|
+
}],
|
|
227
|
+
destinations: {
|
|
228
|
+
"fire-spark": both("login"),
|
|
229
|
+
ga4: both("login"),
|
|
230
|
+
meta: both("login"),
|
|
231
|
+
tiktok: both("Login"),
|
|
232
|
+
masivo: both("login")
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
id: "page_view",
|
|
237
|
+
name: "page_view",
|
|
238
|
+
kind: "native",
|
|
239
|
+
category: "Navegación",
|
|
240
|
+
runtimes: ["web"],
|
|
241
|
+
recommendedParams: [],
|
|
242
|
+
destinations: {
|
|
243
|
+
"fire-spark": webOnly("page_view"),
|
|
244
|
+
ga4: webOnly("page_view"),
|
|
245
|
+
meta: none(),
|
|
246
|
+
tiktok: none(),
|
|
247
|
+
masivo: none()
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
id: "screen_view",
|
|
252
|
+
name: "screen_view",
|
|
253
|
+
kind: "native",
|
|
254
|
+
category: "Navegación",
|
|
255
|
+
runtimes: ["react-native"],
|
|
256
|
+
recommendedParams: [],
|
|
257
|
+
destinations: {
|
|
258
|
+
"fire-spark": nativeOnly("screen_view"),
|
|
259
|
+
ga4: nativeOnly("screen_view"),
|
|
260
|
+
meta: none(),
|
|
261
|
+
tiktok: none(),
|
|
262
|
+
masivo: none()
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
id: "menu_viewed",
|
|
267
|
+
name: "menu_viewed",
|
|
268
|
+
kind: "native",
|
|
269
|
+
category: "Navegación",
|
|
270
|
+
runtimes: ["web", "react-native"],
|
|
271
|
+
recommendedParams: [],
|
|
272
|
+
destinations: {
|
|
273
|
+
"fire-spark": both("menu_viewed"),
|
|
274
|
+
ga4: both("menu_viewed"),
|
|
275
|
+
meta: both("menu_viewed"),
|
|
276
|
+
tiktok: both("menu_viewed"),
|
|
277
|
+
masivo: both("menu_viewed")
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
id: "product_viewed",
|
|
282
|
+
name: "product_viewed",
|
|
283
|
+
kind: "native",
|
|
284
|
+
category: "E-commerce",
|
|
285
|
+
runtimes: ["web", "react-native"],
|
|
286
|
+
recommendedParams: [{
|
|
287
|
+
name: "product_id",
|
|
288
|
+
type: "string",
|
|
289
|
+
required: true,
|
|
290
|
+
scope: "event"
|
|
291
|
+
}],
|
|
292
|
+
destinations: {
|
|
293
|
+
"fire-spark": both("product_viewed"),
|
|
294
|
+
ga4: both("view_item"),
|
|
295
|
+
meta: meta("ViewContent", "fb_mobile_content_view"),
|
|
296
|
+
tiktok: both("ViewContent"),
|
|
297
|
+
masivo: both("view_item")
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
id: "product_visited",
|
|
302
|
+
name: "product_visited",
|
|
303
|
+
kind: "native",
|
|
304
|
+
category: "Navegación",
|
|
305
|
+
runtimes: ["web", "react-native"],
|
|
306
|
+
recommendedParams: [
|
|
307
|
+
{
|
|
308
|
+
name: "product_id",
|
|
309
|
+
type: "string",
|
|
310
|
+
required: true,
|
|
311
|
+
scope: "event"
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
name: "category_id",
|
|
315
|
+
type: "string",
|
|
316
|
+
required: false,
|
|
317
|
+
scope: "event"
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
name: "attribution_source",
|
|
321
|
+
type: "string",
|
|
322
|
+
required: false,
|
|
323
|
+
scope: "event"
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
name: "attribution_id",
|
|
327
|
+
type: "string",
|
|
328
|
+
required: false,
|
|
329
|
+
scope: "event"
|
|
330
|
+
}
|
|
331
|
+
],
|
|
332
|
+
destinations: {
|
|
333
|
+
"fire-spark": both("product_visited"),
|
|
334
|
+
ga4: both("product_visited"),
|
|
335
|
+
meta: both("product_visited"),
|
|
336
|
+
tiktok: both("product_visited"),
|
|
337
|
+
masivo: both("product_visited")
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
id: "category_visited",
|
|
342
|
+
name: "category_visited",
|
|
343
|
+
kind: "native",
|
|
344
|
+
category: "Navegación",
|
|
345
|
+
runtimes: ["web", "react-native"],
|
|
346
|
+
recommendedParams: [{
|
|
347
|
+
name: "category_id",
|
|
348
|
+
type: "string",
|
|
349
|
+
required: true,
|
|
350
|
+
scope: "event"
|
|
351
|
+
}],
|
|
352
|
+
destinations: {
|
|
353
|
+
"fire-spark": both("category_visited"),
|
|
354
|
+
ga4: both("category_visited"),
|
|
355
|
+
meta: both("category_visited"),
|
|
356
|
+
tiktok: both("category_visited"),
|
|
357
|
+
masivo: both("category_visited")
|
|
195
358
|
}
|
|
196
|
-
accepted.push({
|
|
197
|
-
...adapter,
|
|
198
|
-
provider: id
|
|
199
|
-
});
|
|
200
359
|
}
|
|
201
|
-
|
|
360
|
+
];
|
|
361
|
+
const eventsByName = new Map(CATALOG_EVENTS.map((event) => [event.name, event]));
|
|
362
|
+
const catalogEventByName = (name) => {
|
|
363
|
+
return eventsByName.get(name);
|
|
364
|
+
};
|
|
365
|
+
/** Catalog ecommerce events carry `value`, `currency` and `items`. */
|
|
366
|
+
const isCatalogEcommerceEvent = (name) => {
|
|
367
|
+
return catalogEventByName(name)?.category === "E-commerce";
|
|
202
368
|
};
|
|
203
369
|
/**
|
|
204
|
-
* Destination
|
|
205
|
-
*
|
|
206
|
-
* `onWarning` (`source: "configure"`, or `"track"` for `extra`) and that
|
|
207
|
-
* adapter is skipped. `consent` selects the gate. `"ads"` hashes
|
|
208
|
-
* `email`, `phone`, `device_ip`, `ttclid` and the user id the same way
|
|
209
|
-
* Meta and TikTok do. `map` receives the raw tracked event (no catalog
|
|
210
|
-
* rename) and returns the payload `send` delivers, or `null` to drop it.
|
|
211
|
-
* Without `map`, `send` receives the tracked name and properties.
|
|
212
|
-
* `revenue.amount` is forwarded unchanged.
|
|
213
|
-
*
|
|
214
|
-
* The app supplies the native SDK. This package does not include one.
|
|
215
|
-
* Installs, ATT prompts and deep links stay in that SDK and its Expo
|
|
216
|
-
* config plugins.
|
|
217
|
-
*
|
|
218
|
-
* @example Singular-style mapping. The SDK below is the app's own; nothing
|
|
219
|
-
* here imports Singular.
|
|
220
|
-
* ```ts
|
|
221
|
-
* import { customAdapter } from "@fire-spark/analytics";
|
|
222
|
-
* import type { CustomDestinationEvent } from "@fire-spark/analytics";
|
|
223
|
-
* import type { CustomDestinationInput } from "@fire-spark/analytics";
|
|
224
|
-
*
|
|
225
|
-
* type RevenueSdk = {
|
|
226
|
-
* eventWithArgs: (name: string, args: Record<string, unknown>) => void;
|
|
227
|
-
* customRevenueWithArgs: (
|
|
228
|
-
* name: string,
|
|
229
|
-
* currency: string,
|
|
230
|
-
* amount: string | number,
|
|
231
|
-
* args: Record<string, unknown>
|
|
232
|
-
* ) => void;
|
|
233
|
-
* };
|
|
234
|
-
*
|
|
235
|
-
* const SINGULAR_EVENTS: Record<string, string> = {
|
|
236
|
-
* sign_up: "sng_complete_registration",
|
|
237
|
-
* search: "sng_search",
|
|
238
|
-
* product_viewed: "sng_content_view",
|
|
239
|
-
* add_to_cart: "sng_add_to_cart",
|
|
240
|
-
* view_cart: "sng_view_cart",
|
|
241
|
-
* begin_checkout: "sng_checkout_initiated",
|
|
242
|
-
* add_payment_info: "sng_add_payment_info",
|
|
243
|
-
* subscribe: "sng_subscribe"
|
|
244
|
-
* };
|
|
245
|
-
*
|
|
246
|
-
* const singularAdapter = (sdk: RevenueSdk) => {
|
|
247
|
-
* const map = (event: CustomDestinationInput) => {
|
|
248
|
-
* if (event.name === "purchase") {
|
|
249
|
-
* const total = event.properties.total ?? event.properties.value;
|
|
250
|
-
* if (typeof total !== "string" && typeof total !== "number") {
|
|
251
|
-
* return null;
|
|
252
|
-
* }
|
|
253
|
-
* if (!event.currency) return null;
|
|
254
|
-
* const first = event.properties.first_purchase === true;
|
|
255
|
-
* const repeatName = "sng_ecommerce_purchase";
|
|
256
|
-
* const firstName = "sng_ecommerce_firstpurchase";
|
|
257
|
-
* const name = first ? firstName : repeatName;
|
|
258
|
-
* const params = { order_id: event.properties.order_id };
|
|
259
|
-
* const revenue = { currency: event.currency, amount: total };
|
|
260
|
-
* return { name, params, revenue };
|
|
261
|
-
* }
|
|
262
|
-
* const name = SINGULAR_EVENTS[event.name];
|
|
263
|
-
* if (!name) return null;
|
|
264
|
-
* return { name, params: event.properties };
|
|
265
|
-
* };
|
|
266
|
-
* const send = (event: CustomDestinationEvent) => {
|
|
267
|
-
* const revenue = event.revenue;
|
|
268
|
-
* if (!revenue) {
|
|
269
|
-
* sdk.eventWithArgs(event.name, event.params);
|
|
270
|
-
* return;
|
|
271
|
-
* }
|
|
272
|
-
* sdk.customRevenueWithArgs(
|
|
273
|
-
* event.name,
|
|
274
|
-
* revenue.currency,
|
|
275
|
-
* revenue.amount,
|
|
276
|
-
* event.params
|
|
277
|
-
* );
|
|
278
|
-
* };
|
|
279
|
-
* return customAdapter({ id: "singular", consent: "ads", map, send });
|
|
280
|
-
* };
|
|
281
|
-
* ```
|
|
370
|
+
* Destination name for one runtime, or `null` when the event is not sent
|
|
371
|
+
* there: a `null` binding, or a runtime the catalog does not declare.
|
|
282
372
|
*/
|
|
283
|
-
const
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
};
|
|
292
|
-
const b3 = {
|
|
293
|
-
send: options.send,
|
|
294
|
-
setUser: options.setUser
|
|
295
|
-
};
|
|
296
|
-
const b4 = { setConsent: options.setConsent };
|
|
297
|
-
return {
|
|
298
|
-
...b1,
|
|
299
|
-
...b2,
|
|
300
|
-
...b3,
|
|
301
|
-
...b4
|
|
302
|
-
};
|
|
373
|
+
const destinationEventName = (developerName, provider, runtime) => {
|
|
374
|
+
const event = catalogEventByName(developerName);
|
|
375
|
+
if (!event) return developerName;
|
|
376
|
+
if (!event.runtimes.includes(runtime)) return null;
|
|
377
|
+
const binding = event.destinations[provider];
|
|
378
|
+
if (!binding) return developerName;
|
|
379
|
+
if (runtime === "react-native") return binding.reactNative;
|
|
380
|
+
return binding.web;
|
|
303
381
|
};
|
|
304
382
|
//#endregion
|
|
305
|
-
//#region src/
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
3835390401,
|
|
324
|
-
4022224774,
|
|
325
|
-
264347078,
|
|
326
|
-
604807628,
|
|
327
|
-
770255983,
|
|
328
|
-
1249150122,
|
|
329
|
-
1555081692,
|
|
330
|
-
1996064986,
|
|
331
|
-
2554220882,
|
|
332
|
-
2821834349,
|
|
333
|
-
2952996808,
|
|
334
|
-
3210313671,
|
|
335
|
-
3336571891,
|
|
336
|
-
3584528711,
|
|
337
|
-
113926993,
|
|
338
|
-
338241895,
|
|
339
|
-
666307205,
|
|
340
|
-
773529912,
|
|
341
|
-
1294757372,
|
|
342
|
-
1396182291,
|
|
343
|
-
1695183700,
|
|
344
|
-
1986661051,
|
|
345
|
-
2177026350,
|
|
346
|
-
2456956037,
|
|
347
|
-
2730485921,
|
|
348
|
-
2820302411,
|
|
349
|
-
3259730800,
|
|
350
|
-
3345764771,
|
|
351
|
-
3516065817,
|
|
352
|
-
3600352804,
|
|
353
|
-
4094571909,
|
|
354
|
-
275423344,
|
|
355
|
-
430227734,
|
|
356
|
-
506948616,
|
|
357
|
-
659060556,
|
|
358
|
-
883997877,
|
|
359
|
-
958139571,
|
|
360
|
-
1322822218,
|
|
361
|
-
1537002063,
|
|
362
|
-
1747873779,
|
|
363
|
-
1955562222,
|
|
364
|
-
2024104815,
|
|
365
|
-
2227730452,
|
|
366
|
-
2361852424,
|
|
367
|
-
2428436474,
|
|
368
|
-
2756734187,
|
|
369
|
-
3204031479,
|
|
370
|
-
3329325298
|
|
383
|
+
//#region ../../packages/constants/src/tracking-plan-catalog/masivo.ts
|
|
384
|
+
const MASIVO_ATTRIBUTION_FIELDS = [
|
|
385
|
+
"tracking_id",
|
|
386
|
+
"utm_source",
|
|
387
|
+
"utm_medium",
|
|
388
|
+
"utm_campaign",
|
|
389
|
+
"utm_term",
|
|
390
|
+
"utm_content",
|
|
391
|
+
"utm_referral",
|
|
392
|
+
"gclid",
|
|
393
|
+
"gbraid",
|
|
394
|
+
"wbraid",
|
|
395
|
+
"fbclid",
|
|
396
|
+
"ttclid",
|
|
397
|
+
"fbc",
|
|
398
|
+
"fbp",
|
|
399
|
+
"ttp",
|
|
400
|
+
"event_source_url"
|
|
371
401
|
];
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
2600822924,
|
|
379
|
-
528734635,
|
|
380
|
-
1541459225
|
|
402
|
+
const MASIVO_PAYMENT_METHODS = [
|
|
403
|
+
"CREDIT",
|
|
404
|
+
"DEBIT",
|
|
405
|
+
"CASH",
|
|
406
|
+
"BANK_TRANSFER",
|
|
407
|
+
"OTHER"
|
|
381
408
|
];
|
|
382
|
-
const
|
|
383
|
-
|
|
409
|
+
const PAYMENT_ALIASES = {
|
|
410
|
+
CASH: "CASH",
|
|
411
|
+
BANK_TRANSFER: "BANK_TRANSFER",
|
|
412
|
+
CARD: "OTHER",
|
|
413
|
+
PIN_PAD: "OTHER",
|
|
414
|
+
WALLET: "OTHER",
|
|
415
|
+
FINANCING: "OTHER"
|
|
384
416
|
};
|
|
385
|
-
/**
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const b1 = 224 | code >> 12;
|
|
394
|
-
bytes.push(b1, 128 | code >> 6 & 63, 128 | code & 63);
|
|
395
|
-
} else {
|
|
396
|
-
bytes.push(240 | code >> 18, 128 | code >> 12 & 63);
|
|
397
|
-
bytes.push(128 | code >> 6 & 63, 128 | code & 63);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
return Uint8Array.from(bytes);
|
|
417
|
+
/** Spark tender method (or a Masivo value) → Masivo `payment_method`. */
|
|
418
|
+
const masivoPaymentMethod = (method) => {
|
|
419
|
+
if (typeof method !== "string") return "OTHER";
|
|
420
|
+
const upper = method.trim().toUpperCase();
|
|
421
|
+
const alias = PAYMENT_ALIASES[upper];
|
|
422
|
+
if (alias) return alias;
|
|
423
|
+
if (MASIVO_PAYMENT_METHODS.includes(upper)) return upper;
|
|
424
|
+
return "OTHER";
|
|
401
425
|
};
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
426
|
+
new Set(MASIVO_ATTRIBUTION_FIELDS);
|
|
427
|
+
/** Masivo `type` that carries an order or a single product. */
|
|
428
|
+
const MASIVO_ORDER_TYPES = /* @__PURE__ */ new Set(["PURCHASE"]);
|
|
429
|
+
const MASIVO_PRODUCT_TYPES = /* @__PURE__ */ new Set(["ADD_TO_CART"]);
|
|
430
|
+
//#endregion
|
|
431
|
+
//#region ../../packages/constants/src/tracking-plan-catalog/transforms.ts
|
|
432
|
+
/** Params the SDK normalizes and SHA-256 hashes for ads destinations. */
|
|
433
|
+
const HASHED_PII_PARAM_NAMES = ["email", "phone"];
|
|
434
|
+
/**
|
|
435
|
+
* Match keys the ads APIs need raw (TikTok rejects a hashed `ttclid` or
|
|
436
|
+
* IP). They are never hashed and never copied into a pixel payload.
|
|
437
|
+
*/
|
|
438
|
+
const RAW_MATCH_PARAM_NAMES = ["device_ip", "ttclid"];
|
|
439
|
+
/** Personal params kept out of every pixel payload and plan sample. */
|
|
440
|
+
const PII_PARAM_NAMES = [...HASHED_PII_PARAM_NAMES, ...RAW_MATCH_PARAM_NAMES];
|
|
441
|
+
const PII$1 = new Set(PII_PARAM_NAMES);
|
|
442
|
+
const DECIMAL$1 = /^-?\d+(\.\d+)?$/;
|
|
443
|
+
const isRecord$1 = (value) => {
|
|
444
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
412
445
|
};
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
const b = w[i - 2];
|
|
419
|
-
const s0 = rotr(a, 7) ^ rotr(a, 18) ^ a >>> 3;
|
|
420
|
-
const s1 = rotr(b, 17) ^ rotr(b, 19) ^ b >>> 10;
|
|
421
|
-
w[i] = w[i - 16] + s0 + w[i - 7] + s1 | 0;
|
|
422
|
-
}
|
|
423
|
-
let [a, b, c, d, e, f, g, hh] = h;
|
|
424
|
-
for (let i = 0; i < 64; i += 1) {
|
|
425
|
-
const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
426
|
-
const ch = e & f ^ ~e & g;
|
|
427
|
-
const t1 = hh + s1 + ch + K[i] + w[i] | 0;
|
|
428
|
-
const t2 = (rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + (a & b ^ a & c ^ b & c) | 0;
|
|
429
|
-
hh = g;
|
|
430
|
-
g = f;
|
|
431
|
-
f = e;
|
|
432
|
-
e = d + t1 | 0;
|
|
433
|
-
d = c;
|
|
434
|
-
c = b;
|
|
435
|
-
b = a;
|
|
436
|
-
a = t1 + t2 | 0;
|
|
437
|
-
}
|
|
438
|
-
const next = [
|
|
439
|
-
a,
|
|
440
|
-
b,
|
|
441
|
-
c,
|
|
442
|
-
d,
|
|
443
|
-
e,
|
|
444
|
-
f,
|
|
445
|
-
g,
|
|
446
|
-
hh
|
|
447
|
-
];
|
|
448
|
-
for (let i = 0; i < 8; i += 1) h[i] = h[i] + next[i] | 0;
|
|
449
|
-
};
|
|
450
|
-
/** Pure-JS SHA-256 (hex) for runtimes without `crypto.subtle`. */
|
|
451
|
-
const sha256Hex = (value) => {
|
|
452
|
-
const view = padded(utf8(value));
|
|
453
|
-
const h = [...H0];
|
|
454
|
-
for (let offset = 0; offset < view.byteLength; offset += 64) compress(view, offset, h);
|
|
455
|
-
return h.map((word) => (word >>> 0).toString(16).padStart(8, "0")).join("");
|
|
456
|
-
};
|
|
457
|
-
//#endregion
|
|
458
|
-
//#region src/pii.ts
|
|
459
|
-
const PII_KEYS = /* @__PURE__ */ new Set([
|
|
460
|
-
"phone",
|
|
461
|
-
"device_ip",
|
|
462
|
-
"email",
|
|
463
|
-
"ttclid"
|
|
464
|
-
]);
|
|
465
|
-
const toHex = (bytes) => {
|
|
466
|
-
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
467
|
-
};
|
|
468
|
-
const isPiiParam = (name) => {
|
|
469
|
-
return PII_KEYS.has(name);
|
|
446
|
+
const stringVal = (value) => {
|
|
447
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
448
|
+
if (typeof value !== "string") return void 0;
|
|
449
|
+
const trimmed = value.trim();
|
|
450
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
470
451
|
};
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const digest = await subtle.digest("SHA-256", data);
|
|
477
|
-
return toHex(new Uint8Array(digest));
|
|
452
|
+
const toBig = (value) => {
|
|
453
|
+
if (typeof value === "number" && Number.isFinite(value)) return new Big(value);
|
|
454
|
+
if (typeof value !== "string") return void 0;
|
|
455
|
+
const trimmed = value.trim();
|
|
456
|
+
return DECIMAL$1.test(trimmed) ? new Big(trimmed) : void 0;
|
|
478
457
|
};
|
|
479
|
-
|
|
480
|
-
|
|
458
|
+
/** Amounts pass through unrounded; decimal strings become numbers. */
|
|
459
|
+
const amountOf = (value) => {
|
|
460
|
+
return toBig(value)?.toNumber();
|
|
481
461
|
};
|
|
482
|
-
const
|
|
483
|
-
|
|
484
|
-
if (
|
|
485
|
-
|
|
486
|
-
if (!value || typeof value !== "object") return value;
|
|
487
|
-
return hashPiiParams(value, hash);
|
|
462
|
+
const quantityOf = (value) => {
|
|
463
|
+
const amount = amountOf(value);
|
|
464
|
+
if (amount === void 0 || amount <= 0) return void 0;
|
|
465
|
+
return amount;
|
|
488
466
|
};
|
|
489
|
-
/**
|
|
490
|
-
const
|
|
467
|
+
/** Drops `undefined`, `null`, non-finite numbers and functions, deeply. */
|
|
468
|
+
const compact$1 = (value) => {
|
|
469
|
+
if (value === void 0 || value === null) return void 0;
|
|
470
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
471
|
+
if (typeof value === "function" || typeof value === "symbol") return;
|
|
472
|
+
if (Array.isArray(value)) return value.map(compact$1).filter((item) => item !== void 0);
|
|
473
|
+
if (value instanceof Date) return value.toISOString();
|
|
474
|
+
if (typeof value !== "object") return value;
|
|
491
475
|
const next = {};
|
|
492
|
-
for (const [key,
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
continue;
|
|
496
|
-
}
|
|
497
|
-
try {
|
|
498
|
-
next[key] = await hashDeep(value, hash);
|
|
499
|
-
} catch (error) {
|
|
500
|
-
onFailure?.(key, error);
|
|
501
|
-
}
|
|
476
|
+
for (const [key, nested] of Object.entries(value)) {
|
|
477
|
+
const kept = compact$1(nested);
|
|
478
|
+
if (kept !== void 0) next[key] = kept;
|
|
502
479
|
}
|
|
503
480
|
return next;
|
|
504
481
|
};
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
const ISO_4217_CODE = /^[A-Z]{3}$/;
|
|
508
|
-
const USD_AMOUNT_PARAM_SUFFIX = "_in_usd";
|
|
509
|
-
const isUsdAmountParam = (name) => {
|
|
510
|
-
return name.endsWith(USD_AMOUNT_PARAM_SUFFIX);
|
|
482
|
+
const compactPayload$1 = (value) => {
|
|
483
|
+
return compact$1(value);
|
|
511
484
|
};
|
|
512
|
-
const
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
485
|
+
const nonEmpty = (record) => {
|
|
486
|
+
const kept = compactPayload$1(record);
|
|
487
|
+
return Object.keys(kept).length > 0 ? kept : void 0;
|
|
488
|
+
};
|
|
489
|
+
const omit = (record, keys) => {
|
|
490
|
+
const drop = new Set(keys);
|
|
516
491
|
const next = {};
|
|
517
|
-
for (const [key,
|
|
518
|
-
if (isUsdAmountParam(key)) continue;
|
|
519
|
-
next[key] = dropUsdDeep(nested);
|
|
520
|
-
}
|
|
492
|
+
for (const [key, value] of Object.entries(record)) if (!drop.has(key)) next[key] = value;
|
|
521
493
|
return next;
|
|
522
494
|
};
|
|
523
|
-
const
|
|
524
|
-
return
|
|
525
|
-
};
|
|
526
|
-
/** Upper-case ISO 4217 code, or `undefined` when `code` is not one. */
|
|
527
|
-
const isoCurrencyCode = (code) => {
|
|
528
|
-
if (typeof code !== "string") return void 0;
|
|
529
|
-
const normalized = code.trim().toUpperCase();
|
|
530
|
-
if (!ISO_4217_CODE.test(normalized)) return void 0;
|
|
531
|
-
return normalized;
|
|
495
|
+
const itemIdOf$1 = (item) => {
|
|
496
|
+
return stringVal(item.item_id) ?? stringVal(item.content_id) ?? stringVal(item.product_id) ?? stringVal(item.sku);
|
|
532
497
|
};
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
const
|
|
536
|
-
return
|
|
537
|
-
|
|
538
|
-
type: "currency",
|
|
539
|
-
required,
|
|
540
|
-
scope: "ecommerce"
|
|
541
|
-
};
|
|
498
|
+
const sourceItems$1 = (input) => {
|
|
499
|
+
if (input.items && input.items.length > 0) return input.items;
|
|
500
|
+
const fromParams = input.params.items;
|
|
501
|
+
if (!Array.isArray(fromParams)) return [];
|
|
502
|
+
return fromParams.filter(isRecord$1);
|
|
542
503
|
};
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
504
|
+
/** `product_id` + `quantity` (+ `price`) become one item. */
|
|
505
|
+
const productItem = (params) => {
|
|
506
|
+
const item_id = stringVal(params.product_id);
|
|
507
|
+
if (!item_id) return [];
|
|
508
|
+
return [{
|
|
509
|
+
item_id,
|
|
510
|
+
item_name: params.item_name,
|
|
511
|
+
quantity: params.quantity,
|
|
512
|
+
price: params.price
|
|
513
|
+
}];
|
|
550
514
|
};
|
|
551
|
-
const
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
515
|
+
const canonicalItem = (item) => {
|
|
516
|
+
const price = amountOf(item.price ?? item.item_price);
|
|
517
|
+
const quantity = quantityOf(item.quantity);
|
|
518
|
+
const rest = omit(item, ["item_price"]);
|
|
519
|
+
const b1 = {
|
|
520
|
+
item_id: itemIdOf$1(item),
|
|
521
|
+
price,
|
|
522
|
+
quantity
|
|
557
523
|
};
|
|
524
|
+
return compactPayload$1({
|
|
525
|
+
...rest,
|
|
526
|
+
...b1
|
|
527
|
+
});
|
|
558
528
|
};
|
|
559
|
-
const
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
529
|
+
const ecommerceValue = (params, items) => {
|
|
530
|
+
const explicit = amountOf(params.value);
|
|
531
|
+
if (explicit !== void 0) return explicit;
|
|
532
|
+
if (items.length === 0) return void 0;
|
|
533
|
+
let total = new Big(0);
|
|
534
|
+
for (const item of items) {
|
|
535
|
+
const price = toBig(item.price);
|
|
536
|
+
if (!price) return void 0;
|
|
537
|
+
const quantity = quantityOf(item.quantity) ?? 1;
|
|
538
|
+
total = total.plus(price.times(quantity));
|
|
539
|
+
}
|
|
540
|
+
return total.toNumber();
|
|
566
541
|
};
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
542
|
+
const shapeOf = (input) => {
|
|
543
|
+
const params = input.params;
|
|
544
|
+
const explicitItems = sourceItems$1(input);
|
|
545
|
+
const hasValue = amountOf(params.value) !== void 0;
|
|
546
|
+
const ecommerce = isCatalogEcommerceEvent(input.name) || explicitItems.length > 0 || hasValue;
|
|
547
|
+
if (!ecommerce) return {
|
|
548
|
+
ecommerce,
|
|
549
|
+
items: []
|
|
573
550
|
};
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
const both = (name) => {
|
|
551
|
+
const items = (explicitItems.length > 0 ? explicitItems : productItem(params)).map(canonicalItem).filter((item) => {
|
|
552
|
+
return item.item_id !== void 0 || item.item_name !== void 0;
|
|
553
|
+
});
|
|
578
554
|
return {
|
|
579
|
-
|
|
580
|
-
|
|
555
|
+
ecommerce,
|
|
556
|
+
items,
|
|
557
|
+
value: ecommerceValue(params, items),
|
|
558
|
+
currency: isoCurrencyCode(params.currency) ?? isoCurrencyCode(input.currency)
|
|
581
559
|
};
|
|
582
560
|
};
|
|
583
|
-
const
|
|
584
|
-
return
|
|
585
|
-
|
|
586
|
-
reactNative: null
|
|
587
|
-
};
|
|
561
|
+
const hasMoney = (shape) => {
|
|
562
|
+
if (shape.value !== void 0) return true;
|
|
563
|
+
return shape.items.some((item) => item.price !== void 0);
|
|
588
564
|
};
|
|
589
|
-
const
|
|
590
|
-
return
|
|
591
|
-
web,
|
|
592
|
-
reactNative
|
|
593
|
-
};
|
|
565
|
+
const orderIdOf = (params) => {
|
|
566
|
+
return stringVal(params.transaction_id) ?? stringVal(params.order_id);
|
|
594
567
|
};
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
568
|
+
/**
|
|
569
|
+
* Delivery-only TikTok names for storefronts already emitting these events.
|
|
570
|
+
* Never used to build or prefill tracking plans.
|
|
571
|
+
*/
|
|
572
|
+
const LEGACY_TIKTOK_ALIASES = {
|
|
573
|
+
medios_view_item_delivery: "UnlockAchievement",
|
|
574
|
+
medios_view_item_express: "StartTrial",
|
|
575
|
+
medios_purchase_delivery: "Subscribe",
|
|
576
|
+
medios_purchase_express: "GenerateLead"
|
|
600
577
|
};
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
};
|
|
578
|
+
const resolvedName = (input, provider) => {
|
|
579
|
+
const alias = LEGACY_TIKTOK_ALIASES[input.name];
|
|
580
|
+
if (provider === "tiktok" && alias) return alias;
|
|
581
|
+
return destinationEventName(input.name, provider, input.runtime);
|
|
606
582
|
};
|
|
607
|
-
const
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
583
|
+
const ECOMMERCE_KEYS = [
|
|
584
|
+
"items",
|
|
585
|
+
"value",
|
|
586
|
+
"currency"
|
|
587
|
+
];
|
|
588
|
+
const baseParams = (input, shape) => {
|
|
589
|
+
const drop = [...PII$1];
|
|
590
|
+
if (shape.ecommerce) drop.push(...ECOMMERCE_KEYS);
|
|
591
|
+
return omit(input.params, drop);
|
|
613
592
|
};
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
const
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
],
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
593
|
+
const ga4Payload = (input, shape) => {
|
|
594
|
+
const base = baseParams(input, shape);
|
|
595
|
+
if (!shape.ecommerce) return compactPayload$1(base);
|
|
596
|
+
const currency = hasMoney(shape) ? shape.currency : void 0;
|
|
597
|
+
const items = shape.items.length > 0 ? shape.items : void 0;
|
|
598
|
+
const transaction_id = orderIdOf(input.params);
|
|
599
|
+
const b1 = {
|
|
600
|
+
items,
|
|
601
|
+
value: shape.value,
|
|
602
|
+
currency,
|
|
603
|
+
transaction_id
|
|
604
|
+
};
|
|
605
|
+
return compactPayload$1({
|
|
606
|
+
...base,
|
|
607
|
+
...b1
|
|
608
|
+
});
|
|
609
|
+
};
|
|
610
|
+
const userDataOf = (input, keys) => {
|
|
611
|
+
const params = input.params;
|
|
612
|
+
const b1 = {
|
|
613
|
+
[keys.email]: params.email,
|
|
614
|
+
[keys.phone]: params.phone
|
|
615
|
+
};
|
|
616
|
+
const external_id = input.identity?.hashedUserId;
|
|
617
|
+
const data = compactPayload$1({
|
|
618
|
+
...b1,
|
|
619
|
+
external_id
|
|
620
|
+
});
|
|
621
|
+
const entries = Object.entries(data).filter(([, value]) => {
|
|
622
|
+
return typeof value === "string" && value.length > 0;
|
|
623
|
+
});
|
|
624
|
+
if (entries.length === 0) return void 0;
|
|
625
|
+
return Object.fromEntries(entries);
|
|
626
|
+
};
|
|
627
|
+
const numItems = (items) => {
|
|
628
|
+
let total = new Big(0);
|
|
629
|
+
for (const item of items) total = total.plus(quantityOf(item.quantity) ?? 1);
|
|
630
|
+
return total.toNumber();
|
|
631
|
+
};
|
|
632
|
+
const metaContents = (items) => {
|
|
633
|
+
return items.map((item) => {
|
|
634
|
+
const b1 = {
|
|
635
|
+
id: item.item_id,
|
|
636
|
+
quantity: quantityOf(item.quantity) ?? 1
|
|
637
|
+
};
|
|
638
|
+
return compactPayload$1({
|
|
639
|
+
...b1,
|
|
640
|
+
item_price: item.price
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
};
|
|
644
|
+
const flatValue = (value) => {
|
|
645
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
646
|
+
if (typeof value === "boolean") return String(value);
|
|
647
|
+
if (value === void 0 || value === null) return void 0;
|
|
648
|
+
return JSON.stringify(value);
|
|
649
|
+
};
|
|
650
|
+
/** App events params must be strings or numbers. */
|
|
651
|
+
const flatParams = (params) => {
|
|
652
|
+
const next = {};
|
|
653
|
+
for (const [key, value] of Object.entries(compactPayload$1(params))) next[key] = flatValue(value);
|
|
654
|
+
return compactPayload$1(next);
|
|
655
|
+
};
|
|
656
|
+
const metaWebPayload = (input, shape) => {
|
|
657
|
+
const base = baseParams(input, shape);
|
|
658
|
+
if (!shape.ecommerce) return compactPayload$1(base);
|
|
659
|
+
const currency = hasMoney(shape) ? shape.currency : void 0;
|
|
660
|
+
const contents = metaContents(shape.items);
|
|
661
|
+
const listed = contents.length > 0;
|
|
662
|
+
const content_ids = listed ? contents.map((row) => row.id) : void 0;
|
|
663
|
+
const b1 = {
|
|
664
|
+
value: shape.value,
|
|
665
|
+
currency
|
|
666
|
+
};
|
|
667
|
+
const b2 = {
|
|
668
|
+
contents: listed ? contents : void 0,
|
|
669
|
+
content_ids
|
|
670
|
+
};
|
|
671
|
+
const b3 = { content_type: listed ? "product" : void 0 };
|
|
672
|
+
const num_items = listed ? numItems(shape.items) : void 0;
|
|
673
|
+
return compactPayload$1({
|
|
674
|
+
...base,
|
|
675
|
+
...b1,
|
|
676
|
+
...b2,
|
|
677
|
+
...b3,
|
|
678
|
+
num_items
|
|
679
|
+
});
|
|
680
|
+
};
|
|
681
|
+
const metaNativePayload = (input, shape) => {
|
|
682
|
+
const base = baseParams(input, shape);
|
|
683
|
+
if (!shape.ecommerce) return flatParams(base);
|
|
684
|
+
const contents = metaContents(shape.items);
|
|
685
|
+
const listed = contents.length > 0;
|
|
686
|
+
const ids = contents.map((row) => row.id);
|
|
687
|
+
const fb_content_id = ids.length === 1 ? ids[0] : JSON.stringify(ids);
|
|
688
|
+
const b1 = { fb_currency: hasMoney(shape) ? shape.currency : void 0 };
|
|
689
|
+
const b2 = { fb_content: listed ? JSON.stringify(contents) : void 0 };
|
|
690
|
+
const b3 = { fb_content_id: listed ? fb_content_id : void 0 };
|
|
691
|
+
const b4 = { fb_content_type: listed ? "product" : void 0 };
|
|
692
|
+
const b5 = { fb_num_items: listed ? numItems(shape.items) : void 0 };
|
|
693
|
+
const b6 = { fb_order_id: orderIdOf(input.params) };
|
|
694
|
+
return flatParams({
|
|
695
|
+
...base,
|
|
696
|
+
...b1,
|
|
697
|
+
...b2,
|
|
698
|
+
...b3,
|
|
699
|
+
...b4,
|
|
700
|
+
...b5,
|
|
701
|
+
...b6
|
|
702
|
+
});
|
|
703
|
+
};
|
|
704
|
+
const tiktokContents = (items) => {
|
|
705
|
+
return items.map((item) => {
|
|
706
|
+
const b1 = { content_id: item.item_id };
|
|
707
|
+
const b2 = {
|
|
708
|
+
quantity: quantityOf(item.quantity) ?? 1,
|
|
709
|
+
price: item.price
|
|
710
|
+
};
|
|
711
|
+
const b3 = { content_name: item.item_name };
|
|
712
|
+
const b4 = { content_category: item.item_category };
|
|
713
|
+
return compactPayload$1({
|
|
714
|
+
...b1,
|
|
715
|
+
...b2,
|
|
716
|
+
...b3,
|
|
717
|
+
...b4
|
|
718
|
+
});
|
|
719
|
+
});
|
|
720
|
+
};
|
|
721
|
+
const tiktokPayload = (input, shape) => {
|
|
722
|
+
const base = baseParams(input, shape);
|
|
723
|
+
if (!shape.ecommerce) return compactPayload$1(base);
|
|
724
|
+
const currency = hasMoney(shape) ? shape.currency : void 0;
|
|
725
|
+
const contents = tiktokContents(shape.items);
|
|
726
|
+
const listed = contents.length > 0;
|
|
727
|
+
const firstId = contents.length === 1 ? contents[0]?.content_id : void 0;
|
|
728
|
+
const content_id = stringVal(input.params.content_id) ?? firstId;
|
|
729
|
+
const b1 = {
|
|
730
|
+
value: shape.value,
|
|
731
|
+
currency
|
|
732
|
+
};
|
|
733
|
+
const b2 = {
|
|
734
|
+
contents: listed ? contents : void 0,
|
|
735
|
+
content_id
|
|
736
|
+
};
|
|
737
|
+
const b3 = { content_type: listed ? "product" : void 0 };
|
|
738
|
+
const b4 = { order_id: orderIdOf(input.params) };
|
|
739
|
+
return compactPayload$1({
|
|
740
|
+
...base,
|
|
741
|
+
...b1,
|
|
742
|
+
...b2,
|
|
743
|
+
...b3,
|
|
744
|
+
...b4
|
|
745
|
+
});
|
|
746
|
+
};
|
|
747
|
+
const MASIVO_ITEM_FIELDS = [
|
|
748
|
+
"item_id",
|
|
749
|
+
"price",
|
|
750
|
+
"quantity",
|
|
751
|
+
"discount"
|
|
752
|
+
];
|
|
753
|
+
/** Masivo product: `value` is the line total (unit price × amount). */
|
|
754
|
+
const masivoProduct = (item) => {
|
|
755
|
+
const sku = stringVal(item.item_id);
|
|
756
|
+
const price = toBig(item.price);
|
|
757
|
+
if (!sku || !price) return void 0;
|
|
758
|
+
const amount = quantityOf(item.quantity) ?? 1;
|
|
759
|
+
const value = price.times(amount).toNumber();
|
|
760
|
+
const discount = toBig(item.discount);
|
|
761
|
+
const discounted = discount ? price.minus(discount).times(amount) : null;
|
|
762
|
+
const discounted_value = discounted?.gte(0) ? discounted.toNumber() : null;
|
|
763
|
+
const metadata = nonEmpty(omit(item, MASIVO_ITEM_FIELDS));
|
|
764
|
+
return compactPayload$1({
|
|
765
|
+
sku,
|
|
766
|
+
amount,
|
|
767
|
+
value,
|
|
768
|
+
discounted_value,
|
|
769
|
+
metadata
|
|
770
|
+
});
|
|
771
|
+
};
|
|
772
|
+
const masivoProducts = (items) => {
|
|
773
|
+
return items.flatMap((item) => {
|
|
774
|
+
const product = masivoProduct(item);
|
|
775
|
+
return product ? [product] : [];
|
|
776
|
+
});
|
|
777
|
+
};
|
|
778
|
+
const ORDER_PARAMS = [
|
|
779
|
+
"order_id",
|
|
780
|
+
"transaction_id",
|
|
781
|
+
"payment_method",
|
|
782
|
+
"payment_type",
|
|
783
|
+
"discounted_value",
|
|
784
|
+
"shipping",
|
|
785
|
+
"store_id",
|
|
786
|
+
"channel_id",
|
|
787
|
+
"product_id",
|
|
788
|
+
"quantity",
|
|
789
|
+
"price",
|
|
790
|
+
...ECOMMERCE_KEYS
|
|
791
|
+
];
|
|
792
|
+
const PRODUCT_PARAMS = [
|
|
793
|
+
"product_id",
|
|
794
|
+
"quantity",
|
|
795
|
+
"price",
|
|
796
|
+
"discounted_value",
|
|
797
|
+
...ECOMMERCE_KEYS
|
|
798
|
+
];
|
|
799
|
+
const masivoOrder = (input, shape) => {
|
|
800
|
+
const params = input.params;
|
|
801
|
+
const identity = input.identity ?? {};
|
|
802
|
+
const purchase_id = orderIdOf(params);
|
|
803
|
+
const products = masivoProducts(shape.items);
|
|
804
|
+
if (!purchase_id || shape.value === void 0) return void 0;
|
|
805
|
+
if (products.length === 0) return void 0;
|
|
806
|
+
const payment_method = masivoPaymentMethod(params.payment_method ?? params.payment_type);
|
|
807
|
+
const store_id = stringVal(params.store_id) ?? identity.storeId;
|
|
808
|
+
const channel_id = stringVal(params.channel_id) ?? identity.channelId;
|
|
809
|
+
const shipping = nonEmpty({ value: amountOf(params.shipping) });
|
|
810
|
+
const b1 = {
|
|
811
|
+
purchase_id,
|
|
812
|
+
value: shape.value,
|
|
813
|
+
products,
|
|
814
|
+
payment_method
|
|
815
|
+
};
|
|
816
|
+
const b2 = {
|
|
817
|
+
store_id,
|
|
818
|
+
channel_id,
|
|
819
|
+
shipping
|
|
820
|
+
};
|
|
821
|
+
const b3 = { discounted_value: amountOf(params.discounted_value) };
|
|
822
|
+
return compactPayload$1({
|
|
823
|
+
...b1,
|
|
824
|
+
...b2,
|
|
825
|
+
...b3
|
|
826
|
+
});
|
|
827
|
+
};
|
|
828
|
+
const masivoProductOf = (input, shape) => {
|
|
829
|
+
const first = shape.items[0];
|
|
830
|
+
if (!first) return void 0;
|
|
831
|
+
const product = masivoProduct(first);
|
|
832
|
+
if (!product) return void 0;
|
|
833
|
+
const discounted = amountOf(input.params.discounted_value);
|
|
834
|
+
if (discounted === void 0) return product;
|
|
835
|
+
return {
|
|
836
|
+
...product,
|
|
837
|
+
discounted_value: discounted
|
|
838
|
+
};
|
|
839
|
+
};
|
|
840
|
+
const ATTRIBUTION = new Set(MASIVO_ATTRIBUTION_FIELDS);
|
|
841
|
+
const isIso = (value) => {
|
|
842
|
+
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
843
|
+
};
|
|
844
|
+
const masivoTopLevel = (params) => {
|
|
845
|
+
const body = {};
|
|
846
|
+
const used = [];
|
|
847
|
+
const take = (key, ok) => {
|
|
848
|
+
if (!ok) return;
|
|
849
|
+
body[key] = params[key];
|
|
850
|
+
used.push(key);
|
|
851
|
+
};
|
|
852
|
+
take("fulfilled", typeof params.fulfilled === "boolean");
|
|
853
|
+
take("reserve", Number.isInteger(params.reserve));
|
|
854
|
+
take("referral_code", !!stringVal(params.referral_code));
|
|
855
|
+
take("redeem", Array.isArray(params.redeem));
|
|
856
|
+
take("tags", isRecord$1(params.tags));
|
|
857
|
+
take("ga4", isRecord$1(params.ga4));
|
|
858
|
+
take("issued_at", isIso(params.issued_at));
|
|
859
|
+
return {
|
|
860
|
+
body,
|
|
861
|
+
used
|
|
862
|
+
};
|
|
863
|
+
};
|
|
864
|
+
const masivoAttribution = (params) => {
|
|
865
|
+
const next = { ...isRecord$1(params.attribution) ? params.attribution : {} };
|
|
866
|
+
for (const [key, value] of Object.entries(params)) if (ATTRIBUTION.has(key)) next[key] = value;
|
|
867
|
+
return nonEmpty(next);
|
|
868
|
+
};
|
|
869
|
+
/**
|
|
870
|
+
* Masivo "Emit event" body. Documented fields go top-level (or inside
|
|
871
|
+
* `order` / `product`); every other param goes to `metadata`.
|
|
872
|
+
*/
|
|
873
|
+
const masivoPayload = (input, shape, type) => {
|
|
874
|
+
const params = input.params;
|
|
875
|
+
const identity = input.identity ?? {};
|
|
876
|
+
const raw = PII_PARAM_NAMES.filter((key) => key !== "ttclid");
|
|
877
|
+
const fixed = [
|
|
878
|
+
"metadata",
|
|
879
|
+
"attribution",
|
|
880
|
+
...[
|
|
881
|
+
"customer_id",
|
|
882
|
+
"brand_id",
|
|
883
|
+
"platform"
|
|
834
884
|
],
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
885
|
+
...raw
|
|
886
|
+
];
|
|
887
|
+
const used = /* @__PURE__ */ new Set([...fixed, ...ATTRIBUTION]);
|
|
888
|
+
const customer_id = stringVal(params.customer_id) ?? identity.customerId;
|
|
889
|
+
const brand_id = stringVal(params.brand_id) ?? identity.brandId;
|
|
890
|
+
const platform = stringVal(params.platform) ?? identity.platform;
|
|
891
|
+
const at = (/* @__PURE__ */ new Date(input.eventTime * 1e3)).toISOString();
|
|
892
|
+
const top = masivoTopLevel(params);
|
|
893
|
+
for (const key of top.used) used.add(key);
|
|
894
|
+
const body = {
|
|
895
|
+
customer_id,
|
|
896
|
+
brand_id,
|
|
897
|
+
type
|
|
898
|
+
};
|
|
899
|
+
Object.assign(body, {
|
|
900
|
+
platform,
|
|
901
|
+
issued_at: at
|
|
902
|
+
}, top.body);
|
|
903
|
+
if (MASIVO_ORDER_TYPES.has(type)) {
|
|
904
|
+
const order = masivoOrder(input, shape);
|
|
905
|
+
if (order) {
|
|
906
|
+
body.order = order;
|
|
907
|
+
for (const key of ORDER_PARAMS) used.add(key);
|
|
841
908
|
}
|
|
842
|
-
}
|
|
843
|
-
{
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
runtimes: ["web", "react-native"],
|
|
849
|
-
recommendedParams: [{
|
|
850
|
-
name: "category_id",
|
|
851
|
-
type: "string",
|
|
852
|
-
required: true,
|
|
853
|
-
scope: "event"
|
|
854
|
-
}],
|
|
855
|
-
destinations: {
|
|
856
|
-
"fire-spark": both("category_visited"),
|
|
857
|
-
ga4: both("category_visited"),
|
|
858
|
-
meta: both("category_visited"),
|
|
859
|
-
tiktok: both("category_visited"),
|
|
860
|
-
masivo: both("category_visited")
|
|
909
|
+
}
|
|
910
|
+
if (MASIVO_PRODUCT_TYPES.has(type)) {
|
|
911
|
+
const product = masivoProductOf(input, shape);
|
|
912
|
+
if (product) {
|
|
913
|
+
body.product = product;
|
|
914
|
+
for (const key of PRODUCT_PARAMS) used.add(key);
|
|
861
915
|
}
|
|
862
916
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
const
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
917
|
+
body.attribution = masivoAttribution(params);
|
|
918
|
+
if (shape.ecommerce) used.add("currency");
|
|
919
|
+
const extra = isRecord$1(params.metadata) ? params.metadata : {};
|
|
920
|
+
const rest = omit(params, used);
|
|
921
|
+
const currency = hasMoney(shape) ? shape.currency : void 0;
|
|
922
|
+
const b1 = {
|
|
923
|
+
store_id: identity.storeId,
|
|
924
|
+
channel_id: identity.channelId
|
|
925
|
+
};
|
|
926
|
+
const b2 = {
|
|
927
|
+
fulfillment_id: identity.fulfillmentId,
|
|
928
|
+
currency
|
|
929
|
+
};
|
|
930
|
+
body.metadata = nonEmpty({
|
|
931
|
+
...b1,
|
|
932
|
+
...b2,
|
|
933
|
+
...rest,
|
|
934
|
+
...extra
|
|
935
|
+
});
|
|
936
|
+
return compactPayload$1(body);
|
|
871
937
|
};
|
|
872
938
|
/**
|
|
873
|
-
*
|
|
874
|
-
*
|
|
939
|
+
* Builds one destination payload from a tracked event. Returns `null` when
|
|
940
|
+
* the catalog does not send this event to `provider` in this runtime.
|
|
875
941
|
*/
|
|
876
|
-
const
|
|
877
|
-
const
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
942
|
+
const transformForProvider = (provider, input) => {
|
|
943
|
+
const params = dropUsdAmountParams(input.params);
|
|
944
|
+
const itemsIn = input.items?.map((item) => dropUsdAmountParams(item));
|
|
945
|
+
const next = {
|
|
946
|
+
...input,
|
|
947
|
+
params,
|
|
948
|
+
items: itemsIn
|
|
949
|
+
};
|
|
950
|
+
const name = resolvedName(next, provider);
|
|
951
|
+
if (name === null) return null;
|
|
952
|
+
if (provider === "fire-spark") return {
|
|
953
|
+
provider,
|
|
954
|
+
name,
|
|
955
|
+
payload: compactPayload$1(params)
|
|
956
|
+
};
|
|
957
|
+
const shape = shapeOf(next);
|
|
958
|
+
if (provider === "ga4") return {
|
|
959
|
+
provider,
|
|
960
|
+
name,
|
|
961
|
+
payload: ga4Payload(next, shape)
|
|
962
|
+
};
|
|
963
|
+
if (provider === "meta") {
|
|
964
|
+
const userData = userDataOf(next, {
|
|
965
|
+
email: "em",
|
|
966
|
+
phone: "ph"
|
|
967
|
+
});
|
|
968
|
+
if (next.runtime === "web") return {
|
|
969
|
+
provider,
|
|
970
|
+
name,
|
|
971
|
+
payload: metaWebPayload(next, shape),
|
|
972
|
+
userData
|
|
973
|
+
};
|
|
974
|
+
return {
|
|
975
|
+
provider,
|
|
976
|
+
name,
|
|
977
|
+
payload: metaNativePayload(next, shape),
|
|
978
|
+
userData,
|
|
979
|
+
valueToSum: shape.value
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
if (provider === "tiktok") {
|
|
983
|
+
const userData = userDataOf(next, {
|
|
984
|
+
email: "email",
|
|
985
|
+
phone: "phone_number"
|
|
986
|
+
});
|
|
987
|
+
return {
|
|
988
|
+
provider,
|
|
989
|
+
name,
|
|
990
|
+
payload: tiktokPayload(next, shape),
|
|
991
|
+
userData
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
return {
|
|
995
|
+
provider,
|
|
996
|
+
name,
|
|
997
|
+
payload: masivoPayload(next, shape, name)
|
|
998
|
+
};
|
|
884
999
|
};
|
|
885
1000
|
//#endregion
|
|
886
|
-
//#region
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
"utm_source",
|
|
890
|
-
"utm_medium",
|
|
891
|
-
"utm_campaign",
|
|
892
|
-
"utm_term",
|
|
893
|
-
"utm_content",
|
|
894
|
-
"utm_referral",
|
|
895
|
-
"gclid",
|
|
896
|
-
"gbraid",
|
|
897
|
-
"wbraid",
|
|
898
|
-
"fbclid",
|
|
899
|
-
"ttclid",
|
|
900
|
-
"fbc",
|
|
901
|
-
"fbp",
|
|
902
|
-
"ttp",
|
|
903
|
-
"event_source_url"
|
|
904
|
-
];
|
|
905
|
-
const MASIVO_PAYMENT_METHODS = [
|
|
906
|
-
"CREDIT",
|
|
907
|
-
"DEBIT",
|
|
908
|
-
"CASH",
|
|
909
|
-
"BANK_TRANSFER",
|
|
910
|
-
"OTHER"
|
|
911
|
-
];
|
|
912
|
-
const PAYMENT_ALIASES = {
|
|
913
|
-
CASH: "CASH",
|
|
914
|
-
BANK_TRANSFER: "BANK_TRANSFER",
|
|
915
|
-
CARD: "OTHER",
|
|
916
|
-
PIN_PAD: "OTHER",
|
|
917
|
-
WALLET: "OTHER",
|
|
918
|
-
FINANCING: "OTHER"
|
|
919
|
-
};
|
|
920
|
-
/** Spark tender method (or a Masivo value) → Masivo `payment_method`. */
|
|
921
|
-
const masivoPaymentMethod = (method) => {
|
|
922
|
-
if (typeof method !== "string") return "OTHER";
|
|
923
|
-
const upper = method.trim().toUpperCase();
|
|
924
|
-
const alias = PAYMENT_ALIASES[upper];
|
|
925
|
-
if (alias) return alias;
|
|
926
|
-
if (MASIVO_PAYMENT_METHODS.includes(upper)) return upper;
|
|
927
|
-
return "OTHER";
|
|
928
|
-
};
|
|
929
|
-
new Set(MASIVO_ATTRIBUTION_FIELDS);
|
|
930
|
-
/** Masivo `type` that carries an order or a single product. */
|
|
931
|
-
const MASIVO_ORDER_TYPES = /* @__PURE__ */ new Set(["PURCHASE"]);
|
|
932
|
-
const MASIVO_PRODUCT_TYPES = /* @__PURE__ */ new Set(["ADD_TO_CART"]);
|
|
1001
|
+
//#region src/version.ts
|
|
1002
|
+
/** Kept equal to package.json by `pnpm sdk:release` (see version.test.ts). */
|
|
1003
|
+
const SDK_VERSION = "0.23.0";
|
|
933
1004
|
//#endregion
|
|
934
|
-
//#region
|
|
935
|
-
|
|
936
|
-
|
|
1005
|
+
//#region src/contract.ts
|
|
1006
|
+
const TRACKING_CONTRACT_VERSION = 1;
|
|
1007
|
+
/** Versions a server accepts: the current one and the one before. */
|
|
1008
|
+
const SUPPORTED_CONTRACT_VERSIONS = [1];
|
|
1009
|
+
const SERVER_PROVIDER_IDS = [
|
|
1010
|
+
"meta",
|
|
1011
|
+
"tiktok",
|
|
1012
|
+
"google-ads"
|
|
1013
|
+
];
|
|
1014
|
+
const TRACKING_SDK_NAME = "@fire-spark/analytics";
|
|
1015
|
+
const MAX_ENVELOPE_EVENTS = 20;
|
|
1016
|
+
/** Params never copied into a contract event (raw personal data). */
|
|
1017
|
+
const CONTRACT_PII_KEYS = [
|
|
1018
|
+
"email",
|
|
937
1019
|
"phone",
|
|
1020
|
+
"first_name",
|
|
1021
|
+
"last_name",
|
|
938
1022
|
"device_ip",
|
|
939
|
-
"email",
|
|
940
1023
|
"ttclid"
|
|
941
1024
|
];
|
|
942
|
-
|
|
1025
|
+
/** `hashUserData()` field → its snake_case wire name. */
|
|
1026
|
+
const HASHED_USER_WIRE_KEYS = {
|
|
1027
|
+
emailSha256: "email_sha256",
|
|
1028
|
+
emailGoogleSha256: "email_google_sha256",
|
|
1029
|
+
phoneE164Sha256: "phone_e164_sha256",
|
|
1030
|
+
phoneDigitsSha256: "phone_digits_sha256",
|
|
1031
|
+
externalIdSha256: "external_id_sha256",
|
|
1032
|
+
firstNameSha256: "first_name_sha256",
|
|
1033
|
+
lastNameSha256: "last_name_sha256"
|
|
1034
|
+
};
|
|
1035
|
+
/** `captureClickIds()` field → its snake_case wire name. */
|
|
1036
|
+
const CLICK_ID_WIRE_KEYS = {
|
|
1037
|
+
fbp: "fbp",
|
|
1038
|
+
fbc: "fbc",
|
|
1039
|
+
ttclid: "ttclid",
|
|
1040
|
+
ttp: "ttp",
|
|
1041
|
+
gclid: "gclid",
|
|
1042
|
+
gbraid: "gbraid",
|
|
1043
|
+
wbraid: "wbraid",
|
|
1044
|
+
googleSessionAttributes: "google_session_attributes"
|
|
1045
|
+
};
|
|
1046
|
+
const HASHED_USER_KEYS = Object.keys(HASHED_USER_WIRE_KEYS);
|
|
1047
|
+
const CLICK_ID_KEYS$1 = Object.keys(CLICK_ID_WIRE_KEYS);
|
|
1048
|
+
const EVENT_ID = /^[A-Za-z0-9:_.-]{1,64}$/;
|
|
943
1049
|
const DECIMAL = /^-?\d+(\.\d+)?$/;
|
|
1050
|
+
const PII = new Set(CONTRACT_PII_KEYS);
|
|
1051
|
+
const LIFTED = /* @__PURE__ */ new Set([
|
|
1052
|
+
"items",
|
|
1053
|
+
"value",
|
|
1054
|
+
"currency"
|
|
1055
|
+
]);
|
|
944
1056
|
const isRecord = (value) => {
|
|
945
1057
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
946
1058
|
};
|
|
947
|
-
|
|
1059
|
+
/** Trimmed non-empty string; a finite number becomes its string. */
|
|
1060
|
+
const textOf = (value) => {
|
|
948
1061
|
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
949
1062
|
if (typeof value !== "string") return void 0;
|
|
950
1063
|
const trimmed = value.trim();
|
|
951
1064
|
return trimmed.length > 0 ? trimmed : void 0;
|
|
952
1065
|
};
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
const trimmed = value.trim();
|
|
957
|
-
return DECIMAL.test(trimmed) ? new Big(trimmed) : void 0;
|
|
958
|
-
};
|
|
959
|
-
/** Amounts pass through unrounded; decimal strings become numbers. */
|
|
960
|
-
const amountOf = (value) => {
|
|
961
|
-
return toBig(value)?.toNumber();
|
|
962
|
-
};
|
|
963
|
-
const quantityOf = (value) => {
|
|
964
|
-
const amount = amountOf(value);
|
|
965
|
-
if (amount === void 0 || amount <= 0) return void 0;
|
|
966
|
-
return amount;
|
|
967
|
-
};
|
|
968
|
-
/** Drops `undefined`, `null`, non-finite numbers and functions, deeply. */
|
|
969
|
-
const compact = (value) => {
|
|
970
|
-
if (value === void 0 || value === null) return void 0;
|
|
971
|
-
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
972
|
-
if (typeof value === "function" || typeof value === "symbol") return;
|
|
973
|
-
if (Array.isArray(value)) return value.map(compact).filter((item) => item !== void 0);
|
|
974
|
-
if (value instanceof Date) return value.toISOString();
|
|
975
|
-
if (typeof value !== "object") return value;
|
|
976
|
-
const next = {};
|
|
977
|
-
for (const [key, nested] of Object.entries(value)) {
|
|
978
|
-
const kept = compact(nested);
|
|
979
|
-
if (kept !== void 0) next[key] = kept;
|
|
980
|
-
}
|
|
981
|
-
return next;
|
|
982
|
-
};
|
|
983
|
-
const compactPayload$1 = (value) => {
|
|
984
|
-
return compact(value);
|
|
1066
|
+
/** 1 to 64 of `[A-Za-z0-9:_.-]`. */
|
|
1067
|
+
const isValidEventId = (value) => {
|
|
1068
|
+
return typeof value === "string" && EVENT_ID.test(value);
|
|
985
1069
|
};
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1070
|
+
/**
|
|
1071
|
+
* `purchase:<order_id>`: the event id every destination (Meta pixel,
|
|
1072
|
+
* TikTok pixel, the server relay) gets for a purchase, so a reloaded
|
|
1073
|
+
* confirmation page is deduplicated instead of counted twice. Returns
|
|
1074
|
+
* `undefined` when there is no order id or the result is not a valid
|
|
1075
|
+
* event id (longer than 64 characters or with other characters).
|
|
1076
|
+
*/
|
|
1077
|
+
const purchaseEventId = (orderId) => {
|
|
1078
|
+
const id = textOf(orderId);
|
|
1079
|
+
if (!id) return void 0;
|
|
1080
|
+
const eventId = `purchase:${id}`;
|
|
1081
|
+
return isValidEventId(eventId) ? eventId : void 0;
|
|
989
1082
|
};
|
|
990
|
-
const
|
|
991
|
-
|
|
1083
|
+
const isDecimalString = (value) => {
|
|
1084
|
+
return typeof value === "string" && DECIMAL.test(value);
|
|
1085
|
+
};
|
|
1086
|
+
/**
|
|
1087
|
+
* Money as a decimal string, never rounded. A decimal string is kept as
|
|
1088
|
+
* written (`"25.90"` stays `"25.90"`); a finite number keeps every digit
|
|
1089
|
+
* of its shortest representation (`25.9` → `"25.9"`).
|
|
1090
|
+
*/
|
|
1091
|
+
const toDecimalString = (value) => {
|
|
1092
|
+
if (typeof value === "number") return Number.isFinite(value) ? new Big(value).toFixed() : void 0;
|
|
1093
|
+
if (typeof value !== "string") return void 0;
|
|
1094
|
+
const trimmed = value.trim();
|
|
1095
|
+
return DECIMAL.test(trimmed) ? trimmed : void 0;
|
|
1096
|
+
};
|
|
1097
|
+
const positiveDecimal = (value) => {
|
|
1098
|
+
const decimal = toDecimalString(value);
|
|
1099
|
+
if (decimal === void 0) return void 0;
|
|
1100
|
+
return new Big(decimal).gt(0) ? decimal : void 0;
|
|
1101
|
+
};
|
|
1102
|
+
/** Removes personal params at every depth. */
|
|
1103
|
+
const withoutPii = (value) => {
|
|
1104
|
+
if (Array.isArray(value)) return value.map(withoutPii);
|
|
1105
|
+
if (!isRecord(value)) return value;
|
|
992
1106
|
const next = {};
|
|
993
|
-
for (const [key,
|
|
1107
|
+
for (const [key, nested] of Object.entries(value)) if (!PII.has(key)) next[key] = withoutPii(nested);
|
|
994
1108
|
return next;
|
|
995
1109
|
};
|
|
996
|
-
const
|
|
997
|
-
return
|
|
1110
|
+
const stripContractPii = (params) => {
|
|
1111
|
+
return withoutPii(params);
|
|
998
1112
|
};
|
|
999
|
-
const
|
|
1000
|
-
|
|
1001
|
-
const fromParams = input.params.items;
|
|
1002
|
-
if (!Array.isArray(fromParams)) return [];
|
|
1003
|
-
return fromParams.filter(isRecord);
|
|
1113
|
+
const itemIdOf = (item) => {
|
|
1114
|
+
return textOf(item.item_id) ?? textOf(item.content_id) ?? textOf(item.product_id) ?? textOf(item.sku);
|
|
1004
1115
|
};
|
|
1005
|
-
/**
|
|
1006
|
-
const
|
|
1007
|
-
|
|
1116
|
+
/** Same sources as the pixel catalog: `items`, else `product_id`. */
|
|
1117
|
+
const sourceItems = (params) => {
|
|
1118
|
+
if (Array.isArray(params.items) && params.items.length > 0) return params.items.filter(isRecord);
|
|
1119
|
+
const item_id = textOf(params.product_id);
|
|
1008
1120
|
if (!item_id) return [];
|
|
1009
1121
|
return [{
|
|
1010
1122
|
item_id,
|
|
@@ -1013,494 +1125,1078 @@ const productItem = (params) => {
|
|
|
1013
1125
|
price: params.price
|
|
1014
1126
|
}];
|
|
1015
1127
|
};
|
|
1016
|
-
const
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1019
|
-
const rest = omit(item, ["item_price"]);
|
|
1020
|
-
const b1 = {
|
|
1021
|
-
item_id: itemIdOf(item),
|
|
1022
|
-
price,
|
|
1023
|
-
quantity
|
|
1024
|
-
};
|
|
1128
|
+
const contractItem = (item) => {
|
|
1129
|
+
const id = itemIdOf(item);
|
|
1130
|
+
if (!id) return void 0;
|
|
1025
1131
|
return compactPayload$1({
|
|
1026
|
-
|
|
1027
|
-
|
|
1132
|
+
id,
|
|
1133
|
+
name: textOf(item.item_name) ?? textOf(item.name),
|
|
1134
|
+
category: textOf(item.item_category) ?? textOf(item.category),
|
|
1135
|
+
brand: textOf(item.item_brand) ?? textOf(item.brand),
|
|
1136
|
+
price: toDecimalString(item.price ?? item.item_price),
|
|
1137
|
+
quantity: positiveDecimal(item.quantity)
|
|
1028
1138
|
});
|
|
1029
1139
|
};
|
|
1030
|
-
const
|
|
1031
|
-
|
|
1140
|
+
const contractItems = (params) => {
|
|
1141
|
+
return sourceItems(params).flatMap((item) => {
|
|
1142
|
+
const mapped = contractItem(item);
|
|
1143
|
+
return mapped ? [mapped] : [];
|
|
1144
|
+
});
|
|
1145
|
+
};
|
|
1146
|
+
/** `value`, else the sum of unit price × quantity (1 when missing). */
|
|
1147
|
+
const valueOf = (params, items) => {
|
|
1148
|
+
const explicit = toDecimalString(params.value);
|
|
1032
1149
|
if (explicit !== void 0) return explicit;
|
|
1033
1150
|
if (items.length === 0) return void 0;
|
|
1034
1151
|
let total = new Big(0);
|
|
1035
1152
|
for (const item of items) {
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
const quantity = quantityOf(item.quantity) ?? 1;
|
|
1039
|
-
total = total.plus(price.times(quantity));
|
|
1153
|
+
if (item.price === void 0) return void 0;
|
|
1154
|
+
total = total.plus(new Big(item.price).times(item.quantity ?? "1"));
|
|
1040
1155
|
}
|
|
1041
|
-
return total.
|
|
1156
|
+
return total.toFixed();
|
|
1042
1157
|
};
|
|
1043
|
-
const
|
|
1044
|
-
const
|
|
1045
|
-
|
|
1046
|
-
const
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1158
|
+
const userOf$1 = (user) => {
|
|
1159
|
+
const next = {};
|
|
1160
|
+
if (!user) return next;
|
|
1161
|
+
for (const key of HASHED_USER_KEYS) {
|
|
1162
|
+
const value = user[key];
|
|
1163
|
+
if (typeof value === "string" && value) next[HASHED_USER_WIRE_KEYS[key]] = value;
|
|
1164
|
+
}
|
|
1165
|
+
if (user.countryCode) next.country_code = user.countryCode;
|
|
1166
|
+
return next;
|
|
1167
|
+
};
|
|
1168
|
+
const clickIdsOf$1 = (clickIds) => {
|
|
1169
|
+
const next = {};
|
|
1170
|
+
if (!clickIds) return next;
|
|
1171
|
+
for (const key of CLICK_ID_KEYS$1) {
|
|
1172
|
+
const value = clickIds[key];
|
|
1173
|
+
if (typeof value === "string" && value) next[CLICK_ID_WIRE_KEYS[key]] = value;
|
|
1174
|
+
}
|
|
1175
|
+
return next;
|
|
1176
|
+
};
|
|
1177
|
+
/** Wire consent: `ad_user_data` and `ad_personalization` default to `ads`. */
|
|
1178
|
+
const resolveContractConsent = (consent) => {
|
|
1179
|
+
const { ads } = consent;
|
|
1180
|
+
return {
|
|
1181
|
+
ads,
|
|
1182
|
+
ad_user_data: consent.adUserData ?? ads,
|
|
1183
|
+
ad_personalization: consent.adPersonalization ?? ads
|
|
1051
1184
|
};
|
|
1052
|
-
|
|
1053
|
-
|
|
1185
|
+
};
|
|
1186
|
+
const contextOf$2 = (context) => {
|
|
1187
|
+
const c1 = {
|
|
1188
|
+
runtime: context.runtime,
|
|
1189
|
+
locale: context.locale
|
|
1190
|
+
};
|
|
1191
|
+
const c2 = {
|
|
1192
|
+
channel_id: context.channelId,
|
|
1193
|
+
store_id: context.storeId
|
|
1194
|
+
};
|
|
1195
|
+
const c3 = { brand_id: context.brandId };
|
|
1196
|
+
const c4 = { fulfillment_id: context.fulfillmentId };
|
|
1197
|
+
return compactPayload$1({
|
|
1198
|
+
...c1,
|
|
1199
|
+
...c2,
|
|
1200
|
+
...c3,
|
|
1201
|
+
...c4
|
|
1202
|
+
});
|
|
1203
|
+
};
|
|
1204
|
+
const propertiesOf$1 = (params) => {
|
|
1205
|
+
const kept = {};
|
|
1206
|
+
for (const [key, value] of Object.entries(params)) if (!LIFTED.has(key)) kept[key] = value;
|
|
1207
|
+
return compactPayload$1(stripContractPii(kept));
|
|
1208
|
+
};
|
|
1209
|
+
/**
|
|
1210
|
+
* One contract v1 event from a tracked event. Personal params are
|
|
1211
|
+
* dropped (pass hashed user data in `user`), money becomes decimal
|
|
1212
|
+
* strings without rounding, and `items` / `value` / `currency` follow the
|
|
1213
|
+
* same rules as the browser pixels.
|
|
1214
|
+
*/
|
|
1215
|
+
const buildTrackingEvent = (input) => {
|
|
1216
|
+
const params = isRecord(input.properties) ? input.properties : {};
|
|
1217
|
+
const items = contractItems(params);
|
|
1218
|
+
const value = valueOf(params, items);
|
|
1219
|
+
const priced = value !== void 0 || items.some((item) => item.price);
|
|
1220
|
+
const own = isoCurrencyCode(params.currency);
|
|
1221
|
+
const fallback = isoCurrencyCode(input.currency);
|
|
1222
|
+
const currency = priced ? own ?? fallback : void 0;
|
|
1223
|
+
const e1 = {
|
|
1224
|
+
event_id: input.eventId,
|
|
1225
|
+
name: input.name
|
|
1226
|
+
};
|
|
1227
|
+
const e2 = {
|
|
1228
|
+
timestamp: input.timestamp,
|
|
1229
|
+
source_url: input.sourceUrl
|
|
1230
|
+
};
|
|
1231
|
+
const e3 = { referrer_url: textOf(input.referrerUrl) };
|
|
1232
|
+
const e4 = {
|
|
1233
|
+
properties: propertiesOf$1(params),
|
|
1234
|
+
currency,
|
|
1235
|
+
value
|
|
1236
|
+
};
|
|
1237
|
+
const e5 = { items: items.length > 0 ? items : void 0 };
|
|
1238
|
+
const e6 = {
|
|
1239
|
+
order_id: textOf(params.order_id),
|
|
1240
|
+
user: userOf$1(input.user)
|
|
1241
|
+
};
|
|
1242
|
+
const e7 = { click_ids: clickIdsOf$1(input.clickIds) };
|
|
1243
|
+
const e8 = { consent: resolveContractConsent(input.consent) };
|
|
1244
|
+
const e9 = { providers: [...new Set(input.providers)] };
|
|
1245
|
+
const e10 = { context: contextOf$2(input.context) };
|
|
1246
|
+
return compactPayload$1({
|
|
1247
|
+
...e1,
|
|
1248
|
+
...e2,
|
|
1249
|
+
...e3,
|
|
1250
|
+
...e4,
|
|
1251
|
+
...e5,
|
|
1252
|
+
...e6,
|
|
1253
|
+
...e7,
|
|
1254
|
+
...e8,
|
|
1255
|
+
...e9,
|
|
1256
|
+
...e10
|
|
1054
1257
|
});
|
|
1258
|
+
};
|
|
1259
|
+
/** Wraps 1 to 20 events in a contract v1 envelope. */
|
|
1260
|
+
const buildTrackingEnvelope = (events, options = {}) => {
|
|
1261
|
+
if (events.length === 0 || events.length > 20) throw new RangeError(`An envelope carries 1 to 20 events; got ${events.length}`);
|
|
1262
|
+
const sent_at = new Date(options.now ?? Date.now()).toISOString();
|
|
1055
1263
|
return {
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1264
|
+
v: 1,
|
|
1265
|
+
sdk: {
|
|
1266
|
+
name: TRACKING_SDK_NAME,
|
|
1267
|
+
version: SDK_VERSION
|
|
1268
|
+
},
|
|
1269
|
+
sent_at,
|
|
1270
|
+
events: [...events]
|
|
1271
|
+
};
|
|
1272
|
+
};
|
|
1273
|
+
//#endregion
|
|
1274
|
+
//#region src/click-ids.ts
|
|
1275
|
+
const CLICK_ID_STORAGE_PREFIX = "fs_ck_";
|
|
1276
|
+
/** Click ids from a landing URL are kept for 90 days. */
|
|
1277
|
+
const CLICK_ID_TTL_MS = 2160 * 60 * 60 * 1e3;
|
|
1278
|
+
const STORED_KEYS = [
|
|
1279
|
+
"fbc",
|
|
1280
|
+
"ttclid",
|
|
1281
|
+
"gclid",
|
|
1282
|
+
"gbraid",
|
|
1283
|
+
"wbraid"
|
|
1284
|
+
];
|
|
1285
|
+
const URL_PARAMS = [
|
|
1286
|
+
"fbclid",
|
|
1287
|
+
"ttclid",
|
|
1288
|
+
"gclid",
|
|
1289
|
+
"gbraid",
|
|
1290
|
+
"wbraid"
|
|
1291
|
+
];
|
|
1292
|
+
const VALUE = /^[\x21-\x7E]{1,500}$/;
|
|
1293
|
+
const SESSION_ATTRIBUTES$1 = /^[\x21-\x7E]{1,4096}$/;
|
|
1294
|
+
const FBC = /^fb\.\d+\.\d+\.[\x21-\x7E]+$/;
|
|
1295
|
+
const FBP$1 = /^fb\.\d+\.\d+\.\d+$/;
|
|
1296
|
+
const GCL_AW = /^GCL\.\d+\.([\x21-\x7E]+)$/;
|
|
1297
|
+
const clean = (value, pattern = VALUE) => {
|
|
1298
|
+
const trimmed = value?.trim();
|
|
1299
|
+
return trimmed && pattern.test(trimmed) ? trimmed : void 0;
|
|
1300
|
+
};
|
|
1301
|
+
/** Value of cookie `name` in a `document.cookie` string. */
|
|
1302
|
+
const readCookie = (cookie, name) => {
|
|
1303
|
+
for (const part of cookie.split(";")) {
|
|
1304
|
+
const index = part.indexOf("=");
|
|
1305
|
+
if (index < 0 || part.slice(0, index).trim() !== name) continue;
|
|
1306
|
+
const raw = part.slice(index + 1).trim();
|
|
1307
|
+
try {
|
|
1308
|
+
return decodeURIComponent(raw);
|
|
1309
|
+
} catch {
|
|
1310
|
+
return raw;
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
};
|
|
1314
|
+
/**
|
|
1315
|
+
* Meta `fbc` from a `fbclid` URL param:
|
|
1316
|
+
* `fb.<subdomainIndex>.<creationTimeMs>.<fbclid>`. The fbclid keeps its
|
|
1317
|
+
* case.
|
|
1318
|
+
*/
|
|
1319
|
+
const fbcFromFbclid = (fbclid, creationTimeMs, subdomainIndex = 1) => {
|
|
1320
|
+
return `fb.${subdomainIndex}.${Math.trunc(creationTimeMs)}.${fbclid}`;
|
|
1321
|
+
};
|
|
1322
|
+
/** The gclid inside a Google `_gcl_aw` cookie (`GCL.<time>.<gclid>`). */
|
|
1323
|
+
const gclidFromGclAw = (value) => {
|
|
1324
|
+
return clean(GCL_AW.exec(value.trim())?.[1]);
|
|
1325
|
+
};
|
|
1326
|
+
/** `fbclid`, `ttclid`, `gclid`, `gbraid` and `wbraid` of a URL. */
|
|
1327
|
+
const parseClickIdParams = (url) => {
|
|
1328
|
+
const found = {};
|
|
1329
|
+
let params;
|
|
1330
|
+
try {
|
|
1331
|
+
params = new URL(url).searchParams;
|
|
1332
|
+
} catch {
|
|
1333
|
+
return found;
|
|
1334
|
+
}
|
|
1335
|
+
for (const name of URL_PARAMS) {
|
|
1336
|
+
const value = clean(params.get(name));
|
|
1337
|
+
if (value) found[name] = value;
|
|
1338
|
+
}
|
|
1339
|
+
return found;
|
|
1340
|
+
};
|
|
1341
|
+
const keyOf = (key) => `${CLICK_ID_STORAGE_PREFIX}${key}`;
|
|
1342
|
+
const readStored = async (storage, key, now) => {
|
|
1343
|
+
const raw = await storage.getItem(keyOf(key));
|
|
1344
|
+
if (!raw) return void 0;
|
|
1345
|
+
try {
|
|
1346
|
+
const parsed = JSON.parse(raw);
|
|
1347
|
+
const { value, expires_at } = isRecord(parsed) ? parsed : {};
|
|
1348
|
+
const id = typeof expires_at === "number" && expires_at > now && typeof value === "string" ? clean(value) : void 0;
|
|
1349
|
+
if (id) return id;
|
|
1350
|
+
} catch {}
|
|
1351
|
+
await storage.removeItem(keyOf(key));
|
|
1352
|
+
};
|
|
1353
|
+
/** Saves fresh ids and reads the stored ones. Storage errors are ignored. */
|
|
1354
|
+
const syncStorage = async (storage, fresh, now) => {
|
|
1355
|
+
const stored = {};
|
|
1356
|
+
const expires_at = now + CLICK_ID_TTL_MS;
|
|
1357
|
+
for (const key of STORED_KEYS) try {
|
|
1358
|
+
const value = fresh[key];
|
|
1359
|
+
if (value) {
|
|
1360
|
+
const entry = JSON.stringify({
|
|
1361
|
+
value,
|
|
1362
|
+
expires_at
|
|
1363
|
+
});
|
|
1364
|
+
await storage.setItem(keyOf(key), entry);
|
|
1365
|
+
continue;
|
|
1366
|
+
}
|
|
1367
|
+
stored[key] = await readStored(storage, key, now);
|
|
1368
|
+
} catch {}
|
|
1369
|
+
return stored;
|
|
1370
|
+
};
|
|
1371
|
+
const pageUrl = () => {
|
|
1372
|
+
return globalThis.location?.href;
|
|
1373
|
+
};
|
|
1374
|
+
const pageCookie = () => {
|
|
1375
|
+
const root = globalThis;
|
|
1376
|
+
try {
|
|
1377
|
+
return root.document?.cookie ?? "";
|
|
1378
|
+
} catch {
|
|
1379
|
+
return "";
|
|
1380
|
+
}
|
|
1381
|
+
};
|
|
1382
|
+
/** A new fbclid wins over `_fbc` unless that cookie was built from it. */
|
|
1383
|
+
const freshFbc = (fbclid, cookie, now) => {
|
|
1384
|
+
if (!fbclid) return void 0;
|
|
1385
|
+
if (cookie?.endsWith(`.${fbclid}`)) return cookie;
|
|
1386
|
+
return fbcFromFbclid(fbclid, now);
|
|
1387
|
+
};
|
|
1388
|
+
const withoutEmpty = (ids) => {
|
|
1389
|
+
const next = {};
|
|
1390
|
+
for (const [key, value] of Object.entries(ids)) if (value) next[key] = value;
|
|
1391
|
+
return next;
|
|
1392
|
+
};
|
|
1393
|
+
/**
|
|
1394
|
+
* Reads ad click ids for the server relay. Without ads consent it returns
|
|
1395
|
+
* `{}` and reads or writes nothing.
|
|
1396
|
+
*
|
|
1397
|
+
* - `fbc`: a `fbclid` in the URL, else the `_fbc` cookie, else storage.
|
|
1398
|
+
* - `fbp`: the `_fbp` cookie set by the Meta pixel.
|
|
1399
|
+
* - `ttclid`, `gbraid`, `wbraid`: the URL, else storage.
|
|
1400
|
+
* - `ttp`: the `_ttp` cookie set by the TikTok pixel (read only).
|
|
1401
|
+
* - `gclid`: the URL, else the `_gcl_aw` cookie, else storage.
|
|
1402
|
+
*
|
|
1403
|
+
* With `storage`, ids found in the URL are kept for 90 days under
|
|
1404
|
+
* `fs_ck_*` keys so a later page (the checkout) still has them.
|
|
1405
|
+
*/
|
|
1406
|
+
const captureClickIds = async (options) => {
|
|
1407
|
+
if (!options.consent.ads) return {};
|
|
1408
|
+
const now = options.now ?? Date.now();
|
|
1409
|
+
const url = options.url ?? pageUrl();
|
|
1410
|
+
const cookie = options.cookie ?? pageCookie();
|
|
1411
|
+
const fromUrl = url ? parseClickIdParams(url) : {};
|
|
1412
|
+
const fbcCookie = clean(readCookie(cookie, "_fbc"), FBC);
|
|
1413
|
+
const fresh = {
|
|
1414
|
+
fbc: freshFbc(fromUrl.fbclid, fbcCookie, now),
|
|
1415
|
+
ttclid: fromUrl.ttclid,
|
|
1416
|
+
gclid: fromUrl.gclid,
|
|
1417
|
+
gbraid: fromUrl.gbraid,
|
|
1418
|
+
wbraid: fromUrl.wbraid
|
|
1419
|
+
};
|
|
1420
|
+
const storage = options.storage;
|
|
1421
|
+
const stored = storage ? await syncStorage(storage, fresh, now) : {};
|
|
1422
|
+
const gclAw = readCookie(cookie, "_gcl_aw");
|
|
1423
|
+
const b1 = { fbc: fresh.fbc ?? fbcCookie ?? stored.fbc };
|
|
1424
|
+
const b2 = { fbp: clean(readCookie(cookie, "_fbp"), FBP$1) };
|
|
1425
|
+
const b3 = { ttclid: fresh.ttclid ?? stored.ttclid };
|
|
1426
|
+
const b4 = { ttp: clean(readCookie(cookie, "_ttp")) };
|
|
1427
|
+
const b5 = { gclid: fresh.gclid ?? (gclAw ? gclidFromGclAw(gclAw) : void 0) ?? stored.gclid };
|
|
1428
|
+
const b6 = { gbraid: fresh.gbraid ?? stored.gbraid };
|
|
1429
|
+
const b7 = { wbraid: fresh.wbraid ?? stored.wbraid };
|
|
1430
|
+
const session = options.googleSessionAttributes;
|
|
1431
|
+
const b8 = { googleSessionAttributes: clean(session, SESSION_ATTRIBUTES$1) };
|
|
1432
|
+
const m1 = {
|
|
1433
|
+
...b1,
|
|
1434
|
+
...b2,
|
|
1435
|
+
...b3,
|
|
1436
|
+
...b4,
|
|
1437
|
+
...b5,
|
|
1438
|
+
...b6,
|
|
1439
|
+
...b7
|
|
1440
|
+
};
|
|
1441
|
+
return withoutEmpty({
|
|
1442
|
+
...m1,
|
|
1443
|
+
...b8
|
|
1444
|
+
});
|
|
1445
|
+
};
|
|
1446
|
+
/** Removes the `fs_ck_*` entries, for example when ads consent is revoked. */
|
|
1447
|
+
const clearClickIds = async (storage) => {
|
|
1448
|
+
for (const key of STORED_KEYS) await storage.removeItem(keyOf(key));
|
|
1449
|
+
};
|
|
1450
|
+
const intlContext = () => {
|
|
1451
|
+
try {
|
|
1452
|
+
const options = Intl.DateTimeFormat().resolvedOptions();
|
|
1453
|
+
return {
|
|
1454
|
+
locale: options.locale,
|
|
1455
|
+
timezone: options.timeZone
|
|
1456
|
+
};
|
|
1457
|
+
} catch {
|
|
1458
|
+
return {};
|
|
1459
|
+
}
|
|
1460
|
+
};
|
|
1461
|
+
const compact = (context) => {
|
|
1462
|
+
const next = {};
|
|
1463
|
+
for (const [key, raw] of Object.entries(context)) {
|
|
1464
|
+
if (typeof raw !== "string") continue;
|
|
1465
|
+
const value = raw.trim().slice(0, 64);
|
|
1466
|
+
if (value) next[key] = value;
|
|
1467
|
+
}
|
|
1468
|
+
return next;
|
|
1469
|
+
};
|
|
1470
|
+
const buildEventContext = (context) => {
|
|
1471
|
+
const merged = {
|
|
1472
|
+
...intlContext(),
|
|
1473
|
+
...context
|
|
1474
|
+
};
|
|
1475
|
+
return {
|
|
1476
|
+
...compact(merged),
|
|
1477
|
+
sdk_version: SDK_VERSION
|
|
1478
|
+
};
|
|
1479
|
+
};
|
|
1480
|
+
//#endregion
|
|
1481
|
+
//#region src/cookies.ts
|
|
1482
|
+
/** First-party cookies the SDK writes in `server` mode live 90 days. */
|
|
1483
|
+
const FB_COOKIE_TTL_MS = 2160 * 60 * 60 * 1e3;
|
|
1484
|
+
const LOCAL_HOSTS$2 = /* @__PURE__ */ new Set(["localhost", "127.0.0.1"]);
|
|
1485
|
+
const IPV4 = /^\d{1,3}(\.\d{1,3}){3}$/;
|
|
1486
|
+
const FBP = /^fb\.\d+\.\d+\.\d+$/;
|
|
1487
|
+
const root = () => {
|
|
1488
|
+
return globalThis;
|
|
1489
|
+
};
|
|
1490
|
+
const getDocument = () => {
|
|
1491
|
+
try {
|
|
1492
|
+
const doc = root().document;
|
|
1493
|
+
return doc && typeof doc.cookie === "string" ? doc : void 0;
|
|
1494
|
+
} catch {
|
|
1495
|
+
return;
|
|
1496
|
+
}
|
|
1497
|
+
};
|
|
1498
|
+
const getCookie = () => getDocument()?.cookie ?? "";
|
|
1499
|
+
/** eTLD+1 best effort: the last two labels, never for an IP or localhost. */
|
|
1500
|
+
const cookieDomain = (hostname) => {
|
|
1501
|
+
if (!hostname || LOCAL_HOSTS$2.has(hostname) || IPV4.test(hostname)) return;
|
|
1502
|
+
const labels = hostname.split(".");
|
|
1503
|
+
if (labels.length < 2) return void 0;
|
|
1504
|
+
return labels.slice(-2).join(".");
|
|
1505
|
+
};
|
|
1506
|
+
/** 10 random digits for the `_fbp` random segment. */
|
|
1507
|
+
const randomDigits = (count) => {
|
|
1508
|
+
const c = globalThis.crypto;
|
|
1509
|
+
let out = "";
|
|
1510
|
+
if (typeof c?.getRandomValues === "function") {
|
|
1511
|
+
const bytes = new Uint8Array(count);
|
|
1512
|
+
c.getRandomValues(bytes);
|
|
1513
|
+
for (const byte of bytes) out += String(byte % 10);
|
|
1514
|
+
return out;
|
|
1515
|
+
}
|
|
1516
|
+
for (let i = 0; i < count; i += 1) out += String(Math.floor(Math.random() * 10));
|
|
1517
|
+
return out;
|
|
1518
|
+
};
|
|
1519
|
+
/** `fb.1.<creationTimeMs>.<10 random digits>`, the Meta `_fbp` shape. */
|
|
1520
|
+
const newFbp = (creationTimeMs) => {
|
|
1521
|
+
return `fb.1.${Math.trunc(creationTimeMs)}.${randomDigits(10)}`;
|
|
1522
|
+
};
|
|
1523
|
+
const serialize = (name, value, maxAgeMs) => {
|
|
1524
|
+
const location = root().location;
|
|
1525
|
+
const secure = location?.protocol === "https:";
|
|
1526
|
+
const domain = cookieDomain(location?.hostname);
|
|
1527
|
+
const parts = [
|
|
1528
|
+
`${name}=${encodeURIComponent(value)}`,
|
|
1529
|
+
"Path=/",
|
|
1530
|
+
`Max-Age=${Math.trunc(maxAgeMs / 1e3)}`,
|
|
1531
|
+
"SameSite=Lax"
|
|
1532
|
+
];
|
|
1533
|
+
if (domain) parts.push(`Domain=.${domain}`);
|
|
1534
|
+
if (secure) parts.push("Secure");
|
|
1535
|
+
return parts.join("; ");
|
|
1536
|
+
};
|
|
1537
|
+
/** Writes a cookie through `document.cookie`. No document: a no-op. */
|
|
1538
|
+
const writeCookie = (name, value, maxAgeMs) => {
|
|
1539
|
+
const doc = getDocument();
|
|
1540
|
+
if (!doc) return;
|
|
1541
|
+
doc.cookie = serialize(name, value, maxAgeMs);
|
|
1542
|
+
};
|
|
1543
|
+
/** Expires a cookie (host-only and on the eTLD+1). No document: a no-op. */
|
|
1544
|
+
const deleteCookie = (name) => {
|
|
1545
|
+
const doc = getDocument();
|
|
1546
|
+
if (!doc) return;
|
|
1547
|
+
const location = root().location;
|
|
1548
|
+
const domain = cookieDomain(location?.hostname);
|
|
1549
|
+
const base = `${name}=; Path=/; Max-Age=0; SameSite=Lax`;
|
|
1550
|
+
doc.cookie = base;
|
|
1551
|
+
if (domain) doc.cookie = `${base}; Domain=.${domain}`;
|
|
1552
|
+
};
|
|
1553
|
+
/**
|
|
1554
|
+
* In `server` mode the pixel scripts never run, so the SDK writes the
|
|
1555
|
+
* first-party cookies they would. Creates `_fbp` when it is missing and
|
|
1556
|
+
* `_fbc` from a `fbclid` in the URL. `_ttp` cannot be generated.
|
|
1557
|
+
*/
|
|
1558
|
+
const ensureFbCookies = (now) => {
|
|
1559
|
+
const cookie = getCookie();
|
|
1560
|
+
if (!FBP.test(readCookie(cookie, "_fbp") ?? "")) writeCookie("_fbp", newFbp(now), FB_COOKIE_TTL_MS);
|
|
1561
|
+
const existingFbc = readCookie(cookie, "_fbc");
|
|
1562
|
+
const { fbclid } = parseClickIdParams(root().location?.href ?? "");
|
|
1563
|
+
if (fbclid && !existingFbc?.endsWith(`.${fbclid}`) && fbclid) writeCookie("_fbc", fbcFromFbclid(fbclid, now), FB_COOKIE_TTL_MS);
|
|
1564
|
+
};
|
|
1565
|
+
/** Deletes the `_fbp` and `_fbc` cookies the SDK wrote (consent revoke). */
|
|
1566
|
+
const deleteFbCookies = () => {
|
|
1567
|
+
deleteCookie("_fbp");
|
|
1568
|
+
deleteCookie("_fbc");
|
|
1569
|
+
};
|
|
1570
|
+
//#endregion
|
|
1571
|
+
//#region src/report.ts
|
|
1572
|
+
const PREFIX = "[@fire-spark/analytics]";
|
|
1573
|
+
const logMasivo = (issue) => {
|
|
1574
|
+
try {
|
|
1575
|
+
const opts = {
|
|
1576
|
+
alias: "masivo.destination",
|
|
1577
|
+
detail: issue.event
|
|
1578
|
+
};
|
|
1579
|
+
const logged = logError(issue.error ?? issue.message, opts);
|
|
1580
|
+
Promise.resolve(logged).catch((failure) => {
|
|
1581
|
+
console.error(`${PREFIX} Masivo error was not logged`, failure);
|
|
1582
|
+
});
|
|
1583
|
+
} catch (failure) {
|
|
1584
|
+
console.error(`${PREFIX} Masivo error was not logged`, failure);
|
|
1585
|
+
}
|
|
1586
|
+
};
|
|
1587
|
+
const callHook = (hook, issue) => {
|
|
1588
|
+
try {
|
|
1589
|
+
hook(issue);
|
|
1590
|
+
} catch (failure) {
|
|
1591
|
+
console.error(`${PREFIX} onError/onWarning threw`, failure);
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
/** Never throws: a failing reporter must not break tracking. */
|
|
1595
|
+
const report = (rt, issue) => {
|
|
1596
|
+
const error = issue.level === "error";
|
|
1597
|
+
if (error && issue.provider === "masivo") logMasivo(issue);
|
|
1598
|
+
const hook = error ? rt.options?.onError : rt.options?.onWarning;
|
|
1599
|
+
if (hook) {
|
|
1600
|
+
callHook(hook, issue);
|
|
1601
|
+
return;
|
|
1602
|
+
}
|
|
1603
|
+
const detail = {
|
|
1604
|
+
provider: issue.provider,
|
|
1605
|
+
event: issue.event
|
|
1060
1606
|
};
|
|
1607
|
+
const args = [
|
|
1608
|
+
`${PREFIX} ${issue.message}`,
|
|
1609
|
+
detail,
|
|
1610
|
+
issue.error
|
|
1611
|
+
];
|
|
1612
|
+
if (error) console.error(...args);
|
|
1613
|
+
else console.warn(...args);
|
|
1614
|
+
};
|
|
1615
|
+
const errorMessage = (error) => {
|
|
1616
|
+
if (error instanceof Error) return error.message;
|
|
1617
|
+
return String(error);
|
|
1618
|
+
};
|
|
1619
|
+
//#endregion
|
|
1620
|
+
//#region src/types.ts
|
|
1621
|
+
const FUNNEL_EVENTS = [
|
|
1622
|
+
"menu_viewed",
|
|
1623
|
+
"product_viewed",
|
|
1624
|
+
"add_to_cart",
|
|
1625
|
+
"purchase"
|
|
1626
|
+
];
|
|
1627
|
+
const INTERACTION_EVENTS = ["product_visited", "category_visited"];
|
|
1628
|
+
const BUILTIN_DESTINATION_IDS = [
|
|
1629
|
+
"ga4",
|
|
1630
|
+
"meta",
|
|
1631
|
+
"masivo",
|
|
1632
|
+
"tiktok"
|
|
1633
|
+
];
|
|
1634
|
+
/**
|
|
1635
|
+
* Thrown by `resolveDeliveryMode()` for an invalid `mode` / `serverSide`.
|
|
1636
|
+
* In development `configure()` lets it throw; in production (`env:
|
|
1637
|
+
* "production"`) it is caught, reported through `onWarning` and the client
|
|
1638
|
+
* falls back to `mode: "client"` so the storefront never crashes.
|
|
1639
|
+
*/
|
|
1640
|
+
var AnalyticsConfigError = class extends Error {
|
|
1641
|
+
code;
|
|
1642
|
+
constructor(code, message) {
|
|
1643
|
+
super(message);
|
|
1644
|
+
this.name = "AnalyticsConfigError";
|
|
1645
|
+
this.code = code;
|
|
1646
|
+
}
|
|
1061
1647
|
};
|
|
1062
|
-
const
|
|
1063
|
-
|
|
1064
|
-
return shape.items.some((item) => item.price !== void 0);
|
|
1648
|
+
const isFunnelEventName = (name) => {
|
|
1649
|
+
return FUNNEL_EVENTS.includes(name);
|
|
1065
1650
|
};
|
|
1066
|
-
const
|
|
1067
|
-
return
|
|
1651
|
+
const isInteractionEventName = (name) => {
|
|
1652
|
+
return INTERACTION_EVENTS.includes(name);
|
|
1068
1653
|
};
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
medios_view_item_delivery: "UnlockAchievement",
|
|
1075
|
-
medios_view_item_express: "StartTrial",
|
|
1076
|
-
medios_purchase_delivery: "Subscribe",
|
|
1077
|
-
medios_purchase_express: "GenerateLead"
|
|
1654
|
+
//#endregion
|
|
1655
|
+
//#region src/custom.ts
|
|
1656
|
+
const BUILTIN_IDS = new Set(BUILTIN_DESTINATION_IDS);
|
|
1657
|
+
const isBuiltinDestination = (id) => {
|
|
1658
|
+
return BUILTIN_IDS.has(id);
|
|
1078
1659
|
};
|
|
1079
|
-
const
|
|
1080
|
-
|
|
1081
|
-
if (provider === "tiktok" && alias) return alias;
|
|
1082
|
-
return destinationEventName(input.name, provider, input.runtime);
|
|
1660
|
+
const isCustomAdapter = (adapter) => {
|
|
1661
|
+
return "kind" in adapter && adapter.kind === "custom";
|
|
1083
1662
|
};
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1663
|
+
/**
|
|
1664
|
+
* Decimal token check. The original string or number is what callers
|
|
1665
|
+
* receive; Big.js never rewrites it (no `toFixed`, no `toString`).
|
|
1666
|
+
*/
|
|
1667
|
+
const decimalAmount = (amount) => {
|
|
1668
|
+
if (typeof amount === "number") {
|
|
1669
|
+
if (!Number.isFinite(amount)) return void 0;
|
|
1670
|
+
return amount;
|
|
1671
|
+
}
|
|
1672
|
+
if (typeof amount !== "string") return void 0;
|
|
1673
|
+
const trimmed = amount.trim();
|
|
1674
|
+
if (!trimmed) return void 0;
|
|
1675
|
+
try {
|
|
1676
|
+
new Big(trimmed);
|
|
1677
|
+
} catch {
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
return trimmed;
|
|
1093
1681
|
};
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
if (!
|
|
1097
|
-
|
|
1098
|
-
const
|
|
1099
|
-
|
|
1100
|
-
const
|
|
1101
|
-
|
|
1102
|
-
|
|
1682
|
+
/** Revenue copied onto the custom event, or `undefined` when it is unusable. */
|
|
1683
|
+
const forwardRevenue = (revenue) => {
|
|
1684
|
+
if (!revenue) return void 0;
|
|
1685
|
+
if (typeof revenue.currency !== "string") return void 0;
|
|
1686
|
+
const currency = revenue.currency.trim();
|
|
1687
|
+
if (!currency) return void 0;
|
|
1688
|
+
const amount = decimalAmount(revenue.amount);
|
|
1689
|
+
if (amount === void 0) return void 0;
|
|
1690
|
+
return {
|
|
1103
1691
|
currency,
|
|
1104
|
-
|
|
1692
|
+
amount
|
|
1105
1693
|
};
|
|
1106
|
-
return compactPayload$1({
|
|
1107
|
-
...base,
|
|
1108
|
-
...b1
|
|
1109
|
-
});
|
|
1110
1694
|
};
|
|
1111
|
-
const
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
const external_id = input.identity?.hashedUserId;
|
|
1118
|
-
const data = compactPayload$1({
|
|
1119
|
-
...b1,
|
|
1120
|
-
external_id
|
|
1121
|
-
});
|
|
1122
|
-
const entries = Object.entries(data).filter(([, value]) => {
|
|
1123
|
-
return typeof value === "string" && value.length > 0;
|
|
1695
|
+
const warn$1 = (rt, source, message, provider) => {
|
|
1696
|
+
report(rt, {
|
|
1697
|
+
level: "warning",
|
|
1698
|
+
source,
|
|
1699
|
+
message,
|
|
1700
|
+
provider
|
|
1124
1701
|
});
|
|
1125
|
-
if (entries.length === 0) return void 0;
|
|
1126
|
-
return Object.fromEntries(entries);
|
|
1127
|
-
};
|
|
1128
|
-
const numItems = (items) => {
|
|
1129
|
-
let total = new Big(0);
|
|
1130
|
-
for (const item of items) total = total.plus(quantityOf(item.quantity) ?? 1);
|
|
1131
|
-
return total.toNumber();
|
|
1132
1702
|
};
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1703
|
+
/**
|
|
1704
|
+
* Drops custom destinations whose id is empty, reserved or repeated, and
|
|
1705
|
+
* plain adapters whose id is not a built-in. Built-in adapters are kept,
|
|
1706
|
+
* including two adapters that share a built-in id.
|
|
1707
|
+
*/
|
|
1708
|
+
const acceptDestinations = (rt, adapters, source, seen) => {
|
|
1709
|
+
const taken = new Set(seen);
|
|
1710
|
+
const accepted = [];
|
|
1711
|
+
for (const adapter of adapters) {
|
|
1712
|
+
if (!isCustomAdapter(adapter)) {
|
|
1713
|
+
const id = adapter.provider;
|
|
1714
|
+
if (!isBuiltinDestination(id)) {
|
|
1715
|
+
const message = `${`Destination "${id}" is not built in.`} Use customAdapter(). Skipped`;
|
|
1716
|
+
warn$1(rt, source, message, id);
|
|
1717
|
+
continue;
|
|
1718
|
+
}
|
|
1719
|
+
taken.add(id);
|
|
1720
|
+
accepted.push(adapter);
|
|
1721
|
+
continue;
|
|
1722
|
+
}
|
|
1723
|
+
const id = adapter.provider.trim();
|
|
1724
|
+
if (!id) {
|
|
1725
|
+
warn$1(rt, source, "Custom destination id is empty; skipped");
|
|
1726
|
+
continue;
|
|
1727
|
+
}
|
|
1728
|
+
const label = `Custom destination "${id}"`;
|
|
1729
|
+
if (isBuiltinDestination(id)) {
|
|
1730
|
+
const message = `${label} collides with a built-in id; skipped`;
|
|
1731
|
+
warn$1(rt, source, message, id);
|
|
1732
|
+
continue;
|
|
1733
|
+
}
|
|
1734
|
+
if (taken.has(id)) {
|
|
1735
|
+
const message = `${label} is already registered; skipped`;
|
|
1736
|
+
warn$1(rt, source, message, id);
|
|
1737
|
+
continue;
|
|
1738
|
+
}
|
|
1739
|
+
taken.add(id);
|
|
1740
|
+
if (adapter.provider === id) {
|
|
1741
|
+
accepted.push(adapter);
|
|
1742
|
+
continue;
|
|
1743
|
+
}
|
|
1744
|
+
accepted.push({
|
|
1745
|
+
...adapter,
|
|
1746
|
+
provider: id
|
|
1142
1747
|
});
|
|
1143
|
-
}
|
|
1144
|
-
|
|
1145
|
-
const flatValue = (value) => {
|
|
1146
|
-
if (typeof value === "string" || typeof value === "number") return value;
|
|
1147
|
-
if (typeof value === "boolean") return String(value);
|
|
1148
|
-
if (value === void 0 || value === null) return void 0;
|
|
1149
|
-
return JSON.stringify(value);
|
|
1150
|
-
};
|
|
1151
|
-
/** App events params must be strings or numbers. */
|
|
1152
|
-
const flatParams = (params) => {
|
|
1153
|
-
const next = {};
|
|
1154
|
-
for (const [key, value] of Object.entries(compactPayload$1(params))) next[key] = flatValue(value);
|
|
1155
|
-
return compactPayload$1(next);
|
|
1748
|
+
}
|
|
1749
|
+
return accepted;
|
|
1156
1750
|
};
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1751
|
+
/**
|
|
1752
|
+
* Destination the app owns. The id is any string except `ga4`, `meta`,
|
|
1753
|
+
* `masivo` and `tiktok`. A collision or a repeated id is reported with
|
|
1754
|
+
* `onWarning` (`source: "configure"`, or `"track"` for `extra`) and that
|
|
1755
|
+
* adapter is skipped. `consent` selects the gate. `"ads"` hashes
|
|
1756
|
+
* `email`, `phone`, `device_ip`, `ttclid` and the user id the same way
|
|
1757
|
+
* Meta and TikTok do. `map` receives the raw tracked event (no catalog
|
|
1758
|
+
* rename) and returns the payload `send` delivers, or `null` to drop it.
|
|
1759
|
+
* Without `map`, `send` receives the tracked name and properties.
|
|
1760
|
+
* `revenue.amount` is forwarded unchanged.
|
|
1761
|
+
*
|
|
1762
|
+
* The app supplies the native SDK. This package does not include one.
|
|
1763
|
+
* Installs, ATT prompts and deep links stay in that SDK and its Expo
|
|
1764
|
+
* config plugins.
|
|
1765
|
+
*
|
|
1766
|
+
* @example Singular-style mapping. The SDK below is the app's own; nothing
|
|
1767
|
+
* here imports Singular.
|
|
1768
|
+
* ```ts
|
|
1769
|
+
* import { customAdapter } from "@fire-spark/analytics";
|
|
1770
|
+
* import type { CustomDestinationEvent } from "@fire-spark/analytics";
|
|
1771
|
+
* import type { CustomDestinationInput } from "@fire-spark/analytics";
|
|
1772
|
+
*
|
|
1773
|
+
* type RevenueSdk = {
|
|
1774
|
+
* eventWithArgs: (name: string, args: Record<string, unknown>) => void;
|
|
1775
|
+
* customRevenueWithArgs: (
|
|
1776
|
+
* name: string,
|
|
1777
|
+
* currency: string,
|
|
1778
|
+
* amount: string | number,
|
|
1779
|
+
* args: Record<string, unknown>
|
|
1780
|
+
* ) => void;
|
|
1781
|
+
* };
|
|
1782
|
+
*
|
|
1783
|
+
* const SINGULAR_EVENTS: Record<string, string> = {
|
|
1784
|
+
* sign_up: "sng_complete_registration",
|
|
1785
|
+
* search: "sng_search",
|
|
1786
|
+
* product_viewed: "sng_content_view",
|
|
1787
|
+
* add_to_cart: "sng_add_to_cart",
|
|
1788
|
+
* view_cart: "sng_view_cart",
|
|
1789
|
+
* begin_checkout: "sng_checkout_initiated",
|
|
1790
|
+
* add_payment_info: "sng_add_payment_info",
|
|
1791
|
+
* subscribe: "sng_subscribe"
|
|
1792
|
+
* };
|
|
1793
|
+
*
|
|
1794
|
+
* const singularAdapter = (sdk: RevenueSdk) => {
|
|
1795
|
+
* const map = (event: CustomDestinationInput) => {
|
|
1796
|
+
* if (event.name === "purchase") {
|
|
1797
|
+
* const total = event.properties.total ?? event.properties.value;
|
|
1798
|
+
* if (typeof total !== "string" && typeof total !== "number") {
|
|
1799
|
+
* return null;
|
|
1800
|
+
* }
|
|
1801
|
+
* if (!event.currency) return null;
|
|
1802
|
+
* const first = event.properties.first_purchase === true;
|
|
1803
|
+
* const repeatName = "sng_ecommerce_purchase";
|
|
1804
|
+
* const firstName = "sng_ecommerce_firstpurchase";
|
|
1805
|
+
* const name = first ? firstName : repeatName;
|
|
1806
|
+
* const params = { order_id: event.properties.order_id };
|
|
1807
|
+
* const revenue = { currency: event.currency, amount: total };
|
|
1808
|
+
* return { name, params, revenue };
|
|
1809
|
+
* }
|
|
1810
|
+
* const name = SINGULAR_EVENTS[event.name];
|
|
1811
|
+
* if (!name) return null;
|
|
1812
|
+
* return { name, params: event.properties };
|
|
1813
|
+
* };
|
|
1814
|
+
* const send = (event: CustomDestinationEvent) => {
|
|
1815
|
+
* const revenue = event.revenue;
|
|
1816
|
+
* if (!revenue) {
|
|
1817
|
+
* sdk.eventWithArgs(event.name, event.params);
|
|
1818
|
+
* return;
|
|
1819
|
+
* }
|
|
1820
|
+
* sdk.customRevenueWithArgs(
|
|
1821
|
+
* event.name,
|
|
1822
|
+
* revenue.currency,
|
|
1823
|
+
* revenue.amount,
|
|
1824
|
+
* event.params
|
|
1825
|
+
* );
|
|
1826
|
+
* };
|
|
1827
|
+
* return customAdapter({ id: "singular", consent: "ads", map, send });
|
|
1828
|
+
* };
|
|
1829
|
+
* ```
|
|
1830
|
+
*/
|
|
1831
|
+
const customAdapter = (options) => {
|
|
1164
1832
|
const b1 = {
|
|
1165
|
-
|
|
1166
|
-
|
|
1833
|
+
kind: "custom",
|
|
1834
|
+
provider: options.id.trim()
|
|
1167
1835
|
};
|
|
1168
1836
|
const b2 = {
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
};
|
|
1172
|
-
const b3 = { content_type: listed ? "product" : void 0 };
|
|
1173
|
-
const num_items = listed ? numItems(shape.items) : void 0;
|
|
1174
|
-
return compactPayload$1({
|
|
1175
|
-
...base,
|
|
1176
|
-
...b1,
|
|
1177
|
-
...b2,
|
|
1178
|
-
...b3,
|
|
1179
|
-
num_items
|
|
1180
|
-
});
|
|
1181
|
-
};
|
|
1182
|
-
const metaNativePayload = (input, shape) => {
|
|
1183
|
-
const base = baseParams(input, shape);
|
|
1184
|
-
if (!shape.ecommerce) return flatParams(base);
|
|
1185
|
-
const contents = metaContents(shape.items);
|
|
1186
|
-
const listed = contents.length > 0;
|
|
1187
|
-
const ids = contents.map((row) => row.id);
|
|
1188
|
-
const fb_content_id = ids.length === 1 ? ids[0] : JSON.stringify(ids);
|
|
1189
|
-
const b1 = { fb_currency: hasMoney(shape) ? shape.currency : void 0 };
|
|
1190
|
-
const b2 = { fb_content: listed ? JSON.stringify(contents) : void 0 };
|
|
1191
|
-
const b3 = { fb_content_id: listed ? fb_content_id : void 0 };
|
|
1192
|
-
const b4 = { fb_content_type: listed ? "product" : void 0 };
|
|
1193
|
-
const b5 = { fb_num_items: listed ? numItems(shape.items) : void 0 };
|
|
1194
|
-
const b6 = { fb_order_id: orderIdOf(input.params) };
|
|
1195
|
-
return flatParams({
|
|
1196
|
-
...base,
|
|
1197
|
-
...b1,
|
|
1198
|
-
...b2,
|
|
1199
|
-
...b3,
|
|
1200
|
-
...b4,
|
|
1201
|
-
...b5,
|
|
1202
|
-
...b6
|
|
1203
|
-
});
|
|
1204
|
-
};
|
|
1205
|
-
const tiktokContents = (items) => {
|
|
1206
|
-
return items.map((item) => {
|
|
1207
|
-
const b1 = { content_id: item.item_id };
|
|
1208
|
-
const b2 = {
|
|
1209
|
-
quantity: quantityOf(item.quantity) ?? 1,
|
|
1210
|
-
price: item.price
|
|
1211
|
-
};
|
|
1212
|
-
const b3 = { content_name: item.item_name };
|
|
1213
|
-
const b4 = { content_category: item.item_category };
|
|
1214
|
-
return compactPayload$1({
|
|
1215
|
-
...b1,
|
|
1216
|
-
...b2,
|
|
1217
|
-
...b3,
|
|
1218
|
-
...b4
|
|
1219
|
-
});
|
|
1220
|
-
});
|
|
1221
|
-
};
|
|
1222
|
-
const tiktokPayload = (input, shape) => {
|
|
1223
|
-
const base = baseParams(input, shape);
|
|
1224
|
-
if (!shape.ecommerce) return compactPayload$1(base);
|
|
1225
|
-
const currency = hasMoney(shape) ? shape.currency : void 0;
|
|
1226
|
-
const contents = tiktokContents(shape.items);
|
|
1227
|
-
const listed = contents.length > 0;
|
|
1228
|
-
const firstId = contents.length === 1 ? contents[0]?.content_id : void 0;
|
|
1229
|
-
const content_id = stringVal(input.params.content_id) ?? firstId;
|
|
1230
|
-
const b1 = {
|
|
1231
|
-
value: shape.value,
|
|
1232
|
-
currency
|
|
1837
|
+
consent: options.consent,
|
|
1838
|
+
map: options.map
|
|
1233
1839
|
};
|
|
1234
|
-
const
|
|
1235
|
-
|
|
1236
|
-
|
|
1840
|
+
const b3 = {
|
|
1841
|
+
send: options.send,
|
|
1842
|
+
setUser: options.setUser
|
|
1237
1843
|
};
|
|
1238
|
-
const
|
|
1239
|
-
|
|
1240
|
-
return compactPayload$1({
|
|
1241
|
-
...base,
|
|
1844
|
+
const b4 = { setConsent: options.setConsent };
|
|
1845
|
+
return {
|
|
1242
1846
|
...b1,
|
|
1243
1847
|
...b2,
|
|
1244
1848
|
...b3,
|
|
1245
1849
|
...b4
|
|
1246
|
-
}
|
|
1850
|
+
};
|
|
1247
1851
|
};
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1852
|
+
//#endregion
|
|
1853
|
+
//#region src/sha256.ts
|
|
1854
|
+
const K = [
|
|
1855
|
+
1116352408,
|
|
1856
|
+
1899447441,
|
|
1857
|
+
3049323471,
|
|
1858
|
+
3921009573,
|
|
1859
|
+
961987163,
|
|
1860
|
+
1508970993,
|
|
1861
|
+
2453635748,
|
|
1862
|
+
2870763221,
|
|
1863
|
+
3624381080,
|
|
1864
|
+
310598401,
|
|
1865
|
+
607225278,
|
|
1866
|
+
1426881987,
|
|
1867
|
+
1925078388,
|
|
1868
|
+
2162078206,
|
|
1869
|
+
2614888103,
|
|
1870
|
+
3248222580,
|
|
1871
|
+
3835390401,
|
|
1872
|
+
4022224774,
|
|
1873
|
+
264347078,
|
|
1874
|
+
604807628,
|
|
1875
|
+
770255983,
|
|
1876
|
+
1249150122,
|
|
1877
|
+
1555081692,
|
|
1878
|
+
1996064986,
|
|
1879
|
+
2554220882,
|
|
1880
|
+
2821834349,
|
|
1881
|
+
2952996808,
|
|
1882
|
+
3210313671,
|
|
1883
|
+
3336571891,
|
|
1884
|
+
3584528711,
|
|
1885
|
+
113926993,
|
|
1886
|
+
338241895,
|
|
1887
|
+
666307205,
|
|
1888
|
+
773529912,
|
|
1889
|
+
1294757372,
|
|
1890
|
+
1396182291,
|
|
1891
|
+
1695183700,
|
|
1892
|
+
1986661051,
|
|
1893
|
+
2177026350,
|
|
1894
|
+
2456956037,
|
|
1895
|
+
2730485921,
|
|
1896
|
+
2820302411,
|
|
1897
|
+
3259730800,
|
|
1898
|
+
3345764771,
|
|
1899
|
+
3516065817,
|
|
1900
|
+
3600352804,
|
|
1901
|
+
4094571909,
|
|
1902
|
+
275423344,
|
|
1903
|
+
430227734,
|
|
1904
|
+
506948616,
|
|
1905
|
+
659060556,
|
|
1906
|
+
883997877,
|
|
1907
|
+
958139571,
|
|
1908
|
+
1322822218,
|
|
1909
|
+
1537002063,
|
|
1910
|
+
1747873779,
|
|
1911
|
+
1955562222,
|
|
1912
|
+
2024104815,
|
|
1913
|
+
2227730452,
|
|
1914
|
+
2361852424,
|
|
1915
|
+
2428436474,
|
|
1916
|
+
2756734187,
|
|
1917
|
+
3204031479,
|
|
1918
|
+
3329325298
|
|
1253
1919
|
];
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
return
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1920
|
+
const H0 = [
|
|
1921
|
+
1779033703,
|
|
1922
|
+
3144134277,
|
|
1923
|
+
1013904242,
|
|
1924
|
+
2773480762,
|
|
1925
|
+
1359893119,
|
|
1926
|
+
2600822924,
|
|
1927
|
+
528734635,
|
|
1928
|
+
1541459225
|
|
1929
|
+
];
|
|
1930
|
+
const rotr = (value, bits) => {
|
|
1931
|
+
return value >>> bits | value << 32 - bits;
|
|
1932
|
+
};
|
|
1933
|
+
/** UTF-8 bytes without `TextEncoder`, which older Hermes builds lack. */
|
|
1934
|
+
const utf8 = (value) => {
|
|
1935
|
+
const bytes = [];
|
|
1936
|
+
for (const char of value) {
|
|
1937
|
+
const code = char.codePointAt(0);
|
|
1938
|
+
if (code < 128) bytes.push(code);
|
|
1939
|
+
else if (code < 2048) bytes.push(192 | code >> 6, 128 | code & 63);
|
|
1940
|
+
else if (code < 65536) {
|
|
1941
|
+
const b1 = 224 | code >> 12;
|
|
1942
|
+
bytes.push(b1, 128 | code >> 6 & 63, 128 | code & 63);
|
|
1943
|
+
} else {
|
|
1944
|
+
bytes.push(240 | code >> 18, 128 | code >> 12 & 63);
|
|
1945
|
+
bytes.push(128 | code >> 6 & 63, 128 | code & 63);
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
return Uint8Array.from(bytes);
|
|
1949
|
+
};
|
|
1950
|
+
const padded = (bytes) => {
|
|
1951
|
+
const bitLength = bytes.length * 8;
|
|
1952
|
+
const total = Math.ceil((bytes.length + 9) / 64) * 64;
|
|
1953
|
+
const out = new Uint8Array(total);
|
|
1954
|
+
out.set(bytes);
|
|
1955
|
+
out[bytes.length] = 128;
|
|
1956
|
+
const view = new DataView(out.buffer);
|
|
1957
|
+
view.setUint32(total - 8, Math.floor(bitLength / 4294967296));
|
|
1958
|
+
view.setUint32(total - 4, bitLength >>> 0);
|
|
1959
|
+
return view;
|
|
1960
|
+
};
|
|
1961
|
+
const compress = (view, offset, h) => {
|
|
1962
|
+
const w = new Array(64);
|
|
1963
|
+
for (let i = 0; i < 16; i += 1) w[i] = view.getUint32(offset + i * 4);
|
|
1964
|
+
for (let i = 16; i < 64; i += 1) {
|
|
1965
|
+
const a = w[i - 15];
|
|
1966
|
+
const b = w[i - 2];
|
|
1967
|
+
const s0 = rotr(a, 7) ^ rotr(a, 18) ^ a >>> 3;
|
|
1968
|
+
const s1 = rotr(b, 17) ^ rotr(b, 19) ^ b >>> 10;
|
|
1969
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1 | 0;
|
|
1970
|
+
}
|
|
1971
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
1972
|
+
for (let i = 0; i < 64; i += 1) {
|
|
1973
|
+
const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
1974
|
+
const ch = e & f ^ ~e & g;
|
|
1975
|
+
const t1 = hh + s1 + ch + K[i] + w[i] | 0;
|
|
1976
|
+
const t2 = (rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + (a & b ^ a & c ^ b & c) | 0;
|
|
1977
|
+
hh = g;
|
|
1978
|
+
g = f;
|
|
1979
|
+
f = e;
|
|
1980
|
+
e = d + t1 | 0;
|
|
1981
|
+
d = c;
|
|
1982
|
+
c = b;
|
|
1983
|
+
b = a;
|
|
1984
|
+
a = t1 + t2 | 0;
|
|
1985
|
+
}
|
|
1986
|
+
const next = [
|
|
1987
|
+
a,
|
|
1988
|
+
b,
|
|
1989
|
+
c,
|
|
1990
|
+
d,
|
|
1991
|
+
e,
|
|
1992
|
+
f,
|
|
1993
|
+
g,
|
|
1994
|
+
hh
|
|
1995
|
+
];
|
|
1996
|
+
for (let i = 0; i < 8; i += 1) h[i] = h[i] + next[i] | 0;
|
|
1997
|
+
};
|
|
1998
|
+
/** Pure-JS SHA-256 (hex) for runtimes without `crypto.subtle`. */
|
|
1999
|
+
const sha256Hex$1 = (value) => {
|
|
2000
|
+
const view = padded(utf8(value));
|
|
2001
|
+
const h = [...H0];
|
|
2002
|
+
for (let offset = 0; offset < view.byteLength; offset += 64) compress(view, offset, h);
|
|
2003
|
+
return h.map((word) => (word >>> 0).toString(16).padStart(8, "0")).join("");
|
|
1272
2004
|
};
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
2005
|
+
//#endregion
|
|
2006
|
+
//#region src/normalize.ts
|
|
2007
|
+
const EMAIL = /^[^@\s]+@[^@\s]+$/;
|
|
2008
|
+
const GMAIL_DOMAINS = /* @__PURE__ */ new Set(["gmail.com", "googlemail.com"]);
|
|
2009
|
+
const COUNTRY$1 = /^[A-Za-z]{2}$/;
|
|
2010
|
+
const E164 = /^\+[1-9]\d{1,14}$/;
|
|
2011
|
+
const NOT_NAME = /[^\p{L}\p{M}\s]/gu;
|
|
2012
|
+
let libphone;
|
|
2013
|
+
/** Loaded on the first phone so pages that never send one skip it. */
|
|
2014
|
+
const loadLibphone = () => {
|
|
2015
|
+
libphone ??= import("libphonenumber-js/min").catch((error) => {
|
|
2016
|
+
libphone = void 0;
|
|
2017
|
+
throw error;
|
|
1277
2018
|
});
|
|
2019
|
+
return libphone;
|
|
1278
2020
|
};
|
|
1279
|
-
const
|
|
1280
|
-
"
|
|
1281
|
-
"transaction_id",
|
|
1282
|
-
"payment_method",
|
|
1283
|
-
"payment_type",
|
|
1284
|
-
"discounted_value",
|
|
1285
|
-
"shipping",
|
|
1286
|
-
"store_id",
|
|
1287
|
-
"channel_id",
|
|
1288
|
-
"product_id",
|
|
1289
|
-
"quantity",
|
|
1290
|
-
"price",
|
|
1291
|
-
...ECOMMERCE_KEYS
|
|
1292
|
-
];
|
|
1293
|
-
const PRODUCT_PARAMS = [
|
|
1294
|
-
"product_id",
|
|
1295
|
-
"quantity",
|
|
1296
|
-
"price",
|
|
1297
|
-
"discounted_value",
|
|
1298
|
-
...ECOMMERCE_KEYS
|
|
1299
|
-
];
|
|
1300
|
-
const masivoOrder = (input, shape) => {
|
|
1301
|
-
const params = input.params;
|
|
1302
|
-
const identity = input.identity ?? {};
|
|
1303
|
-
const purchase_id = orderIdOf(params);
|
|
1304
|
-
const products = masivoProducts(shape.items);
|
|
1305
|
-
if (!purchase_id || shape.value === void 0) return void 0;
|
|
1306
|
-
if (products.length === 0) return void 0;
|
|
1307
|
-
const payment_method = masivoPaymentMethod(params.payment_method ?? params.payment_type);
|
|
1308
|
-
const store_id = stringVal(params.store_id) ?? identity.storeId;
|
|
1309
|
-
const channel_id = stringVal(params.channel_id) ?? identity.channelId;
|
|
1310
|
-
const shipping = nonEmpty({ value: amountOf(params.shipping) });
|
|
1311
|
-
const b1 = {
|
|
1312
|
-
purchase_id,
|
|
1313
|
-
value: shape.value,
|
|
1314
|
-
products,
|
|
1315
|
-
payment_method
|
|
1316
|
-
};
|
|
1317
|
-
const b2 = {
|
|
1318
|
-
store_id,
|
|
1319
|
-
channel_id,
|
|
1320
|
-
shipping
|
|
1321
|
-
};
|
|
1322
|
-
const b3 = { discounted_value: amountOf(params.discounted_value) };
|
|
1323
|
-
return compactPayload$1({
|
|
1324
|
-
...b1,
|
|
1325
|
-
...b2,
|
|
1326
|
-
...b3
|
|
1327
|
-
});
|
|
2021
|
+
const toHex = (bytes) => {
|
|
2022
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1328
2023
|
};
|
|
1329
|
-
const
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
const product = masivoProduct(first);
|
|
1333
|
-
if (!product) return void 0;
|
|
1334
|
-
const discounted = amountOf(input.params.discounted_value);
|
|
1335
|
-
if (discounted === void 0) return product;
|
|
1336
|
-
return {
|
|
1337
|
-
...product,
|
|
1338
|
-
discounted_value: discounted
|
|
1339
|
-
};
|
|
2024
|
+
const filled = (value) => value.length > 0 ? value : void 0;
|
|
2025
|
+
const ifSet = (value, fn) => {
|
|
2026
|
+
return value === void 0 ? void 0 : fn(value);
|
|
1340
2027
|
};
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
2028
|
+
/**
|
|
2029
|
+
* Lowercase hex SHA-256 of the UTF-8 bytes of `value`: `crypto.subtle`
|
|
2030
|
+
* when available, else a built-in implementation (Hermes, insecure web).
|
|
2031
|
+
*/
|
|
2032
|
+
const sha256Hex = async (value) => {
|
|
2033
|
+
const subtle = globalThis.crypto?.subtle;
|
|
2034
|
+
if (!subtle) return sha256Hex$1(value);
|
|
2035
|
+
const data = new TextEncoder().encode(value);
|
|
2036
|
+
const digest = await subtle.digest("SHA-256", data);
|
|
2037
|
+
return toHex(new Uint8Array(digest));
|
|
1344
2038
|
};
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
const
|
|
1348
|
-
|
|
1349
|
-
if (!ok) return;
|
|
1350
|
-
body[key] = params[key];
|
|
1351
|
-
used.push(key);
|
|
1352
|
-
};
|
|
1353
|
-
take("fulfilled", typeof params.fulfilled === "boolean");
|
|
1354
|
-
take("reserve", Number.isInteger(params.reserve));
|
|
1355
|
-
take("referral_code", !!stringVal(params.referral_code));
|
|
1356
|
-
take("redeem", Array.isArray(params.redeem));
|
|
1357
|
-
take("tags", isRecord(params.tags));
|
|
1358
|
-
take("ga4", isRecord(params.ga4));
|
|
1359
|
-
take("issued_at", isIso(params.issued_at));
|
|
1360
|
-
return {
|
|
1361
|
-
body,
|
|
1362
|
-
used
|
|
1363
|
-
};
|
|
2039
|
+
/** Meta and TikTok: trim and lowercase. Not an email → `undefined`. */
|
|
2040
|
+
const normalizeEmail = (value) => {
|
|
2041
|
+
const email = value.trim().toLowerCase();
|
|
2042
|
+
return EMAIL.test(email) ? email : void 0;
|
|
1364
2043
|
};
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
2044
|
+
/**
|
|
2045
|
+
* Google only: removes every whitespace and lowercases. For `gmail.com`
|
|
2046
|
+
* and `googlemail.com` it also drops the dots and any `+tag` of the local
|
|
2047
|
+
* part. Never use it for Meta or TikTok.
|
|
2048
|
+
*/
|
|
2049
|
+
const normalizeEmailForGoogle = (value) => {
|
|
2050
|
+
const email = value.replace(/\s+/g, "").toLowerCase();
|
|
2051
|
+
if (!EMAIL.test(email)) return void 0;
|
|
2052
|
+
const at = email.indexOf("@");
|
|
2053
|
+
const domain = email.slice(at + 1);
|
|
2054
|
+
if (!GMAIL_DOMAINS.has(domain)) return email;
|
|
2055
|
+
const local = (email.slice(0, at).split("+")[0] ?? "").replace(/\./g, "");
|
|
2056
|
+
return local ? `${local}@${domain}` : void 0;
|
|
2057
|
+
};
|
|
2058
|
+
/** ISO 3166-1 alpha-2, uppercase. Anything else → `undefined`. */
|
|
2059
|
+
const normalizeCountryCode = (value) => {
|
|
2060
|
+
if (typeof value !== "string") return void 0;
|
|
2061
|
+
const code = value.trim();
|
|
2062
|
+
return COUNTRY$1.test(code) ? code.toUpperCase() : void 0;
|
|
1369
2063
|
};
|
|
1370
2064
|
/**
|
|
1371
|
-
*
|
|
1372
|
-
*
|
|
2065
|
+
* E.164 with `+` (`+593991234567`). A number without a country code is
|
|
2066
|
+
* read in `defaultCountry`. A value that is not a valid phone number →
|
|
2067
|
+
* `undefined`: omit it, never hash it.
|
|
1373
2068
|
*/
|
|
1374
|
-
const
|
|
1375
|
-
const
|
|
1376
|
-
|
|
1377
|
-
const
|
|
1378
|
-
const
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
const
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
2069
|
+
const normalizePhoneE164 = async (value, defaultCountry) => {
|
|
2070
|
+
const raw = value.trim();
|
|
2071
|
+
if (!raw) return void 0;
|
|
2072
|
+
const lib = await loadLibphone();
|
|
2073
|
+
const code = normalizeCountryCode(defaultCountry);
|
|
2074
|
+
const supported = code && lib.isSupportedCountry(code) ? code : void 0;
|
|
2075
|
+
const parsed = lib.parsePhoneNumberFromString(raw, supported);
|
|
2076
|
+
if (!parsed?.isValid()) return void 0;
|
|
2077
|
+
return parsed.number;
|
|
2078
|
+
};
|
|
2079
|
+
/** Meta `ph`: the E.164 digits without `+`. Not E.164 → `undefined`. */
|
|
2080
|
+
const phoneDigits = (e164) => {
|
|
2081
|
+
return E164.test(e164) ? e164.slice(1) : void 0;
|
|
2082
|
+
};
|
|
2083
|
+
/**
|
|
2084
|
+
* Meta `fn` / `ln`: lowercase, without punctuation, digits or symbols.
|
|
2085
|
+
* Letters keep their accents (UTF-8); inner spaces collapse to one.
|
|
2086
|
+
*/
|
|
2087
|
+
const normalizeName = (value) => {
|
|
2088
|
+
const letters = value.normalize("NFC").toLowerCase().replace(NOT_NAME, "").replace(/\s+/g, " ");
|
|
2089
|
+
return filled(letters.trim());
|
|
2090
|
+
};
|
|
2091
|
+
/** `external_id`: trim and lowercase, as the user id always was. */
|
|
2092
|
+
const normalizeExternalId = (value) => {
|
|
2093
|
+
return filled(value.trim().toLowerCase());
|
|
2094
|
+
};
|
|
2095
|
+
/**
|
|
2096
|
+
* Normalizes and hashes each field the way each ads platform expects. One
|
|
2097
|
+
* email gives two hashes: `emailSha256` (Meta, TikTok) and
|
|
2098
|
+
* `emailGoogleSha256` (Google). One phone gives `phoneE164Sha256` (TikTok,
|
|
2099
|
+
* Google) and `phoneDigitsSha256` (Meta).
|
|
2100
|
+
*/
|
|
2101
|
+
const hashUserData = async (input, options = {}) => {
|
|
2102
|
+
const hash = options.hash ?? sha256Hex;
|
|
2103
|
+
const { email, phone, firstName, lastName, externalId } = input;
|
|
2104
|
+
const country = options.defaultCountry;
|
|
2105
|
+
const e164 = await ifSet(phone, (value) => normalizePhoneE164(value, country));
|
|
2106
|
+
const b1 = { emailSha256: ifSet(email, normalizeEmail) };
|
|
2107
|
+
const b2 = { emailGoogleSha256: ifSet(email, normalizeEmailForGoogle) };
|
|
2108
|
+
const b3 = { phoneE164Sha256: e164 };
|
|
2109
|
+
const b4 = { phoneDigitsSha256: ifSet(e164, phoneDigits) };
|
|
2110
|
+
const b5 = { externalIdSha256: ifSet(externalId, normalizeExternalId) };
|
|
2111
|
+
const b6 = { firstNameSha256: ifSet(firstName, normalizeName) };
|
|
2112
|
+
const b7 = { lastNameSha256: ifSet(lastName, normalizeName) };
|
|
2113
|
+
const m1 = {
|
|
2114
|
+
...b1,
|
|
2115
|
+
...b2,
|
|
2116
|
+
...b3,
|
|
2117
|
+
...b4,
|
|
2118
|
+
...b5,
|
|
2119
|
+
...b6,
|
|
2120
|
+
...b7
|
|
1399
2121
|
};
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
if (MASIVO_ORDER_TYPES.has(type)) {
|
|
1405
|
-
const order = masivoOrder(input, shape);
|
|
1406
|
-
if (order) {
|
|
1407
|
-
body.order = order;
|
|
1408
|
-
for (const key of ORDER_PARAMS) used.add(key);
|
|
1409
|
-
}
|
|
2122
|
+
const out = {};
|
|
2123
|
+
for (const [key, value] of Object.entries(m1)) {
|
|
2124
|
+
if (value === void 0) continue;
|
|
2125
|
+
out[key] = await hash(value);
|
|
1410
2126
|
}
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
2127
|
+
const countryCode = normalizeCountryCode(input.countryCode);
|
|
2128
|
+
if (countryCode) out.countryCode = countryCode;
|
|
2129
|
+
return out;
|
|
2130
|
+
};
|
|
2131
|
+
//#endregion
|
|
2132
|
+
//#region src/pii.ts
|
|
2133
|
+
const PII_KEYS = new Set(PII_PARAM_NAMES);
|
|
2134
|
+
const HASHED_KEYS = new Set(HASHED_PII_PARAM_NAMES);
|
|
2135
|
+
/** Personal params: never in a pixel payload or a plan sample. */
|
|
2136
|
+
const isPiiParam = (name) => {
|
|
2137
|
+
return PII_KEYS.has(name);
|
|
2138
|
+
};
|
|
2139
|
+
/** `crypto.subtle` when available, else the built-in implementation. */
|
|
2140
|
+
const defaultHash = sha256Hex;
|
|
2141
|
+
const isBlank = (value) => {
|
|
2142
|
+
if (value === void 0 || value === null) return true;
|
|
2143
|
+
return typeof value === "string" && value.trim() === "";
|
|
2144
|
+
};
|
|
2145
|
+
const normalizedPhone = async (text, options) => {
|
|
2146
|
+
const e164 = await normalizePhoneE164(text, options.defaultCountry);
|
|
2147
|
+
if (!e164 || options.phoneFormat === "e164") return e164;
|
|
2148
|
+
return phoneDigits(e164);
|
|
2149
|
+
};
|
|
2150
|
+
const normalizedValue = async (key, value, options) => {
|
|
2151
|
+
if (typeof value !== "string" && typeof value !== "number") return void 0;
|
|
2152
|
+
const text = String(value);
|
|
2153
|
+
if (key === "phone") return normalizedPhone(text, options);
|
|
2154
|
+
return normalizeEmail(text);
|
|
2155
|
+
};
|
|
2156
|
+
const hashedValue = async (key, value, options) => {
|
|
2157
|
+
const hash = options.hash ?? defaultHash;
|
|
2158
|
+
if (Array.isArray(value)) {
|
|
2159
|
+
const one = (item) => hashedValue(key, item, options);
|
|
2160
|
+
const hashes = (await Promise.all(value.filter((item) => !isBlank(item)).map(one))).filter((item) => typeof item === "string");
|
|
2161
|
+
return hashes.length > 0 ? hashes : void 0;
|
|
1417
2162
|
}
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
const extra = isRecord(params.metadata) ? params.metadata : {};
|
|
1421
|
-
const rest = omit(params, used);
|
|
1422
|
-
const currency = hasMoney(shape) ? shape.currency : void 0;
|
|
1423
|
-
const b1 = {
|
|
1424
|
-
store_id: identity.storeId,
|
|
1425
|
-
channel_id: identity.channelId
|
|
1426
|
-
};
|
|
1427
|
-
const b2 = {
|
|
1428
|
-
fulfillment_id: identity.fulfillmentId,
|
|
1429
|
-
currency
|
|
1430
|
-
};
|
|
1431
|
-
body.metadata = nonEmpty({
|
|
1432
|
-
...b1,
|
|
1433
|
-
...b2,
|
|
1434
|
-
...rest,
|
|
1435
|
-
...extra
|
|
1436
|
-
});
|
|
1437
|
-
return compactPayload$1(body);
|
|
2163
|
+
const normalized = await normalizedValue(key, value, options);
|
|
2164
|
+
return normalized === void 0 ? void 0 : hash(normalized);
|
|
1438
2165
|
};
|
|
1439
2166
|
/**
|
|
1440
|
-
*
|
|
1441
|
-
*
|
|
2167
|
+
* Normalizes and hashes `email` and `phone`. A value that cannot be
|
|
2168
|
+
* normalized or whose hash fails is omitted, never sent raw. `device_ip`
|
|
2169
|
+
* and `ttclid` stay raw: the ads APIs reject them hashed.
|
|
1442
2170
|
*/
|
|
1443
|
-
const
|
|
1444
|
-
const
|
|
1445
|
-
const
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
const shape = shapeOf(next);
|
|
1459
|
-
if (provider === "ga4") return {
|
|
1460
|
-
provider,
|
|
1461
|
-
name,
|
|
1462
|
-
payload: ga4Payload(next, shape)
|
|
1463
|
-
};
|
|
1464
|
-
if (provider === "meta") {
|
|
1465
|
-
const userData = userDataOf(next, {
|
|
1466
|
-
email: "em",
|
|
1467
|
-
phone: "ph"
|
|
1468
|
-
});
|
|
1469
|
-
if (next.runtime === "web") return {
|
|
1470
|
-
provider,
|
|
1471
|
-
name,
|
|
1472
|
-
payload: metaWebPayload(next, shape),
|
|
1473
|
-
userData
|
|
1474
|
-
};
|
|
1475
|
-
return {
|
|
1476
|
-
provider,
|
|
1477
|
-
name,
|
|
1478
|
-
payload: metaNativePayload(next, shape),
|
|
1479
|
-
userData,
|
|
1480
|
-
valueToSum: shape.value
|
|
1481
|
-
};
|
|
1482
|
-
}
|
|
1483
|
-
if (provider === "tiktok") {
|
|
1484
|
-
const userData = userDataOf(next, {
|
|
1485
|
-
email: "email",
|
|
1486
|
-
phone: "phone_number"
|
|
1487
|
-
});
|
|
1488
|
-
return {
|
|
1489
|
-
provider,
|
|
1490
|
-
name,
|
|
1491
|
-
payload: tiktokPayload(next, shape),
|
|
1492
|
-
userData
|
|
1493
|
-
};
|
|
2171
|
+
const hashPiiParams = async (params, options) => {
|
|
2172
|
+
const next = {};
|
|
2173
|
+
for (const [key, value] of Object.entries(params)) {
|
|
2174
|
+
if (!HASHED_KEYS.has(key)) {
|
|
2175
|
+
next[key] = value;
|
|
2176
|
+
continue;
|
|
2177
|
+
}
|
|
2178
|
+
if (isBlank(value)) continue;
|
|
2179
|
+
try {
|
|
2180
|
+
const hashed = await hashedValue(key, value, options);
|
|
2181
|
+
if (hashed === void 0) options.onInvalid?.(key);
|
|
2182
|
+
else next[key] = hashed;
|
|
2183
|
+
} catch (error) {
|
|
2184
|
+
options.onFailure?.(key, error);
|
|
2185
|
+
}
|
|
1494
2186
|
}
|
|
1495
|
-
return
|
|
1496
|
-
provider,
|
|
1497
|
-
name,
|
|
1498
|
-
payload: masivoPayload(next, shape, name)
|
|
1499
|
-
};
|
|
2187
|
+
return next;
|
|
1500
2188
|
};
|
|
1501
2189
|
//#endregion
|
|
1502
2190
|
//#region src/fanout.ts
|
|
1503
2191
|
const ADS_PROVIDERS = /* @__PURE__ */ new Set(["meta", "tiktok"]);
|
|
2192
|
+
/**
|
|
2193
|
+
* In `server` mode the pixel adapters routed through the relay never fire:
|
|
2194
|
+
* their `send`, `setUser` and `setConsent` are skipped. `hybrid` fires both.
|
|
2195
|
+
*/
|
|
2196
|
+
const isServerBypassed = (rt, provider) => {
|
|
2197
|
+
if (rt.delivery !== "server") return false;
|
|
2198
|
+
return rt.serverSide?.providers.has(provider) ?? false;
|
|
2199
|
+
};
|
|
1504
2200
|
/** Masivo `platform` for every Fire Spark event (same as the API). */
|
|
1505
2201
|
const MASIVO_PLATFORM = "firespark";
|
|
1506
2202
|
const DEFAULT_CONSENT = {
|
|
@@ -1563,18 +2259,47 @@ const resolveAdapters = (rt, opts) => {
|
|
|
1563
2259
|
return available.filter((adapter) => !opts.exclude?.includes(adapter.provider));
|
|
1564
2260
|
};
|
|
1565
2261
|
const hashFn = (rt) => rt.options?.hash ?? defaultHash;
|
|
1566
|
-
|
|
1567
|
-
|
|
2262
|
+
/** Meta `ph` is the E.164 digits; TikTok and custom ads keep the `+`. */
|
|
2263
|
+
const phoneFormatOf = (provider) => {
|
|
2264
|
+
return provider === "meta" ? "digits" : "e164";
|
|
2265
|
+
};
|
|
2266
|
+
const hashOptions = (rt, provider, name) => {
|
|
2267
|
+
const b2 = {
|
|
2268
|
+
level: "warning",
|
|
2269
|
+
source: "hash",
|
|
2270
|
+
provider,
|
|
2271
|
+
event: name
|
|
2272
|
+
};
|
|
2273
|
+
const onFailure = (key, error) => {
|
|
2274
|
+
const message = `Could not hash "${key}"; it was not sent`;
|
|
1568
2275
|
report(rt, {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
2276
|
+
...b2,
|
|
2277
|
+
message,
|
|
2278
|
+
error
|
|
2279
|
+
});
|
|
2280
|
+
};
|
|
2281
|
+
const onInvalid = (key) => {
|
|
2282
|
+
const message = `"${key}" is not a valid ${key}; it was not sent`;
|
|
2283
|
+
report(rt, {
|
|
2284
|
+
...b2,
|
|
2285
|
+
message
|
|
1574
2286
|
});
|
|
1575
2287
|
};
|
|
2288
|
+
const o1 = {
|
|
2289
|
+
hash: hashFn(rt),
|
|
2290
|
+
phoneFormat: phoneFormatOf(provider)
|
|
2291
|
+
};
|
|
2292
|
+
const o2 = {
|
|
2293
|
+
defaultCountry: rt.country,
|
|
2294
|
+
onFailure,
|
|
2295
|
+
onInvalid
|
|
2296
|
+
};
|
|
2297
|
+
return {
|
|
2298
|
+
...o1,
|
|
2299
|
+
...o2
|
|
2300
|
+
};
|
|
1576
2301
|
};
|
|
1577
|
-
const itemsOf = (params) => {
|
|
2302
|
+
const itemsOf$1 = (params) => {
|
|
1578
2303
|
if (!Array.isArray(params.items)) return void 0;
|
|
1579
2304
|
return params.items.filter((row) => {
|
|
1580
2305
|
return !!row && typeof row === "object" && !Array.isArray(row);
|
|
@@ -1585,20 +2310,24 @@ const userFor = async (rt, category, user) => {
|
|
|
1585
2310
|
if (!user) return void 0;
|
|
1586
2311
|
if (category !== "ads") return user;
|
|
1587
2312
|
if (typeof user.id !== "string") return { id: user.id };
|
|
1588
|
-
|
|
2313
|
+
const id = normalizeExternalId(user.id);
|
|
2314
|
+
return { id: id === void 0 ? null : await hashFn(rt)(id) };
|
|
1589
2315
|
};
|
|
1590
|
-
const preparedEvent = async (rt, ads, name, properties) => {
|
|
2316
|
+
const preparedEvent = async (rt, provider, ads, name, properties) => {
|
|
1591
2317
|
const raw = properties ?? {};
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
2318
|
+
if (!ads) return {
|
|
2319
|
+
params: raw,
|
|
2320
|
+
items: itemsOf$1(raw)
|
|
2321
|
+
};
|
|
2322
|
+
const options = hashOptions(rt, provider, name);
|
|
2323
|
+
const params = await hashPiiParams(raw, options);
|
|
2324
|
+
const rows = itemsOf$1(params);
|
|
1596
2325
|
const hashRow = (row) => {
|
|
1597
|
-
return hashPiiParams(row,
|
|
2326
|
+
return hashPiiParams(row, options);
|
|
1598
2327
|
};
|
|
1599
2328
|
return {
|
|
1600
2329
|
params,
|
|
1601
|
-
items:
|
|
2330
|
+
items: rows ? await Promise.all(rows.map(hashRow)) : rows
|
|
1602
2331
|
};
|
|
1603
2332
|
};
|
|
1604
2333
|
const copyParams = (params) => {
|
|
@@ -1659,7 +2388,15 @@ const deliverBuiltin = async (adapter, item, ctx) => {
|
|
|
1659
2388
|
const category = categoryOf(adapter);
|
|
1660
2389
|
const ads = category === "ads";
|
|
1661
2390
|
const name = item.event.name;
|
|
1662
|
-
const
|
|
2391
|
+
const properties = item.event.properties;
|
|
2392
|
+
const p1 = [
|
|
2393
|
+
rt,
|
|
2394
|
+
adapter.provider,
|
|
2395
|
+
ads,
|
|
2396
|
+
name,
|
|
2397
|
+
properties
|
|
2398
|
+
];
|
|
2399
|
+
const { params, items } = await preparedEvent(...p1);
|
|
1663
2400
|
const user = await userFor(rt, category, ctx.user);
|
|
1664
2401
|
const hashedUserId = ads && user?.id ? user.id : void 0;
|
|
1665
2402
|
const b1 = {
|
|
@@ -1717,13 +2454,21 @@ const deliverCustom = async (adapter, item, ctx) => {
|
|
|
1717
2454
|
const { rt } = ctx;
|
|
1718
2455
|
const ads = adapter.consent === "ads";
|
|
1719
2456
|
const name = item.event.name;
|
|
1720
|
-
const
|
|
2457
|
+
const raw = item.event.properties;
|
|
2458
|
+
const p1 = [
|
|
2459
|
+
rt,
|
|
2460
|
+
adapter.provider,
|
|
2461
|
+
ads,
|
|
2462
|
+
name,
|
|
2463
|
+
raw
|
|
2464
|
+
];
|
|
2465
|
+
const prepared = await preparedEvent(...p1);
|
|
1721
2466
|
const user = await userFor(rt, adapter.consent, ctx.user);
|
|
1722
2467
|
const hashed = ads && user?.id ? user.id : void 0;
|
|
1723
2468
|
const base = ads ? copyParams(prepared.params) : prepared.params;
|
|
1724
2469
|
if (ads && prepared.items) base.items = prepared.items;
|
|
1725
2470
|
const properties = copyData(base);
|
|
1726
|
-
const safeItems = itemsOf(properties);
|
|
2471
|
+
const safeItems = itemsOf$1(properties);
|
|
1727
2472
|
const currency = isoCurrencyCode(properties.currency) ?? rt.currency;
|
|
1728
2473
|
const identity = identityOf(rt, hashed);
|
|
1729
2474
|
const context = copyData(ctx.context);
|
|
@@ -1768,6 +2513,7 @@ const deliverCustom = async (adapter, item, ctx) => {
|
|
|
1768
2513
|
await adapter.send(event);
|
|
1769
2514
|
};
|
|
1770
2515
|
const sendOne = async (adapter, item, ctx) => {
|
|
2516
|
+
if (isServerBypassed(ctx.rt, adapter.provider)) return;
|
|
1771
2517
|
if (!allowed(adapter, ctx.consent)) return;
|
|
1772
2518
|
if (isCustomAdapter(adapter)) {
|
|
1773
2519
|
await deliverCustom(adapter, item, ctx);
|
|
@@ -1821,6 +2567,7 @@ const pushUser = async (rt) => {
|
|
|
1821
2567
|
const consent = resolveConsent(rt);
|
|
1822
2568
|
const user = resolveUser(rt) ?? { id: null };
|
|
1823
2569
|
const push = async (adapter) => {
|
|
2570
|
+
if (isServerBypassed(rt, adapter.provider)) return;
|
|
1824
2571
|
if (!adapter.setUser || !allowed(adapter, consent)) return;
|
|
1825
2572
|
try {
|
|
1826
2573
|
const target = await userFor(rt, categoryOf(adapter), user);
|
|
@@ -1838,6 +2585,7 @@ const pushConsent = async (rt) => {
|
|
|
1838
2585
|
const runtimeId = resolveRuntime(options);
|
|
1839
2586
|
const consent = resolveConsent(rt);
|
|
1840
2587
|
const push = async (adapter) => {
|
|
2588
|
+
if (isServerBypassed(rt, adapter.provider)) return;
|
|
1841
2589
|
if (!adapter.setConsent) return;
|
|
1842
2590
|
try {
|
|
1843
2591
|
await adapter.setConsent(consent, runtimeId);
|
|
@@ -1935,19 +2683,28 @@ const createRuntimeState = () => {
|
|
|
1935
2683
|
};
|
|
1936
2684
|
const r5 = {
|
|
1937
2685
|
flushTimer: null,
|
|
1938
|
-
|
|
1939
|
-
|
|
2686
|
+
relayTimer: null,
|
|
2687
|
+
sampleSeen: null
|
|
1940
2688
|
};
|
|
1941
2689
|
const r6 = {
|
|
1942
|
-
|
|
1943
|
-
|
|
2690
|
+
currency: void 0,
|
|
2691
|
+
pending: []
|
|
1944
2692
|
};
|
|
1945
2693
|
const r7 = {
|
|
2694
|
+
trackChain: Promise.resolve(),
|
|
1946
2695
|
flushWait: null,
|
|
1947
|
-
user: null
|
|
1948
|
-
consent: null
|
|
2696
|
+
user: null
|
|
1949
2697
|
};
|
|
1950
|
-
const r8 = {
|
|
2698
|
+
const r8 = {
|
|
2699
|
+
consent: null,
|
|
2700
|
+
detachLifecycle: null,
|
|
2701
|
+
country: void 0
|
|
2702
|
+
};
|
|
2703
|
+
const r9 = {
|
|
2704
|
+
delivery: "client",
|
|
2705
|
+
serverSide: null
|
|
2706
|
+
};
|
|
2707
|
+
const r10 = { relayBuffer: [] };
|
|
1951
2708
|
return {
|
|
1952
2709
|
...r1,
|
|
1953
2710
|
...r2,
|
|
@@ -1956,7 +2713,9 @@ const createRuntimeState = () => {
|
|
|
1956
2713
|
...r5,
|
|
1957
2714
|
...r6,
|
|
1958
2715
|
...r7,
|
|
1959
|
-
...r8
|
|
2716
|
+
...r8,
|
|
2717
|
+
...r9,
|
|
2718
|
+
...r10
|
|
1960
2719
|
};
|
|
1961
2720
|
};
|
|
1962
2721
|
/** State behind the module-level `configure`/`track`/`analytics` API. */
|
|
@@ -2017,6 +2776,7 @@ const resetRuntime = (rt = runtime) => {
|
|
|
2017
2776
|
clearTimer(rt, "sessionTimer");
|
|
2018
2777
|
clearTimer(rt, "retryTimer");
|
|
2019
2778
|
clearTimer(rt, "flushTimer");
|
|
2779
|
+
clearTimer(rt, "relayTimer");
|
|
2020
2780
|
rt.detachLifecycle?.();
|
|
2021
2781
|
Object.assign(rt, createRuntimeState());
|
|
2022
2782
|
};
|
|
@@ -2083,7 +2843,8 @@ const fetchRemoteConfig = async (rt) => {
|
|
|
2083
2843
|
const currency_code = prefs.currency?.code;
|
|
2084
2844
|
return {
|
|
2085
2845
|
...prefs.analytics,
|
|
2086
|
-
currency_code
|
|
2846
|
+
currency_code,
|
|
2847
|
+
country: prefs.country
|
|
2087
2848
|
};
|
|
2088
2849
|
};
|
|
2089
2850
|
/**
|
|
@@ -2244,6 +3005,249 @@ const markInteraction = (state, name, entityId) => {
|
|
|
2244
3005
|
};
|
|
2245
3006
|
};
|
|
2246
3007
|
//#endregion
|
|
3008
|
+
//#region src/relay.ts
|
|
3009
|
+
const RELAY_CONTRACT_HEADER = "x-firespark-contract";
|
|
3010
|
+
const RETRY_STATUSES = /* @__PURE__ */ new Set([408, 429]);
|
|
3011
|
+
/** A relay HTTP response the client should retry with backoff. */
|
|
3012
|
+
var RelayHttpError = class extends Error {
|
|
3013
|
+
status;
|
|
3014
|
+
constructor(status) {
|
|
3015
|
+
super(`Relay responded ${status}`);
|
|
3016
|
+
this.name = "RelayHttpError";
|
|
3017
|
+
this.status = status;
|
|
3018
|
+
}
|
|
3019
|
+
};
|
|
3020
|
+
const isRetryableStatus = (status) => {
|
|
3021
|
+
return RETRY_STATUSES.has(status) || status >= 500;
|
|
3022
|
+
};
|
|
3023
|
+
const isRetryableError = (error) => {
|
|
3024
|
+
if (error instanceof RelayHttpError) return isRetryableStatus(error.status);
|
|
3025
|
+
return true;
|
|
3026
|
+
};
|
|
3027
|
+
const pageHref = () => {
|
|
3028
|
+
const location = globalThis.location;
|
|
3029
|
+
return textOf(location?.href);
|
|
3030
|
+
};
|
|
3031
|
+
const pageReferrer = () => {
|
|
3032
|
+
const doc = globalThis.document;
|
|
3033
|
+
return textOf(doc?.referrer);
|
|
3034
|
+
};
|
|
3035
|
+
const userInput = (rt, event) => {
|
|
3036
|
+
const props = event.event.properties ?? {};
|
|
3037
|
+
const b1 = {
|
|
3038
|
+
email: textOf(props.email),
|
|
3039
|
+
phone: textOf(props.phone)
|
|
3040
|
+
};
|
|
3041
|
+
const b2 = { firstName: textOf(props.first_name) };
|
|
3042
|
+
const b3 = { lastName: textOf(props.last_name) };
|
|
3043
|
+
const b4 = { externalId: textOf(resolveUser(rt)?.id) };
|
|
3044
|
+
const b5 = { countryCode: textOf(props.country_code) };
|
|
3045
|
+
return {
|
|
3046
|
+
...b1,
|
|
3047
|
+
...b2,
|
|
3048
|
+
...b3,
|
|
3049
|
+
...b4,
|
|
3050
|
+
...b5
|
|
3051
|
+
};
|
|
3052
|
+
};
|
|
3053
|
+
const contextOf$1 = (rt) => {
|
|
3054
|
+
const scope = rt.options?.getScope?.() ?? {};
|
|
3055
|
+
const channelId = rt.options?.channel_id ?? scope.channel_id ?? "";
|
|
3056
|
+
const b1 = {
|
|
3057
|
+
runtime: "web",
|
|
3058
|
+
locale: rt.context?.locale
|
|
3059
|
+
};
|
|
3060
|
+
const b2 = {
|
|
3061
|
+
channelId,
|
|
3062
|
+
storeId: scope.store_id,
|
|
3063
|
+
brandId: scope.brand_id
|
|
3064
|
+
};
|
|
3065
|
+
return {
|
|
3066
|
+
...b1,
|
|
3067
|
+
...b2,
|
|
3068
|
+
fulfillmentId: scope.fulfillment_id
|
|
3069
|
+
};
|
|
3070
|
+
};
|
|
3071
|
+
const buildRelayEvent = async (rt, event) => {
|
|
3072
|
+
const relay = rt.serverSide;
|
|
3073
|
+
if (!relay) return null;
|
|
3074
|
+
if (!resolveConsent(rt).ads) return null;
|
|
3075
|
+
const sourceUrl = pageHref();
|
|
3076
|
+
if (!sourceUrl) return null;
|
|
3077
|
+
if (rt.delivery === "server") ensureFbCookies(event.at);
|
|
3078
|
+
const clickIds = await captureClickIds({
|
|
3079
|
+
consent: { ads: true },
|
|
3080
|
+
storage: rt.options?.storage,
|
|
3081
|
+
now: event.at
|
|
3082
|
+
});
|
|
3083
|
+
const hashOptions = {
|
|
3084
|
+
hash: rt.options?.hash,
|
|
3085
|
+
defaultCountry: rt.country
|
|
3086
|
+
};
|
|
3087
|
+
const user = await hashUserData(userInput(rt, event), hashOptions);
|
|
3088
|
+
const wireConsent = { ads: true };
|
|
3089
|
+
const i1 = {
|
|
3090
|
+
eventId: event.eventId,
|
|
3091
|
+
name: event.event.name
|
|
3092
|
+
};
|
|
3093
|
+
const i2 = {
|
|
3094
|
+
timestamp: event.at,
|
|
3095
|
+
sourceUrl,
|
|
3096
|
+
referrerUrl: pageReferrer()
|
|
3097
|
+
};
|
|
3098
|
+
const i3 = {
|
|
3099
|
+
properties: event.event.properties,
|
|
3100
|
+
currency: rt.currency
|
|
3101
|
+
};
|
|
3102
|
+
const i4 = {
|
|
3103
|
+
user,
|
|
3104
|
+
clickIds,
|
|
3105
|
+
consent: wireConsent
|
|
3106
|
+
};
|
|
3107
|
+
const i5 = {
|
|
3108
|
+
providers: [...relay.providers],
|
|
3109
|
+
context: contextOf$1(rt)
|
|
3110
|
+
};
|
|
3111
|
+
return buildTrackingEvent({
|
|
3112
|
+
...i1,
|
|
3113
|
+
...i2,
|
|
3114
|
+
...i3,
|
|
3115
|
+
...i4,
|
|
3116
|
+
...i5
|
|
3117
|
+
});
|
|
3118
|
+
};
|
|
3119
|
+
const relayHeaders = (rt) => {
|
|
3120
|
+
const b1 = { "content-type": "application/json" };
|
|
3121
|
+
const b2 = { [RELAY_CONTRACT_HEADER]: "1" };
|
|
3122
|
+
return {
|
|
3123
|
+
...b1,
|
|
3124
|
+
...b2,
|
|
3125
|
+
...rt.serverSide?.headers ?? {}
|
|
3126
|
+
};
|
|
3127
|
+
};
|
|
3128
|
+
const reportRelay = (rt, message, error) => {
|
|
3129
|
+
report(rt, {
|
|
3130
|
+
level: "error",
|
|
3131
|
+
source: "relay",
|
|
3132
|
+
message,
|
|
3133
|
+
error
|
|
3134
|
+
});
|
|
3135
|
+
};
|
|
3136
|
+
const reportRejected = (rt, body) => {
|
|
3137
|
+
const rejected = body?.rejected;
|
|
3138
|
+
if (!Array.isArray(rejected)) return;
|
|
3139
|
+
for (const entry of rejected) {
|
|
3140
|
+
const reason = textOf(entry?.reason) ?? "rejected";
|
|
3141
|
+
const eventId = textOf(entry?.event_id);
|
|
3142
|
+
const suffix = eventId ? ` (${eventId})` : "";
|
|
3143
|
+
reportRelay(rt, `Relay rejected an event: ${reason}${suffix}`);
|
|
3144
|
+
}
|
|
3145
|
+
};
|
|
3146
|
+
const postEnvelope = async (rt, events, keepalive) => {
|
|
3147
|
+
const relay = rt.serverSide;
|
|
3148
|
+
if (!relay) return;
|
|
3149
|
+
const fetchImpl = rt.options?.fetchImpl ?? globalThis.fetch;
|
|
3150
|
+
if (typeof fetchImpl !== "function") return;
|
|
3151
|
+
const body = JSON.stringify(buildTrackingEnvelope(events));
|
|
3152
|
+
const b1 = {
|
|
3153
|
+
method: "POST",
|
|
3154
|
+
keepalive,
|
|
3155
|
+
headers: relayHeaders(rt),
|
|
3156
|
+
body
|
|
3157
|
+
};
|
|
3158
|
+
const res = await fetchImpl(relay.endpoint, b1);
|
|
3159
|
+
if (res.status === 202) {
|
|
3160
|
+
const parsed = await res.json().catch(() => void 0);
|
|
3161
|
+
reportRejected(rt, parsed);
|
|
3162
|
+
return;
|
|
3163
|
+
}
|
|
3164
|
+
if (isRetryableStatus(res.status)) throw new RelayHttpError(res.status);
|
|
3165
|
+
reportRelay(rt, `Relay responded ${res.status}; the events were dropped`);
|
|
3166
|
+
};
|
|
3167
|
+
const sendBeacon = (rt, events) => {
|
|
3168
|
+
const nav = globalThis.navigator;
|
|
3169
|
+
if (typeof nav?.sendBeacon !== "function") return false;
|
|
3170
|
+
const relay = rt.serverSide;
|
|
3171
|
+
if (!relay) return false;
|
|
3172
|
+
const body = JSON.stringify(buildTrackingEnvelope(events));
|
|
3173
|
+
const blob = new Blob([body], { type: "application/json" });
|
|
3174
|
+
return nav.sendBeacon(relay.endpoint, blob);
|
|
3175
|
+
};
|
|
3176
|
+
const chunkEvents = (events, batchSize) => {
|
|
3177
|
+
const chunks = [];
|
|
3178
|
+
let current = [];
|
|
3179
|
+
let bytes = 0;
|
|
3180
|
+
for (const event of events) {
|
|
3181
|
+
const size = JSON.stringify(event).length;
|
|
3182
|
+
const full = current.length >= batchSize || bytes + size > 6e4;
|
|
3183
|
+
if (current.length > 0 && full) {
|
|
3184
|
+
chunks.push(current);
|
|
3185
|
+
current = [];
|
|
3186
|
+
bytes = 0;
|
|
3187
|
+
}
|
|
3188
|
+
current.push(event);
|
|
3189
|
+
bytes += size;
|
|
3190
|
+
}
|
|
3191
|
+
if (current.length > 0) chunks.push(current);
|
|
3192
|
+
return chunks;
|
|
3193
|
+
};
|
|
3194
|
+
/** Sends every buffered contract event. Keeps running on a failure. */
|
|
3195
|
+
const flushRelay = async (rt, options = {}) => {
|
|
3196
|
+
clearTimer(rt, "relayTimer");
|
|
3197
|
+
const relay = rt.serverSide;
|
|
3198
|
+
if (!relay || rt.relayBuffer.length === 0) return;
|
|
3199
|
+
const chunks = chunkEvents(rt.relayBuffer, relay.batchSize);
|
|
3200
|
+
rt.relayBuffer = [];
|
|
3201
|
+
const keepalive = options.keepalive ?? false;
|
|
3202
|
+
const send = async (chunk) => {
|
|
3203
|
+
if (options.beacon && sendBeacon(rt, chunk)) return;
|
|
3204
|
+
const b1 = {
|
|
3205
|
+
extraAttempts: 2,
|
|
3206
|
+
baseDelayMs: 300
|
|
3207
|
+
};
|
|
3208
|
+
const run = () => postEnvelope(rt, chunk, keepalive || !!options.beacon);
|
|
3209
|
+
try {
|
|
3210
|
+
await withExponentialRetry(run, {
|
|
3211
|
+
...b1,
|
|
3212
|
+
shouldRetry: isRetryableError
|
|
3213
|
+
});
|
|
3214
|
+
} catch (error) {
|
|
3215
|
+
reportRelay(rt, `Relay request failed: ${errorMessage(error)}`, error);
|
|
3216
|
+
}
|
|
3217
|
+
};
|
|
3218
|
+
await Promise.all(chunks.map(send));
|
|
3219
|
+
};
|
|
3220
|
+
const scheduleRelayFlush = (rt) => {
|
|
3221
|
+
const relay = rt.serverSide;
|
|
3222
|
+
if (!relay || relay.flushIntervalMs <= 0 || rt.relayTimer) return;
|
|
3223
|
+
rt.relayTimer = setTimeout(() => {
|
|
3224
|
+
rt.relayTimer = null;
|
|
3225
|
+
flushRelay(rt);
|
|
3226
|
+
}, relay.flushIntervalMs);
|
|
3227
|
+
};
|
|
3228
|
+
/** Builds and buffers a contract event, flushing per `batchSize`/purchase. */
|
|
3229
|
+
const relayTrack = async (rt, event) => {
|
|
3230
|
+
const relay = rt.serverSide;
|
|
3231
|
+
if (!relay || relay.providers.size === 0) return;
|
|
3232
|
+
const built = await buildRelayEvent(rt, event);
|
|
3233
|
+
if (!built) return;
|
|
3234
|
+
rt.relayBuffer.push(built);
|
|
3235
|
+
if (event.event.name === "purchase" || rt.relayBuffer.length >= relay.batchSize) {
|
|
3236
|
+
await flushRelay(rt);
|
|
3237
|
+
return;
|
|
3238
|
+
}
|
|
3239
|
+
scheduleRelayFlush(rt);
|
|
3240
|
+
};
|
|
3241
|
+
/** Sends the buffer with `navigator.sendBeacon` when the page hides. */
|
|
3242
|
+
const flushRelayBeacon = (rt) => {
|
|
3243
|
+
flushRelay(rt, { beacon: true });
|
|
3244
|
+
};
|
|
3245
|
+
/** Clears the relay buffer and timer (used on consent revoke). */
|
|
3246
|
+
const stopRelay = (rt) => {
|
|
3247
|
+
clearTimer(rt, "relayTimer");
|
|
3248
|
+
rt.relayBuffer = [];
|
|
3249
|
+
};
|
|
3250
|
+
//#endregion
|
|
2247
3251
|
//#region src/lifecycle.ts
|
|
2248
3252
|
const browserTargets = () => {
|
|
2249
3253
|
const root = globalThis;
|
|
@@ -2262,6 +3266,7 @@ const attachBrowser = (rt) => {
|
|
|
2262
3266
|
const { doc, win } = targets;
|
|
2263
3267
|
const onHide = () => {
|
|
2264
3268
|
flushInBackground(rt, { keepalive: true });
|
|
3269
|
+
flushRelayBeacon(rt);
|
|
2265
3270
|
};
|
|
2266
3271
|
const onVisibility = () => {
|
|
2267
3272
|
if (doc.visibilityState === "hidden") onHide();
|
|
@@ -2302,6 +3307,127 @@ const attachLifecycle = (rt) => {
|
|
|
2302
3307
|
};
|
|
2303
3308
|
};
|
|
2304
3309
|
//#endregion
|
|
3310
|
+
//#region src/mode.ts
|
|
3311
|
+
const LOCAL_HOSTS$1 = /* @__PURE__ */ new Set([
|
|
3312
|
+
"localhost",
|
|
3313
|
+
"127.0.0.1",
|
|
3314
|
+
"[::1]"
|
|
3315
|
+
]);
|
|
3316
|
+
const DELIVERY_MODES = /* @__PURE__ */ new Set([
|
|
3317
|
+
"client",
|
|
3318
|
+
"server",
|
|
3319
|
+
"hybrid"
|
|
3320
|
+
]);
|
|
3321
|
+
const SERVER_PROVIDERS = new Set(SERVER_PROVIDER_IDS);
|
|
3322
|
+
const DEFAULT_BATCH_SIZE = 10;
|
|
3323
|
+
const MIN_BATCH_SIZE = 1;
|
|
3324
|
+
const MAX_BATCH_SIZE = 20;
|
|
3325
|
+
const isProduction = (options) => {
|
|
3326
|
+
return options.env === "production";
|
|
3327
|
+
};
|
|
3328
|
+
const warn = (message) => {
|
|
3329
|
+
return {
|
|
3330
|
+
level: "warning",
|
|
3331
|
+
source: "configure",
|
|
3332
|
+
message
|
|
3333
|
+
};
|
|
3334
|
+
};
|
|
3335
|
+
/**
|
|
3336
|
+
* The endpoint must be an `https:` URL or a same-origin path (`/...`).
|
|
3337
|
+
* `http://localhost` is allowed only outside production.
|
|
3338
|
+
*/
|
|
3339
|
+
const validateEndpoint = (endpoint, production) => {
|
|
3340
|
+
if (endpoint.startsWith("/")) return;
|
|
3341
|
+
let url;
|
|
3342
|
+
try {
|
|
3343
|
+
url = new URL(endpoint);
|
|
3344
|
+
} catch {
|
|
3345
|
+
throw new AnalyticsConfigError("ANALYTICS_INVALID_ENDPOINT", "serverSide.endpoint must be an https URL or a path");
|
|
3346
|
+
}
|
|
3347
|
+
if (url.protocol === "https:") return;
|
|
3348
|
+
if (url.protocol === "http:" && LOCAL_HOSTS$1.has(url.hostname) && !production) return;
|
|
3349
|
+
throw new AnalyticsConfigError("ANALYTICS_INVALID_ENDPOINT", `serverSide.endpoint must use https (got "${endpoint}")`);
|
|
3350
|
+
};
|
|
3351
|
+
const endpointRequired = (mode) => {
|
|
3352
|
+
throw new AnalyticsConfigError("ANALYTICS_SERVER_ENDPOINT_REQUIRED", `mode "${mode}" requires serverSide.endpoint`);
|
|
3353
|
+
};
|
|
3354
|
+
const unsupportedRuntime = () => {
|
|
3355
|
+
throw new AnalyticsConfigError("ANALYTICS_SERVER_MODE_UNSUPPORTED_RUNTIME", "server delivery is web-only; it is not supported on react-native");
|
|
3356
|
+
};
|
|
3357
|
+
const resolveProviders = (options) => {
|
|
3358
|
+
const configured = options.serverSide?.providers;
|
|
3359
|
+
if (configured) {
|
|
3360
|
+
const valid = configured.filter((id) => SERVER_PROVIDERS.has(id));
|
|
3361
|
+
return new Set(valid);
|
|
3362
|
+
}
|
|
3363
|
+
const adapters = options.adapters ?? [];
|
|
3364
|
+
const present = /* @__PURE__ */ new Set();
|
|
3365
|
+
for (const adapter of adapters) if (SERVER_PROVIDERS.has(adapter.provider)) present.add(adapter.provider);
|
|
3366
|
+
return present;
|
|
3367
|
+
};
|
|
3368
|
+
const clampBatchSize = (value) => {
|
|
3369
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return DEFAULT_BATCH_SIZE;
|
|
3370
|
+
return Math.min(Math.max(Math.trunc(value), MIN_BATCH_SIZE), MAX_BATCH_SIZE);
|
|
3371
|
+
};
|
|
3372
|
+
const resolveServerSide = (options, endpoint) => {
|
|
3373
|
+
const serverSide = options.serverSide;
|
|
3374
|
+
const b1 = {
|
|
3375
|
+
endpoint,
|
|
3376
|
+
providers: resolveProviders(options)
|
|
3377
|
+
};
|
|
3378
|
+
const b2 = { batchSize: clampBatchSize(serverSide?.batchSize) };
|
|
3379
|
+
const flushIntervalMs = Math.max(serverSide?.flushIntervalMs ?? 0, 0);
|
|
3380
|
+
const headers = { ...serverSide?.headers ?? {} };
|
|
3381
|
+
return {
|
|
3382
|
+
...b1,
|
|
3383
|
+
...b2,
|
|
3384
|
+
flushIntervalMs,
|
|
3385
|
+
headers
|
|
3386
|
+
};
|
|
3387
|
+
};
|
|
3388
|
+
/**
|
|
3389
|
+
* Resolves `mode` and `serverSide` synchronously, the same spot that
|
|
3390
|
+
* validates `channel_id`. It throws `AnalyticsConfigError` for an invalid
|
|
3391
|
+
* configuration (see the table in the docs). `configure()` wraps this: in
|
|
3392
|
+
* production it reports the error and falls back to `client`.
|
|
3393
|
+
*/
|
|
3394
|
+
const planDelivery = (options) => {
|
|
3395
|
+
const mode = options.mode;
|
|
3396
|
+
if (mode !== void 0 && !DELIVERY_MODES.has(mode)) throw new AnalyticsConfigError("ANALYTICS_INVALID_MODE", `Unknown delivery mode "${String(mode)}"`);
|
|
3397
|
+
const web = resolveRuntime(options) === "web";
|
|
3398
|
+
const production = isProduction(options);
|
|
3399
|
+
const endpoint = textOf(options.serverSide?.endpoint);
|
|
3400
|
+
const warnings = [];
|
|
3401
|
+
const client = () => {
|
|
3402
|
+
return {
|
|
3403
|
+
mode: "client",
|
|
3404
|
+
serverSide: null,
|
|
3405
|
+
warnings
|
|
3406
|
+
};
|
|
3407
|
+
};
|
|
3408
|
+
if (mode === "client") {
|
|
3409
|
+
if (endpoint) warnings.push(warn("endpoint ignored in mode \"client\""));
|
|
3410
|
+
return client();
|
|
3411
|
+
}
|
|
3412
|
+
if (mode === void 0 && !endpoint) return client();
|
|
3413
|
+
if (mode === void 0 && !web) {
|
|
3414
|
+
warnings.push(warn("server delivery is web-only; endpoint ignored"));
|
|
3415
|
+
return client();
|
|
3416
|
+
}
|
|
3417
|
+
if (!endpoint) return endpointRequired(mode ?? "hybrid");
|
|
3418
|
+
if (!web) return unsupportedRuntime();
|
|
3419
|
+
validateEndpoint(endpoint, production);
|
|
3420
|
+
return {
|
|
3421
|
+
mode: mode === "server" ? "server" : "hybrid",
|
|
3422
|
+
serverSide: resolveServerSide(options, endpoint),
|
|
3423
|
+
warnings
|
|
3424
|
+
};
|
|
3425
|
+
};
|
|
3426
|
+
/** The resolved delivery mode. Throws `AnalyticsConfigError` when invalid. */
|
|
3427
|
+
const resolveDeliveryMode = (options) => {
|
|
3428
|
+
return planDelivery(options).mode;
|
|
3429
|
+
};
|
|
3430
|
+
//#endregion
|
|
2305
3431
|
//#region src/sample.ts
|
|
2306
3432
|
const PLAN_SAMPLES_PATH = "/api/storefront/v1/analytics/plan-samples";
|
|
2307
3433
|
const DEFAULT_RATE = .01;
|
|
@@ -2569,9 +3695,19 @@ const resolveCurrency = (rt, currency) => {
|
|
|
2569
3695
|
message: `Ignoring invalid currency "${String(currency)}"`
|
|
2570
3696
|
});
|
|
2571
3697
|
};
|
|
3698
|
+
const resolveCountry = (rt, country) => {
|
|
3699
|
+
if (country === void 0 || country === null || country === "") return;
|
|
3700
|
+
const code = normalizeCountryCode(country);
|
|
3701
|
+
if (code) return code;
|
|
3702
|
+
report(rt, {
|
|
3703
|
+
level: "warning",
|
|
3704
|
+
source: "configure",
|
|
3705
|
+
message: `Ignoring invalid defaultCountry "${String(country)}"`
|
|
3706
|
+
});
|
|
3707
|
+
};
|
|
2572
3708
|
/**
|
|
2573
|
-
* `sessionMs` and `
|
|
2574
|
-
* fetched whenever one of them is missing.
|
|
3709
|
+
* `sessionMs`, `currency` and `defaultCountry` from `configure()` win.
|
|
3710
|
+
* Preferences are fetched whenever one of them is missing.
|
|
2575
3711
|
*/
|
|
2576
3712
|
const refreshConfig = async (rt) => {
|
|
2577
3713
|
const provided = rt.options?.sessionMs;
|
|
@@ -2580,7 +3716,7 @@ const refreshConfig = async (rt) => {
|
|
|
2580
3716
|
rt.sessionMs = provided;
|
|
2581
3717
|
await persistConfig(rt, rt.sessionMs);
|
|
2582
3718
|
}
|
|
2583
|
-
if (hasSession && rt.currency) return;
|
|
3719
|
+
if (hasSession && rt.currency && rt.country) return;
|
|
2584
3720
|
try {
|
|
2585
3721
|
const config = await fetchRemoteConfig(rt);
|
|
2586
3722
|
if (!hasSession) {
|
|
@@ -2588,6 +3724,7 @@ const refreshConfig = async (rt) => {
|
|
|
2588
3724
|
await persistConfig(rt, rt.sessionMs);
|
|
2589
3725
|
}
|
|
2590
3726
|
if (!rt.currency) rt.currency = isoCurrencyCode(config.currency_code);
|
|
3727
|
+
if (!rt.country) rt.country = normalizeCountryCode(config.country);
|
|
2591
3728
|
} catch (error) {
|
|
2592
3729
|
report(rt, {
|
|
2593
3730
|
level: "warning",
|
|
@@ -2731,10 +3868,31 @@ const storeEvent = async (rt, pending) => {
|
|
|
2731
3868
|
return;
|
|
2732
3869
|
}
|
|
2733
3870
|
};
|
|
3871
|
+
const invalidOrderIdWarning = (rt) => {
|
|
3872
|
+
report(rt, {
|
|
3873
|
+
level: "warning",
|
|
3874
|
+
source: "track",
|
|
3875
|
+
message: `purchase order_id does not fit an event id (1 to 55 of A-Z a-z 0-9 : _ . -); destinations get a random event id, so a reload is not deduplicated`,
|
|
3876
|
+
event: "purchase"
|
|
3877
|
+
});
|
|
3878
|
+
};
|
|
3879
|
+
/**
|
|
3880
|
+
* A purchase uses `purchase:<order_id>` (see `purchaseEventId`), so every
|
|
3881
|
+
* pixel and the server relay send the same id. Other events use `fallback`.
|
|
3882
|
+
*/
|
|
3883
|
+
const destinationEventId = (rt, event, fallback) => {
|
|
3884
|
+
if (event.name !== "purchase") return fallback;
|
|
3885
|
+
const orderId = event.properties?.order_id;
|
|
3886
|
+
const eventId = purchaseEventId(orderId);
|
|
3887
|
+
if (eventId) return eventId;
|
|
3888
|
+
if (textOf(orderId)) invalidOrderIdWarning(rt);
|
|
3889
|
+
return fallback;
|
|
3890
|
+
};
|
|
2734
3891
|
/**
|
|
2735
3892
|
* Only the object form, funnel and interaction events are stored in Fire
|
|
2736
3893
|
* Spark. `track("name", params)` for other names goes to destinations only
|
|
2737
|
-
* (by design). A stored event shares its id with the destinations
|
|
3894
|
+
* (by design). A stored event shares its id with the destinations, except
|
|
3895
|
+
* a purchase: destinations get `purchase:<order_id>`.
|
|
2738
3896
|
*/
|
|
2739
3897
|
const dispatch = async (rt, pending) => {
|
|
2740
3898
|
if (!rt.options) {
|
|
@@ -2751,11 +3909,13 @@ const dispatch = async (rt, pending) => {
|
|
|
2751
3909
|
event: event.name
|
|
2752
3910
|
});
|
|
2753
3911
|
});
|
|
2754
|
-
const
|
|
3912
|
+
const fallback = await storeEvent(rt, pending) ?? randomUUID();
|
|
3913
|
+
const item = {
|
|
2755
3914
|
event,
|
|
2756
|
-
eventId:
|
|
3915
|
+
eventId: destinationEventId(rt, event, fallback),
|
|
2757
3916
|
at
|
|
2758
|
-
}
|
|
3917
|
+
};
|
|
3918
|
+
const fanoutP = fanoutAdapters(rt, item, opts).catch((error) => {
|
|
2759
3919
|
report(rt, {
|
|
2760
3920
|
level: "error",
|
|
2761
3921
|
source: "destination",
|
|
@@ -2764,16 +3924,57 @@ const dispatch = async (rt, pending) => {
|
|
|
2764
3924
|
event: event.name
|
|
2765
3925
|
});
|
|
2766
3926
|
});
|
|
2767
|
-
|
|
3927
|
+
const relayP = relayTrack(rt, item).catch((error) => {
|
|
3928
|
+
report(rt, {
|
|
3929
|
+
level: "error",
|
|
3930
|
+
source: "relay",
|
|
3931
|
+
message: `Relay failed: ${errorMessage(error)}`,
|
|
3932
|
+
error,
|
|
3933
|
+
event: event.name
|
|
3934
|
+
});
|
|
3935
|
+
});
|
|
3936
|
+
await Promise.all([
|
|
3937
|
+
sampleP,
|
|
3938
|
+
fanoutP,
|
|
3939
|
+
relayP
|
|
3940
|
+
]);
|
|
2768
3941
|
};
|
|
2769
3942
|
const replayPending = async (rt) => {
|
|
2770
3943
|
const pending = rt.pending;
|
|
2771
3944
|
rt.pending = [];
|
|
2772
3945
|
for (const item of pending) await dispatch(rt, item);
|
|
2773
3946
|
};
|
|
3947
|
+
/**
|
|
3948
|
+
* Resolves the delivery mode. A misconfiguration throws synchronously in
|
|
3949
|
+
* development so it surfaces in tests; in production (`env: "production"`)
|
|
3950
|
+
* it warns and falls back to `client` mode so tracking never breaks.
|
|
3951
|
+
*/
|
|
3952
|
+
const resolveDelivery = (rt, options) => {
|
|
3953
|
+
try {
|
|
3954
|
+
return planDelivery(options);
|
|
3955
|
+
} catch (error) {
|
|
3956
|
+
if (!(options.env === "production") || !(error instanceof AnalyticsConfigError)) throw error;
|
|
3957
|
+
const p1 = `${error.message}; falling back to client delivery`;
|
|
3958
|
+
return {
|
|
3959
|
+
mode: "client",
|
|
3960
|
+
serverSide: null,
|
|
3961
|
+
warnings: [{
|
|
3962
|
+
level: "warning",
|
|
3963
|
+
...configSource(p1)
|
|
3964
|
+
}]
|
|
3965
|
+
};
|
|
3966
|
+
}
|
|
3967
|
+
};
|
|
3968
|
+
const configSource = (message) => {
|
|
3969
|
+
return {
|
|
3970
|
+
source: "configure",
|
|
3971
|
+
message
|
|
3972
|
+
};
|
|
3973
|
+
};
|
|
2774
3974
|
const configureWith = async (rt, options) => {
|
|
2775
3975
|
const channel_id = options.channel_id?.trim() ?? "";
|
|
2776
3976
|
if (!channel_id) throw new Error("channel_id is required");
|
|
3977
|
+
const plan = resolveDelivery(rt, options);
|
|
2777
3978
|
return enqueue(rt, async () => {
|
|
2778
3979
|
const next = {
|
|
2779
3980
|
...options,
|
|
@@ -2787,7 +3988,11 @@ const configureWith = async (rt, options) => {
|
|
|
2787
3988
|
adapters: accepted
|
|
2788
3989
|
};
|
|
2789
3990
|
}
|
|
3991
|
+
rt.delivery = plan.mode;
|
|
3992
|
+
rt.serverSide = plan.serverSide;
|
|
3993
|
+
for (const warning of plan.warnings) report(rt, warning);
|
|
2790
3994
|
rt.currency = resolveCurrency(rt, options.currency);
|
|
3995
|
+
rt.country = resolveCountry(rt, options.defaultCountry);
|
|
2791
3996
|
const ctx = options.context;
|
|
2792
3997
|
rt.context = buildEventContext(typeof ctx === "function" ? void 0 : ctx);
|
|
2793
3998
|
await loadPersisted(rt);
|
|
@@ -2840,12 +4045,24 @@ const setUserWith = (rt, user) => {
|
|
|
2840
4045
|
await pushUser(rt);
|
|
2841
4046
|
});
|
|
2842
4047
|
};
|
|
4048
|
+
/**
|
|
4049
|
+
* Revoking ads consent stops the relay and deletes the first-party ids the
|
|
4050
|
+
* SDK wrote (`_fbp`, `_fbc` and the `fs_ck_*` click-id snapshots), so no
|
|
4051
|
+
* identifiers linger after the customer opts out.
|
|
4052
|
+
*/
|
|
4053
|
+
const revokeAdsArtifacts = async (rt) => {
|
|
4054
|
+
stopRelay(rt);
|
|
4055
|
+
deleteFbCookies();
|
|
4056
|
+
const storage = rt.options?.storage;
|
|
4057
|
+
if (storage) await clearClickIds(storage);
|
|
4058
|
+
};
|
|
2843
4059
|
const setConsentWith = (rt, consent) => {
|
|
2844
4060
|
return enqueue(rt, async () => {
|
|
2845
4061
|
rt.consent = {
|
|
2846
4062
|
analytics: consent.analytics,
|
|
2847
4063
|
ads: consent.ads
|
|
2848
4064
|
};
|
|
4065
|
+
if (!consent.ads) await revokeAdsArtifacts(rt);
|
|
2849
4066
|
await pushConsent(rt);
|
|
2850
4067
|
});
|
|
2851
4068
|
};
|
|
@@ -2902,6 +4119,353 @@ const loadScript = (src, id) => {
|
|
|
2902
4119
|
return promise;
|
|
2903
4120
|
};
|
|
2904
4121
|
//#endregion
|
|
4122
|
+
//#region src/contract.validate.ts
|
|
4123
|
+
const EVENT_NAME = /^[A-Za-z][A-Za-z0-9_]{0,39}$/;
|
|
4124
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
4125
|
+
const CURRENCY = /^[A-Z]{3}$/;
|
|
4126
|
+
const COUNTRY = /^[A-Z]{2}$/;
|
|
4127
|
+
const CLICK_ID = /^[\x21-\x7E]{1,500}$/;
|
|
4128
|
+
const SESSION_ATTRIBUTES = /^[\x21-\x7E]{1,4096}$/;
|
|
4129
|
+
const MAX_TEXT = 255;
|
|
4130
|
+
/** Caps the recursive PII strip so hostile nesting cannot overflow it. */
|
|
4131
|
+
const MAX_PROPERTIES_DEPTH = 32;
|
|
4132
|
+
const MINUTE_MS = 6e4;
|
|
4133
|
+
/** Meta rejects a whole batch with an event older than 7 days. */
|
|
4134
|
+
const MAX_AGE_MS = 10080 * MINUTE_MS - 5 * MINUTE_MS;
|
|
4135
|
+
const MAX_SKEW_MS = 5 * MINUTE_MS;
|
|
4136
|
+
const LOCAL_HOSTS = /* @__PURE__ */ new Set([
|
|
4137
|
+
"localhost",
|
|
4138
|
+
"127.0.0.1",
|
|
4139
|
+
"[::1]"
|
|
4140
|
+
]);
|
|
4141
|
+
const KNOWN_PROVIDERS = new Set(SERVER_PROVIDER_IDS);
|
|
4142
|
+
const USER_HASH_KEYS = Object.values(HASHED_USER_WIRE_KEYS);
|
|
4143
|
+
const CLICK_ID_KEYS = Object.values(CLICK_ID_WIRE_KEYS);
|
|
4144
|
+
var Rejection = class extends Error {
|
|
4145
|
+
reason;
|
|
4146
|
+
constructor(reason) {
|
|
4147
|
+
super(reason);
|
|
4148
|
+
this.reason = reason;
|
|
4149
|
+
}
|
|
4150
|
+
};
|
|
4151
|
+
const fail = (reason) => {
|
|
4152
|
+
throw new Rejection(reason);
|
|
4153
|
+
};
|
|
4154
|
+
/** Optional short text: missing → `undefined`, wrong type → rejected. */
|
|
4155
|
+
const optionalText = (value) => {
|
|
4156
|
+
if (value === void 0) return void 0;
|
|
4157
|
+
if (typeof value !== "string" || value.length > MAX_TEXT) return fail("invalid_event");
|
|
4158
|
+
return value.length > 0 ? value : void 0;
|
|
4159
|
+
};
|
|
4160
|
+
const requiredText = (value) => {
|
|
4161
|
+
return optionalText(value) ?? fail("invalid_event");
|
|
4162
|
+
};
|
|
4163
|
+
const parseUrl = (value) => {
|
|
4164
|
+
if (typeof value !== "string") return void 0;
|
|
4165
|
+
try {
|
|
4166
|
+
return new URL(value);
|
|
4167
|
+
} catch {
|
|
4168
|
+
return;
|
|
4169
|
+
}
|
|
4170
|
+
};
|
|
4171
|
+
const isLocal = (url) => {
|
|
4172
|
+
return url.protocol === "http:" && LOCAL_HOSTS.has(url.hostname);
|
|
4173
|
+
};
|
|
4174
|
+
const sourceUrlOf = (value, check) => {
|
|
4175
|
+
const url = parseUrl(value);
|
|
4176
|
+
if (!url) return fail("invalid_source_url");
|
|
4177
|
+
const local = check.allowLocalhost && isLocal(url);
|
|
4178
|
+
if (url.protocol !== "https:" && !local) return fail("invalid_source_url");
|
|
4179
|
+
if (check.origins && !check.origins.has(url.origin)) return fail("origin_not_allowed");
|
|
4180
|
+
return url.href;
|
|
4181
|
+
};
|
|
4182
|
+
/** An unreadable referrer is dropped, never a reason to reject. */
|
|
4183
|
+
const referrerOf = (value) => {
|
|
4184
|
+
const url = parseUrl(value);
|
|
4185
|
+
if (!url) return void 0;
|
|
4186
|
+
return url.protocol === "https:" || url.protocol === "http:" ? url.href : void 0;
|
|
4187
|
+
};
|
|
4188
|
+
const timestampOf = (value, check) => {
|
|
4189
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value)) return fail("invalid_event");
|
|
4190
|
+
if (value < check.now - MAX_AGE_MS) return fail("event_too_old");
|
|
4191
|
+
if (value > check.now + MAX_SKEW_MS) return fail("event_in_future");
|
|
4192
|
+
return value;
|
|
4193
|
+
};
|
|
4194
|
+
const recordOf = (value) => {
|
|
4195
|
+
if (value === void 0) return {};
|
|
4196
|
+
return isRecord(value) ? value : fail("invalid_event");
|
|
4197
|
+
};
|
|
4198
|
+
const isShallow = (value, depth) => {
|
|
4199
|
+
if (!value || typeof value !== "object") return true;
|
|
4200
|
+
if (depth >= MAX_PROPERTIES_DEPTH) return false;
|
|
4201
|
+
return (Array.isArray(value) ? value : Object.values(value)).every((item) => isShallow(item, depth + 1));
|
|
4202
|
+
};
|
|
4203
|
+
const propertiesOf = (value) => {
|
|
4204
|
+
const properties = recordOf(value);
|
|
4205
|
+
if (!isShallow(properties, 0)) return fail("invalid_event");
|
|
4206
|
+
return stripContractPii(properties);
|
|
4207
|
+
};
|
|
4208
|
+
const userOf = (value) => {
|
|
4209
|
+
const raw = recordOf(value);
|
|
4210
|
+
const user = {};
|
|
4211
|
+
for (const key of USER_HASH_KEYS) {
|
|
4212
|
+
const hash = raw[key];
|
|
4213
|
+
if (hash === void 0) continue;
|
|
4214
|
+
if (typeof hash !== "string" || !SHA256.test(hash)) fail("invalid_hash");
|
|
4215
|
+
user[key] = hash;
|
|
4216
|
+
}
|
|
4217
|
+
const country = raw.country_code;
|
|
4218
|
+
if (country === void 0) return user;
|
|
4219
|
+
if (typeof country !== "string" || !COUNTRY.test(country)) return fail("invalid_event");
|
|
4220
|
+
return {
|
|
4221
|
+
...user,
|
|
4222
|
+
country_code: country
|
|
4223
|
+
};
|
|
4224
|
+
};
|
|
4225
|
+
const clickIdsOf = (value) => {
|
|
4226
|
+
const raw = recordOf(value);
|
|
4227
|
+
const clickIds = {};
|
|
4228
|
+
for (const key of CLICK_ID_KEYS) {
|
|
4229
|
+
const id = raw[key];
|
|
4230
|
+
if (id === void 0) continue;
|
|
4231
|
+
if (typeof id !== "string" || !(key === "google_session_attributes" ? SESSION_ATTRIBUTES : CLICK_ID).test(id)) fail("invalid_event");
|
|
4232
|
+
clickIds[key] = id;
|
|
4233
|
+
}
|
|
4234
|
+
return clickIds;
|
|
4235
|
+
};
|
|
4236
|
+
const optionalBoolean = (value) => {
|
|
4237
|
+
if (value === void 0 || typeof value === "boolean") return value;
|
|
4238
|
+
return fail("invalid_event");
|
|
4239
|
+
};
|
|
4240
|
+
const consentOf = (value) => {
|
|
4241
|
+
if (!isRecord(value) || typeof value.ads !== "boolean") return fail("invalid_event");
|
|
4242
|
+
const adUserData = optionalBoolean(value.ad_user_data);
|
|
4243
|
+
const adPersonalization = optionalBoolean(value.ad_personalization);
|
|
4244
|
+
return resolveContractConsent({
|
|
4245
|
+
ads: value.ads,
|
|
4246
|
+
adUserData,
|
|
4247
|
+
adPersonalization
|
|
4248
|
+
});
|
|
4249
|
+
};
|
|
4250
|
+
const contextOf = (value) => {
|
|
4251
|
+
if (!isRecord(value) || value.runtime !== "web") return fail("invalid_event");
|
|
4252
|
+
const c1 = {
|
|
4253
|
+
runtime: "web",
|
|
4254
|
+
locale: optionalText(value.locale)
|
|
4255
|
+
};
|
|
4256
|
+
const c2 = { channel_id: requiredText(value.channel_id) };
|
|
4257
|
+
const c3 = { store_id: optionalText(value.store_id) };
|
|
4258
|
+
const c4 = { brand_id: optionalText(value.brand_id) };
|
|
4259
|
+
const c5 = { fulfillment_id: optionalText(value.fulfillment_id) };
|
|
4260
|
+
return withoutUndefined({
|
|
4261
|
+
...c1,
|
|
4262
|
+
...c2,
|
|
4263
|
+
...c3,
|
|
4264
|
+
...c4,
|
|
4265
|
+
...c5
|
|
4266
|
+
});
|
|
4267
|
+
};
|
|
4268
|
+
const decimalOf = (value) => {
|
|
4269
|
+
if (value === void 0) return void 0;
|
|
4270
|
+
return isDecimalString(value) ? value : fail("invalid_decimal");
|
|
4271
|
+
};
|
|
4272
|
+
const itemOf = (value) => {
|
|
4273
|
+
if (!isRecord(value)) return fail("invalid_event");
|
|
4274
|
+
const i1 = {
|
|
4275
|
+
id: requiredText(value.id),
|
|
4276
|
+
name: optionalText(value.name)
|
|
4277
|
+
};
|
|
4278
|
+
const i2 = { category: optionalText(value.category) };
|
|
4279
|
+
const i3 = { brand: optionalText(value.brand) };
|
|
4280
|
+
const i4 = { price: decimalOf(value.price) };
|
|
4281
|
+
const i5 = { quantity: decimalOf(value.quantity) };
|
|
4282
|
+
return withoutUndefined({
|
|
4283
|
+
...i1,
|
|
4284
|
+
...i2,
|
|
4285
|
+
...i3,
|
|
4286
|
+
...i4,
|
|
4287
|
+
...i5
|
|
4288
|
+
});
|
|
4289
|
+
};
|
|
4290
|
+
const itemsOf = (value) => {
|
|
4291
|
+
if (value === void 0) return void 0;
|
|
4292
|
+
if (!Array.isArray(value)) return fail("invalid_event");
|
|
4293
|
+
return value.map(itemOf);
|
|
4294
|
+
};
|
|
4295
|
+
const currencyOf = (value) => {
|
|
4296
|
+
if (value === void 0) return void 0;
|
|
4297
|
+
if (typeof value === "string" && CURRENCY.test(value)) return value;
|
|
4298
|
+
return fail("invalid_currency");
|
|
4299
|
+
};
|
|
4300
|
+
const providersOf = (value, check) => {
|
|
4301
|
+
if (!Array.isArray(value)) return fail("invalid_event");
|
|
4302
|
+
for (const id of value) if (typeof id !== "string" || !check.providers.has(id)) fail("unknown_provider");
|
|
4303
|
+
return [...new Set(value)];
|
|
4304
|
+
};
|
|
4305
|
+
const withoutUndefined = (record) => {
|
|
4306
|
+
const next = {};
|
|
4307
|
+
for (const [key, value] of Object.entries(record)) if (value !== void 0) next[key] = value;
|
|
4308
|
+
return next;
|
|
4309
|
+
};
|
|
4310
|
+
const parseEvent = (raw, check) => {
|
|
4311
|
+
if (!isValidEventId(raw.event_id)) return fail("invalid_event_id");
|
|
4312
|
+
const name = raw.name;
|
|
4313
|
+
if (typeof name !== "string" || !EVENT_NAME.test(name)) return fail("invalid_name");
|
|
4314
|
+
const e1 = {
|
|
4315
|
+
event_id: raw.event_id,
|
|
4316
|
+
name
|
|
4317
|
+
};
|
|
4318
|
+
const e2 = { timestamp: timestampOf(raw.timestamp, check) };
|
|
4319
|
+
const e3 = { source_url: sourceUrlOf(raw.source_url, check) };
|
|
4320
|
+
const e4 = { referrer_url: referrerOf(raw.referrer_url) };
|
|
4321
|
+
const e5 = { properties: propertiesOf(raw.properties) };
|
|
4322
|
+
const e6 = {
|
|
4323
|
+
user: userOf(raw.user),
|
|
4324
|
+
click_ids: clickIdsOf(raw.click_ids)
|
|
4325
|
+
};
|
|
4326
|
+
const e7 = { consent: consentOf(raw.consent) };
|
|
4327
|
+
const e8 = { context: contextOf(raw.context) };
|
|
4328
|
+
const e9 = {
|
|
4329
|
+
value: decimalOf(raw.value),
|
|
4330
|
+
items: itemsOf(raw.items)
|
|
4331
|
+
};
|
|
4332
|
+
const e10 = { currency: currencyOf(raw.currency) };
|
|
4333
|
+
const e11 = { order_id: optionalText(raw.order_id) };
|
|
4334
|
+
const e12 = { providers: providersOf(raw.providers, check) };
|
|
4335
|
+
const event = {
|
|
4336
|
+
...e1,
|
|
4337
|
+
...e2,
|
|
4338
|
+
...e3,
|
|
4339
|
+
...e4,
|
|
4340
|
+
...e5,
|
|
4341
|
+
...e6,
|
|
4342
|
+
...e7,
|
|
4343
|
+
...e8,
|
|
4344
|
+
...e9,
|
|
4345
|
+
...e10,
|
|
4346
|
+
...e11,
|
|
4347
|
+
...e12
|
|
4348
|
+
};
|
|
4349
|
+
return withoutUndefined(event);
|
|
4350
|
+
};
|
|
4351
|
+
const checkEvent = (raw, check) => {
|
|
4352
|
+
if (!isRecord(raw)) return { reason: "invalid_event" };
|
|
4353
|
+
try {
|
|
4354
|
+
return { value: parseEvent(raw, check) };
|
|
4355
|
+
} catch (error) {
|
|
4356
|
+
if (error instanceof Rejection) return { reason: error.reason };
|
|
4357
|
+
throw error;
|
|
4358
|
+
}
|
|
4359
|
+
};
|
|
4360
|
+
const checkOf = (options) => {
|
|
4361
|
+
const parsed = options.allowedOrigins?.flatMap((origin) => parseUrl(origin)?.origin ?? []);
|
|
4362
|
+
const origins = parsed ? new Set(parsed) : null;
|
|
4363
|
+
const known = (options.providers ?? SERVER_PROVIDER_IDS).filter((id) => KNOWN_PROVIDERS.has(id));
|
|
4364
|
+
const providers = new Set(known);
|
|
4365
|
+
return {
|
|
4366
|
+
now: options.now ?? Date.now(),
|
|
4367
|
+
origins,
|
|
4368
|
+
providers,
|
|
4369
|
+
allowLocalhost: options.allowLocalhost === true
|
|
4370
|
+
};
|
|
4371
|
+
};
|
|
4372
|
+
const parseJson = (input) => {
|
|
4373
|
+
if (typeof input !== "string") return { value: input };
|
|
4374
|
+
try {
|
|
4375
|
+
return { value: JSON.parse(input) };
|
|
4376
|
+
} catch {
|
|
4377
|
+
return;
|
|
4378
|
+
}
|
|
4379
|
+
};
|
|
4380
|
+
const isSupported = (version) => {
|
|
4381
|
+
return SUPPORTED_CONTRACT_VERSIONS.includes(version);
|
|
4382
|
+
};
|
|
4383
|
+
const hasSdk = (value) => {
|
|
4384
|
+
if (!isRecord(value)) return false;
|
|
4385
|
+
const { name, version } = value;
|
|
4386
|
+
return typeof name === "string" && !!name && typeof version === "string";
|
|
4387
|
+
};
|
|
4388
|
+
const isEnvelopeShape = (raw) => {
|
|
4389
|
+
if (!hasSdk(raw.sdk)) return false;
|
|
4390
|
+
const sentAt = raw.sent_at;
|
|
4391
|
+
if (typeof sentAt !== "string" || Number.isNaN(Date.parse(sentAt))) return false;
|
|
4392
|
+
const { events } = raw;
|
|
4393
|
+
if (!Array.isArray(events)) return false;
|
|
4394
|
+
return events.length > 0 && events.length <= 20;
|
|
4395
|
+
};
|
|
4396
|
+
/**
|
|
4397
|
+
* Brings an envelope of a supported older version to the current one.
|
|
4398
|
+
* Version 1 is current, so it is returned as is.
|
|
4399
|
+
*/
|
|
4400
|
+
const upgradeEnvelope = (envelope) => {
|
|
4401
|
+
return envelope;
|
|
4402
|
+
};
|
|
4403
|
+
/**
|
|
4404
|
+
* Validates a relay body (a JSON string or a parsed value). A malformed
|
|
4405
|
+
* body or an unsupported `v` fails as a whole; otherwise every event is
|
|
4406
|
+
* checked on its own and the invalid ones are listed in `rejected`. The
|
|
4407
|
+
* returned envelope is a clean copy: unknown fields and personal params
|
|
4408
|
+
* (`email`, `phone`, `first_name`, `last_name`, `device_ip`, `ttclid`) are
|
|
4409
|
+
* dropped, and `consent.ad_user_data` / `ad_personalization` default to
|
|
4410
|
+
* `ads`. Field names are snake_case, as on the wire.
|
|
4411
|
+
*/
|
|
4412
|
+
const validateEnvelope = (input, options = {}) => {
|
|
4413
|
+
const parsed = parseJson(input);
|
|
4414
|
+
if (!parsed) return {
|
|
4415
|
+
ok: false,
|
|
4416
|
+
error: "invalid_json"
|
|
4417
|
+
};
|
|
4418
|
+
const raw = parsed.value;
|
|
4419
|
+
if (!isRecord(raw)) return {
|
|
4420
|
+
ok: false,
|
|
4421
|
+
error: "invalid_envelope"
|
|
4422
|
+
};
|
|
4423
|
+
const { v } = raw;
|
|
4424
|
+
if (typeof v !== "number" || !Number.isInteger(v)) return {
|
|
4425
|
+
ok: false,
|
|
4426
|
+
error: "invalid_envelope"
|
|
4427
|
+
};
|
|
4428
|
+
if (!isSupported(v)) return {
|
|
4429
|
+
ok: false,
|
|
4430
|
+
error: "unsupported_contract_version"
|
|
4431
|
+
};
|
|
4432
|
+
const current = upgradeEnvelope(raw);
|
|
4433
|
+
if (!isEnvelopeShape(current)) return {
|
|
4434
|
+
ok: false,
|
|
4435
|
+
error: "invalid_envelope"
|
|
4436
|
+
};
|
|
4437
|
+
const check = checkOf(options);
|
|
4438
|
+
const events = [];
|
|
4439
|
+
const rejected = [];
|
|
4440
|
+
for (const item of current.events) {
|
|
4441
|
+
const outcome = checkEvent(item, check);
|
|
4442
|
+
if ("value" in outcome) {
|
|
4443
|
+
events.push(outcome.value);
|
|
4444
|
+
continue;
|
|
4445
|
+
}
|
|
4446
|
+
const id = isRecord(item) ? item.event_id : void 0;
|
|
4447
|
+
const event_id = isValidEventId(id) ? id : void 0;
|
|
4448
|
+
rejected.push(withoutUndefined({
|
|
4449
|
+
event_id,
|
|
4450
|
+
reason: outcome.reason
|
|
4451
|
+
}));
|
|
4452
|
+
}
|
|
4453
|
+
const sdk = current.sdk;
|
|
4454
|
+
return {
|
|
4455
|
+
ok: true,
|
|
4456
|
+
envelope: {
|
|
4457
|
+
v: 1,
|
|
4458
|
+
sdk: {
|
|
4459
|
+
name: sdk.name,
|
|
4460
|
+
version: sdk.version
|
|
4461
|
+
},
|
|
4462
|
+
sent_at: current.sent_at,
|
|
4463
|
+
events
|
|
4464
|
+
},
|
|
4465
|
+
rejected
|
|
4466
|
+
};
|
|
4467
|
+
};
|
|
4468
|
+
//#endregion
|
|
2905
4469
|
//#region src/index.ts
|
|
2906
4470
|
const configure = (options) => {
|
|
2907
4471
|
return configureWith(runtime, options);
|
|
@@ -2969,4 +4533,4 @@ const analytics = {
|
|
|
2969
4533
|
setConsent
|
|
2970
4534
|
};
|
|
2971
4535
|
//#endregion
|
|
2972
|
-
export { Analytics, BUILTIN_DESTINATION_IDS, FUNNEL_EVENTS, INTERACTION_EVENTS, SDK_VERSION, analytics, compactPayload, configure, customAdapter, endSession, flush, hasScript, isFunnelEventName, isInteractionEventName, loadScript, reset, setConsent, setUser, track };
|
|
4536
|
+
export { Analytics, AnalyticsConfigError, BUILTIN_DESTINATION_IDS, CLICK_ID_STORAGE_PREFIX, CLICK_ID_TTL_MS, CLICK_ID_WIRE_KEYS, CONTRACT_PII_KEYS, FUNNEL_EVENTS, HASHED_USER_WIRE_KEYS, INTERACTION_EVENTS, MAX_ENVELOPE_EVENTS, SDK_VERSION, SERVER_PROVIDER_IDS, SUPPORTED_CONTRACT_VERSIONS, TRACKING_CONTRACT_VERSION, TRACKING_SDK_NAME, analytics, buildTrackingEnvelope, buildTrackingEvent, captureClickIds, clearClickIds, compactPayload, configure, customAdapter, endSession, fbcFromFbclid, flush, gclidFromGclAw, hasScript, hashUserData, isDecimalString, isFunnelEventName, isInteractionEventName, isValidEventId, loadScript, normalizeCountryCode, normalizeEmail, normalizeEmailForGoogle, normalizeExternalId, normalizeName, normalizePhoneE164, parseClickIdParams, phoneDigits, purchaseEventId, readCookie, reset, resolveDeliveryMode, setConsent, setUser, sha256Hex, toDecimalString, track, upgradeEnvelope, validateEnvelope };
|