@azzas/azzas-tracker-web 2.0.2-preview.3 → 2.0.2-preview.5
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/mod.cjs +163 -21
- package/dist/mod.cjs.map +1 -1
- package/dist/mod.d.cts +3 -2
- package/dist/mod.d.ts +3 -2
- package/dist/mod.global.js +163 -21
- package/dist/mod.global.js.map +1 -1
- package/dist/mod.js +163 -21
- package/dist/mod.js.map +1 -1
- package/dist/mod.vtex.global.js +157 -8
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -127,6 +127,7 @@ var EVENTS = {
|
|
|
127
127
|
"region",
|
|
128
128
|
"currency",
|
|
129
129
|
"promotion_name",
|
|
130
|
+
"promotion_url",
|
|
130
131
|
"creative_slot",
|
|
131
132
|
"creative_name"
|
|
132
133
|
// "items",
|
|
@@ -930,6 +931,23 @@ var isDebugMode = () => {
|
|
|
930
931
|
return window.location.search.indexOf("__trk_inspect__") !== -1;
|
|
931
932
|
}
|
|
932
933
|
};
|
|
934
|
+
function setCookie(name, value, days = 7, domain) {
|
|
935
|
+
if (typeof document === "undefined") return;
|
|
936
|
+
const encodedValue = encodeURIComponent(value);
|
|
937
|
+
let expires = "";
|
|
938
|
+
if (days) {
|
|
939
|
+
const date = /* @__PURE__ */ new Date();
|
|
940
|
+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1e3);
|
|
941
|
+
expires = "; expires=" + date.toUTCString();
|
|
942
|
+
}
|
|
943
|
+
const domainAttr = domain ? `; domain=.${domain.replace(/^\./, "")}` : "";
|
|
944
|
+
document.cookie = `${name}=${encodedValue}${expires}${domainAttr}; path=/;`;
|
|
945
|
+
}
|
|
946
|
+
function getCookie(name) {
|
|
947
|
+
if (typeof document === "undefined") return null;
|
|
948
|
+
const match = document.cookie.split("; ").find((row) => row.startsWith(`${name}=`));
|
|
949
|
+
return match ? decodeURIComponent(match.slice(name.length + 1)) : null;
|
|
950
|
+
}
|
|
933
951
|
var paymentTypeMap = [
|
|
934
952
|
["vale", "vale cr\xE9dito"],
|
|
935
953
|
["pagaleve", "pix"],
|
|
@@ -990,6 +1008,23 @@ var resizeVtexImage2 = (url, width = 500, height = 500) => {
|
|
|
990
1008
|
`$1-${width}-${height}/`
|
|
991
1009
|
);
|
|
992
1010
|
};
|
|
1011
|
+
function cleanPath(path) {
|
|
1012
|
+
try {
|
|
1013
|
+
return decodeURIComponent(path).toLowerCase().replace(/\/+$/, "");
|
|
1014
|
+
} catch (e) {
|
|
1015
|
+
return path.toLowerCase().replace(/\/+$/, "");
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
function matchesPromotion(listName, promotion) {
|
|
1019
|
+
if (!listName || !promotion.promotion_url) return false;
|
|
1020
|
+
let promotionPath;
|
|
1021
|
+
try {
|
|
1022
|
+
promotionPath = new URL(promotion.promotion_url).pathname;
|
|
1023
|
+
} catch (e) {
|
|
1024
|
+
promotionPath = promotion.promotion_url;
|
|
1025
|
+
}
|
|
1026
|
+
return cleanPath(promotionPath) === cleanPath(listName);
|
|
1027
|
+
}
|
|
993
1028
|
|
|
994
1029
|
// src/params/legacy/resolvers/paymentType/fromOrderForm.ts
|
|
995
1030
|
function paymentTypeFromOrderForm(context) {
|
|
@@ -1368,6 +1403,9 @@ var metaAdapter = {
|
|
|
1368
1403
|
promotion_name: (context) => {
|
|
1369
1404
|
return context.meta.promotion_name || null;
|
|
1370
1405
|
},
|
|
1406
|
+
promotion_url: (context) => {
|
|
1407
|
+
return context.meta.promotion_url || null;
|
|
1408
|
+
},
|
|
1371
1409
|
creative_name: (context) => {
|
|
1372
1410
|
return context.meta.creative_name || null;
|
|
1373
1411
|
},
|
|
@@ -1555,7 +1593,7 @@ var getItemCategory5 = (item) => {
|
|
|
1555
1593
|
};
|
|
1556
1594
|
var getItemCategory23 = (item) => {
|
|
1557
1595
|
var _a, _b, _c, _d;
|
|
1558
|
-
const AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção)$/i;
|
|
1596
|
+
const AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção|promoção)$/i;
|
|
1559
1597
|
const categories = (_c = (_b = (_a = item == null ? void 0 : item.similarItems) == null ? void 0 : _a[0]) == null ? void 0 : _b.categories) != null ? _c : [];
|
|
1560
1598
|
const validHierarchy = categories.filter((c) => c && !AVOID_CATEGORIES.test(c));
|
|
1561
1599
|
const deepest = (_d = validHierarchy[validHierarchy.length - 1]) != null ? _d : "";
|
|
@@ -1570,7 +1608,7 @@ var reservaAdapter = {
|
|
|
1570
1608
|
if (context == null ? void 0 : context.items) return context.items;
|
|
1571
1609
|
if (context == null ? void 0 : context.products) {
|
|
1572
1610
|
return context.products.map((item) => {
|
|
1573
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D;
|
|
1611
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E;
|
|
1574
1612
|
const listPrice = (_b = (_a = item == null ? void 0 : item.offers) == null ? void 0 : _a.offers) == null ? void 0 : _b[0].listPrice;
|
|
1575
1613
|
return {
|
|
1576
1614
|
quantity: 1,
|
|
@@ -1581,18 +1619,18 @@ var reservaAdapter = {
|
|
|
1581
1619
|
discount: Math.round((Number(listPrice) - Number((_j = item == null ? void 0 : item.offers) == null ? void 0 : _j.lowPrice)) * 100) / 100,
|
|
1582
1620
|
item_variant: ((_l = (_k = item == null ? void 0 : item.name) == null ? void 0 : _k.split(" - ")[0]) == null ? void 0 : _l.replace(/^[^_]+_/, "").replace(/_/g, " ")) || null,
|
|
1583
1621
|
// cor
|
|
1584
|
-
item_variant2: (_n = (_m = item == null ? void 0 : item.name) == null ? void 0 : _m.split(" - ")[1].toUpperCase()) != null ?
|
|
1622
|
+
item_variant2: (_o = (_n = (_m = item == null ? void 0 : item.name) == null ? void 0 : _m.split(" - ")[1]) == null ? void 0 : _n.toUpperCase()) != null ? _o : null,
|
|
1585
1623
|
// tamanho
|
|
1586
1624
|
item_shipping_tier: "",
|
|
1587
|
-
item_ref: (
|
|
1588
|
-
item_category: (
|
|
1589
|
-
item_category2: (
|
|
1590
|
-
item_id: (
|
|
1591
|
-
item_sku: (
|
|
1592
|
-
item_url: (
|
|
1593
|
-
image_url: (
|
|
1594
|
-
seller_id: (_C = (_B = (_A = (_z =
|
|
1595
|
-
item_list_name: (
|
|
1625
|
+
item_ref: (_p = item.groupRefId) != null ? _p : null,
|
|
1626
|
+
item_category: (_q = getItemCategory5(item)) != null ? _q : null,
|
|
1627
|
+
item_category2: (_r = getItemCategory23(item)) != null ? _r : null,
|
|
1628
|
+
item_id: (_t = (_s = item == null ? void 0 : item.isVariantOf) == null ? void 0 : _s.productGroupID) != null ? _t : null,
|
|
1629
|
+
item_sku: (_u = item == null ? void 0 : item.sku) != null ? _u : null,
|
|
1630
|
+
item_url: (_w = (_v = item == null ? void 0 : item.seo) == null ? void 0 : _v.canonical) != null ? _w : null,
|
|
1631
|
+
image_url: (_y = resizeVtexImage2((_x = item == null ? void 0 : item.image) == null ? void 0 : _x[0].url)) != null ? _y : null,
|
|
1632
|
+
seller_id: (_D = (_C = (_B = (_A = (_z = item.offers) == null ? void 0 : _z.offers) == null ? void 0 : _A[0]) == null ? void 0 : _B.seller) == null ? void 0 : _C.identifier) != null ? _D : null,
|
|
1633
|
+
item_list_name: (_E = item == null ? void 0 : item.item_list_name) != null ? _E : null
|
|
1596
1634
|
};
|
|
1597
1635
|
});
|
|
1598
1636
|
}
|
|
@@ -2110,6 +2148,94 @@ var orderFormAdapter = {
|
|
|
2110
2148
|
}
|
|
2111
2149
|
};
|
|
2112
2150
|
|
|
2151
|
+
// src/core/promotion.ts
|
|
2152
|
+
var PROMO_COOKIE = "__azzas_trk_promo";
|
|
2153
|
+
var PROMO_TTL_MS = 10 * 60 * 1e3;
|
|
2154
|
+
var MAX_RELATED_KEYS = 30;
|
|
2155
|
+
var cachedRootDomain;
|
|
2156
|
+
function resolveRootDomain() {
|
|
2157
|
+
if (cachedRootDomain !== void 0) return cachedRootDomain;
|
|
2158
|
+
if (typeof document === "undefined") return null;
|
|
2159
|
+
const hostname = window.location.hostname;
|
|
2160
|
+
const isLocalhost = hostname === "localhost";
|
|
2161
|
+
const isIpAddress = /^[\d.]+$/.test(hostname);
|
|
2162
|
+
if (!hostname || isLocalhost || isIpAddress) {
|
|
2163
|
+
cachedRootDomain = null;
|
|
2164
|
+
return null;
|
|
2165
|
+
}
|
|
2166
|
+
const parts = hostname.split(".");
|
|
2167
|
+
const PROBE = "__azzas_trk_probe";
|
|
2168
|
+
for (let i = parts.length - 2; i >= 0; i--) {
|
|
2169
|
+
const candidate = parts.slice(i).join(".");
|
|
2170
|
+
document.cookie = `${PROBE}=1; domain=.${candidate}; path=/;`;
|
|
2171
|
+
if (document.cookie.includes(`${PROBE}=`)) {
|
|
2172
|
+
document.cookie = `${PROBE}=; domain=.${candidate}; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
2173
|
+
cachedRootDomain = candidate;
|
|
2174
|
+
return candidate;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
cachedRootDomain = null;
|
|
2178
|
+
return null;
|
|
2179
|
+
}
|
|
2180
|
+
function writePromotion(promotion) {
|
|
2181
|
+
const remainingDays = (promotion.saved_at + PROMO_TTL_MS - Date.now()) / (24 * 60 * 60 * 1e3);
|
|
2182
|
+
if (remainingDays <= 0) return;
|
|
2183
|
+
setCookie(PROMO_COOKIE, JSON.stringify(promotion), remainingDays, resolveRootDomain());
|
|
2184
|
+
}
|
|
2185
|
+
function savePromotion(input) {
|
|
2186
|
+
writePromotion({
|
|
2187
|
+
...input,
|
|
2188
|
+
related_keys: [],
|
|
2189
|
+
saved_at: Date.now()
|
|
2190
|
+
});
|
|
2191
|
+
}
|
|
2192
|
+
function getPromotion() {
|
|
2193
|
+
const raw = getCookie(PROMO_COOKIE);
|
|
2194
|
+
if (!raw) return null;
|
|
2195
|
+
try {
|
|
2196
|
+
const promotion = JSON.parse(raw);
|
|
2197
|
+
const isExpired = !promotion.saved_at || Date.now() - promotion.saved_at > PROMO_TTL_MS;
|
|
2198
|
+
if (isExpired) {
|
|
2199
|
+
clearPromotion();
|
|
2200
|
+
return null;
|
|
2201
|
+
}
|
|
2202
|
+
return promotion;
|
|
2203
|
+
} catch (e) {
|
|
2204
|
+
return null;
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
function clearPromotion() {
|
|
2208
|
+
setCookie(PROMO_COOKIE, "", -1, resolveRootDomain());
|
|
2209
|
+
}
|
|
2210
|
+
function itemsWithPromotion(items) {
|
|
2211
|
+
const promotion = getPromotion();
|
|
2212
|
+
if (!promotion) return items;
|
|
2213
|
+
const relatedKeys = new Set(promotion.related_keys);
|
|
2214
|
+
const newKeys = [];
|
|
2215
|
+
const enrichedItems = items.map((item) => {
|
|
2216
|
+
const itemKeys = [item.item_id].filter((key) => Boolean(key));
|
|
2217
|
+
const alreadyRelated = itemKeys.some((key) => relatedKeys.has(key));
|
|
2218
|
+
const isRelated = alreadyRelated || matchesPromotion(item.item_list_name, promotion);
|
|
2219
|
+
if (!isRelated) return item;
|
|
2220
|
+
if (!alreadyRelated) {
|
|
2221
|
+
newKeys.push(...itemKeys);
|
|
2222
|
+
}
|
|
2223
|
+
return {
|
|
2224
|
+
...item,
|
|
2225
|
+
promotion_name: promotion.promotion_name,
|
|
2226
|
+
creative_name: promotion.creative_name,
|
|
2227
|
+
creative_slot: promotion.creative_slot
|
|
2228
|
+
};
|
|
2229
|
+
});
|
|
2230
|
+
if (newKeys.length) {
|
|
2231
|
+
writePromotion({
|
|
2232
|
+
...promotion,
|
|
2233
|
+
related_keys: [...promotion.related_keys, ...newKeys].slice(0, MAX_RELATED_KEYS)
|
|
2234
|
+
});
|
|
2235
|
+
}
|
|
2236
|
+
return enrichedItems;
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2113
2239
|
// src/formatter.ts
|
|
2114
2240
|
var adapters = {
|
|
2115
2241
|
DECO: decoAdapter,
|
|
@@ -2135,7 +2261,11 @@ async function getParameters(context, eventName, config2) {
|
|
|
2135
2261
|
return [param, await getter(context, config2)];
|
|
2136
2262
|
})
|
|
2137
2263
|
);
|
|
2138
|
-
|
|
2264
|
+
const parameters = Object.fromEntries(entries);
|
|
2265
|
+
if (Array.isArray(parameters.items) && parameters.items.length) {
|
|
2266
|
+
parameters.items = itemsWithPromotion(parameters.items);
|
|
2267
|
+
}
|
|
2268
|
+
return parameters;
|
|
2139
2269
|
}
|
|
2140
2270
|
|
|
2141
2271
|
// src/index.ts
|
|
@@ -2144,18 +2274,30 @@ function initTracker(c) {
|
|
|
2144
2274
|
config = c;
|
|
2145
2275
|
}
|
|
2146
2276
|
async function trackWebEvent(event, context) {
|
|
2277
|
+
var _a, _b, _c, _d;
|
|
2147
2278
|
try {
|
|
2148
2279
|
const parameters = await getParameters(context, event, config);
|
|
2149
|
-
if (
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
parameters
|
|
2280
|
+
if (event === "SELECT_PROMOTION" && parameters) {
|
|
2281
|
+
savePromotion({
|
|
2282
|
+
promotion_name: (_a = parameters.promotion_name) != null ? _a : null,
|
|
2283
|
+
creative_name: (_b = parameters.creative_name) != null ? _b : null,
|
|
2284
|
+
creative_slot: (_c = parameters.creative_slot) != null ? _c : null,
|
|
2285
|
+
promotion_url: (_d = parameters.promotion_url) != null ? _d : null
|
|
2153
2286
|
});
|
|
2154
2287
|
}
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2288
|
+
if (isDebugMode()) {
|
|
2289
|
+
console.log(
|
|
2290
|
+
`%c [DT v2] %c ${event} `,
|
|
2291
|
+
"background: #6366f1; color: #fff; font-weight: bold; padding: 2px 6px; border-radius: 4px 0 0 4px;",
|
|
2292
|
+
"background: #1e1e2e; color: #a5b4fc; font-weight: bold; padding: 2px 6px; border-radius: 0 4px 4px 0;",
|
|
2293
|
+
{ context, parameters }
|
|
2294
|
+
);
|
|
2295
|
+
}
|
|
2296
|
+
const result = await pushToDataLayer(event, Object.assign({}, parameters, { window: context.window }));
|
|
2297
|
+
if (event === "PURCHASE" || event === "CUSTOM_PURCHASE") {
|
|
2298
|
+
clearPromotion();
|
|
2299
|
+
}
|
|
2300
|
+
return result;
|
|
2159
2301
|
} catch (err) {
|
|
2160
2302
|
return console.error(`[DT] Error tracking event ${event}:`, err);
|
|
2161
2303
|
}
|