@azzas/azzas-tracker-web 2.0.2-preview.4 → 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.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 : "";
@@ -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
- return Object.fromEntries(entries);
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 (isDebugMode()) {
2150
- console.log(`[DT v2] ${event}, context e parameters: `, {
2151
- context,
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
- return await pushToDataLayer(
2156
- event,
2157
- Object.assign({}, parameters, { window: context.window })
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
  }