@azzas/azzas-tracker-web 2.0.2-preview.4 → 2.0.2-preview.6

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 CHANGED
@@ -156,6 +156,7 @@ var EVENTS = {
156
156
  "region",
157
157
  "currency",
158
158
  "promotion_name",
159
+ "promotion_url",
159
160
  "creative_slot",
160
161
  "creative_name"
161
162
  // "items",
@@ -959,6 +960,23 @@ var isDebugMode = () => {
959
960
  return window.location.search.indexOf("__trk_inspect__") !== -1;
960
961
  }
961
962
  };
963
+ function setCookie(name, value, days = 7, domain) {
964
+ if (typeof document === "undefined") return;
965
+ const encodedValue = encodeURIComponent(value);
966
+ let expires = "";
967
+ if (days) {
968
+ const date = /* @__PURE__ */ new Date();
969
+ date.setTime(date.getTime() + days * 24 * 60 * 60 * 1e3);
970
+ expires = "; expires=" + date.toUTCString();
971
+ }
972
+ const domainAttr = domain ? `; domain=.${domain.replace(/^\./, "")}` : "";
973
+ document.cookie = `${name}=${encodedValue}${expires}${domainAttr}; path=/;`;
974
+ }
975
+ function getCookie(name) {
976
+ if (typeof document === "undefined") return null;
977
+ const match = document.cookie.split("; ").find((row) => row.startsWith(`${name}=`));
978
+ return match ? decodeURIComponent(match.slice(name.length + 1)) : null;
979
+ }
962
980
  var paymentTypeMap = [
963
981
  ["vale", "vale cr\xE9dito"],
964
982
  ["pagaleve", "pix"],
@@ -1019,6 +1037,23 @@ var resizeVtexImage2 = (url, width = 500, height = 500) => {
1019
1037
  `$1-${width}-${height}/`
1020
1038
  );
1021
1039
  };
1040
+ function cleanPath(path) {
1041
+ try {
1042
+ return decodeURIComponent(path).toLowerCase().replace(/\/+$/, "");
1043
+ } catch (e) {
1044
+ return path.toLowerCase().replace(/\/+$/, "");
1045
+ }
1046
+ }
1047
+ function matchesPromotion(listName, promotion) {
1048
+ if (!listName || !promotion.promotion_url) return false;
1049
+ let promotionPath;
1050
+ try {
1051
+ promotionPath = new URL(promotion.promotion_url).pathname;
1052
+ } catch (e) {
1053
+ promotionPath = promotion.promotion_url;
1054
+ }
1055
+ return cleanPath(promotionPath) === cleanPath(listName);
1056
+ }
1022
1057
 
1023
1058
  // src/params/legacy/resolvers/paymentType/fromOrderForm.ts
1024
1059
  function paymentTypeFromOrderForm(context) {
@@ -1096,6 +1131,10 @@ var PROD_DOMAINS = {
1096
1131
  "lojafabula": {
1097
1132
  "store": "https://lojafabula.myvtex.com",
1098
1133
  "checkout": "https://secure.lojafabula.com.br"
1134
+ },
1135
+ "lojausereserva": {
1136
+ "store": "https://lojausereserva.myvtex.com",
1137
+ "checkout": "https://lojausereserva.myvtex.com"
1099
1138
  }
1100
1139
  };
1101
1140
  async function getUserInfo(context) {
@@ -1397,6 +1436,9 @@ var metaAdapter = {
1397
1436
  promotion_name: (context) => {
1398
1437
  return context.meta.promotion_name || null;
1399
1438
  },
1439
+ promotion_url: (context) => {
1440
+ return context.meta.promotion_url || null;
1441
+ },
1400
1442
  creative_name: (context) => {
1401
1443
  return context.meta.creative_name || null;
1402
1444
  },
@@ -1584,7 +1626,7 @@ var getItemCategory5 = (item) => {
1584
1626
  };
1585
1627
  var getItemCategory23 = (item) => {
1586
1628
  var _a, _b, _c, _d;
1587
- const AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção)$/i;
1629
+ const AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção|promoção)$/i;
1588
1630
  const categories = (_c = (_b = (_a = item == null ? void 0 : item.similarItems) == null ? void 0 : _a[0]) == null ? void 0 : _b.categories) != null ? _c : [];
1589
1631
  const validHierarchy = categories.filter((c) => c && !AVOID_CATEGORIES.test(c));
1590
1632
  const deepest = (_d = validHierarchy[validHierarchy.length - 1]) != null ? _d : "";
@@ -1935,7 +1977,7 @@ var getItemCategory7 = (item) => {
1935
1977
  };
1936
1978
  var getItemCategory25 = (item) => {
1937
1979
  var _a, _b;
1938
- const AVOID_CATEGORIES = /outlet|bazar|sale/i;
1980
+ const AVOID_CATEGORIES = /outlet|bazar|sale|coleção|promoção/i;
1939
1981
  const categories = Object.values((_a = item == null ? void 0 : item.productCategories) != null ? _a : {});
1940
1982
  const validHierarchy = categories.map((c) => c.trim()).filter((c) => c && !AVOID_CATEGORIES.test(c));
1941
1983
  const deepest = (_b = validHierarchy[validHierarchy.length - 1]) != null ? _b : "";
@@ -1974,28 +2016,28 @@ var orderFormAdapter = {
1974
2016
  if (context == null ? void 0 : context.items) return context.items;
1975
2017
  if ((context == null ? void 0 : context.orderForm) && (context == null ? void 0 : context.orderForm.items)) {
1976
2018
  return getOrderFormItems(context.orderForm).map((item, index) => {
1977
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
2019
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
1978
2020
  const color = context.brand === "carolbassi" ? null : (_c = (_b = (_a = item == null ? void 0 : item.skuName) == null ? void 0 : _a.split(" - ")[0]) == null ? void 0 : _b.split("_").pop()) == null ? void 0 : _c.trim();
1979
2021
  const size = context.brand === "carolbassi" ? item == null ? void 0 : item.skuName : (_e = (_d = item == null ? void 0 : item.skuName) == null ? void 0 : _d.split(" - ")[1]) == null ? void 0 : _e.trim();
1980
2022
  return {
1981
2023
  quantity: (_f = item == null ? void 0 : item.quantity) != null ? _f : 1,
1982
2024
  index: (_h = (_g = context == null ? void 0 : context.orderForm) == null ? void 0 : _g.index) != null ? _h : index + 1,
1983
- item_brand: (_j = (_i = item.additionalInfo) == null ? void 0 : _i.brandName) != null ? _j : null,
1984
- item_name: (_k = item == null ? void 0 : item.name.replace(item == null ? void 0 : item.skuName, "").trim()) != null ? _k : null,
2025
+ item_brand: (_k = (_j = (_i = item.additionalInfo) == null ? void 0 : _i.brandName) == null ? void 0 : _j.toLowerCase()) != null ? _k : null,
2026
+ item_name: (_l = item == null ? void 0 : item.name.replace(item == null ? void 0 : item.skuName, "").trim()) != null ? _l : null,
1985
2027
  price: (item == null ? void 0 : item.sellingPrice) / 100,
1986
2028
  discount: ((item == null ? void 0 : item.listPrice) - (item == null ? void 0 : item.sellingPrice)) / 100,
1987
2029
  item_variant: color != null ? color : null,
1988
2030
  item_variant2: size != null ? size : null,
1989
2031
  item_shipping_tier: getItemShippingTier2(context.orderForm, item),
1990
2032
  item_ref: (item == null ? void 0 : item.productRefId) || (item == null ? void 0 : item.refId.replace(/_\d+$/, "")) || null,
1991
- item_category: (_l = getItemCategory7(item)) != null ? _l : null,
1992
- item_category2: (_m = getItemCategory25(item)) != null ? _m : null,
1993
- item_id: (_n = item == null ? void 0 : item.productId) != null ? _n : null,
1994
- item_sku: (_o = item == null ? void 0 : item.id) != null ? _o : null,
1995
- item_url: (_p = item.detailUrl) != null ? _p : null,
1996
- image_url: (_q = resizeVtexImage2(item.imageUrl)) != null ? _q : null,
1997
- seller_id: (_r = item == null ? void 0 : item.seller) != null ? _r : null,
1998
- item_list_name: (_t = (_s = context == null ? void 0 : context.orderForm) == null ? void 0 : _s.item_list_name) != null ? _t : null
2033
+ item_category: (_m = getItemCategory7(item)) != null ? _m : null,
2034
+ item_category2: (_n = getItemCategory25(item)) != null ? _n : null,
2035
+ item_id: (_o = item == null ? void 0 : item.productId) != null ? _o : null,
2036
+ item_sku: (_p = item == null ? void 0 : item.id) != null ? _p : null,
2037
+ item_url: (_q = item.detailUrl) != null ? _q : null,
2038
+ image_url: (_r = resizeVtexImage2(item.imageUrl)) != null ? _r : null,
2039
+ seller_id: (_s = item == null ? void 0 : item.seller) != null ? _s : null,
2040
+ item_list_name: (_u = (_t = context == null ? void 0 : context.orderForm) == null ? void 0 : _t.item_list_name) != null ? _u : null
1999
2041
  };
2000
2042
  });
2001
2043
  }
@@ -2139,6 +2181,94 @@ var orderFormAdapter = {
2139
2181
  }
2140
2182
  };
2141
2183
 
2184
+ // src/core/promotion.ts
2185
+ var PROMO_COOKIE = "__azzas_trk_promo";
2186
+ var PROMO_TTL_MS = 10 * 60 * 1e3;
2187
+ var MAX_RELATED_KEYS = 30;
2188
+ var cachedRootDomain;
2189
+ function resolveRootDomain() {
2190
+ if (cachedRootDomain !== void 0) return cachedRootDomain;
2191
+ if (typeof document === "undefined") return null;
2192
+ const hostname = window.location.hostname;
2193
+ const isLocalhost = hostname === "localhost";
2194
+ const isIpAddress = /^[\d.]+$/.test(hostname);
2195
+ if (!hostname || isLocalhost || isIpAddress) {
2196
+ cachedRootDomain = null;
2197
+ return null;
2198
+ }
2199
+ const parts = hostname.split(".");
2200
+ const PROBE = "__azzas_trk_probe";
2201
+ for (let i = parts.length - 2; i >= 0; i--) {
2202
+ const candidate = parts.slice(i).join(".");
2203
+ document.cookie = `${PROBE}=1; domain=.${candidate}; path=/;`;
2204
+ if (document.cookie.includes(`${PROBE}=`)) {
2205
+ document.cookie = `${PROBE}=; domain=.${candidate}; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
2206
+ cachedRootDomain = candidate;
2207
+ return candidate;
2208
+ }
2209
+ }
2210
+ cachedRootDomain = null;
2211
+ return null;
2212
+ }
2213
+ function writePromotion(promotion) {
2214
+ const remainingDays = (promotion.saved_at + PROMO_TTL_MS - Date.now()) / (24 * 60 * 60 * 1e3);
2215
+ if (remainingDays <= 0) return;
2216
+ setCookie(PROMO_COOKIE, JSON.stringify(promotion), remainingDays, resolveRootDomain());
2217
+ }
2218
+ function savePromotion(input) {
2219
+ writePromotion({
2220
+ ...input,
2221
+ related_keys: [],
2222
+ saved_at: Date.now()
2223
+ });
2224
+ }
2225
+ function getPromotion() {
2226
+ const raw = getCookie(PROMO_COOKIE);
2227
+ if (!raw) return null;
2228
+ try {
2229
+ const promotion = JSON.parse(raw);
2230
+ const isExpired = !promotion.saved_at || Date.now() - promotion.saved_at > PROMO_TTL_MS;
2231
+ if (isExpired) {
2232
+ clearPromotion();
2233
+ return null;
2234
+ }
2235
+ return promotion;
2236
+ } catch (e) {
2237
+ return null;
2238
+ }
2239
+ }
2240
+ function clearPromotion() {
2241
+ setCookie(PROMO_COOKIE, "", -1, resolveRootDomain());
2242
+ }
2243
+ function itemsWithPromotion(items) {
2244
+ const promotion = getPromotion();
2245
+ if (!promotion) return items;
2246
+ const relatedKeys = new Set(promotion.related_keys);
2247
+ const newKeys = [];
2248
+ const enrichedItems = items.map((item) => {
2249
+ const itemKeys = [item.item_id].filter((key) => Boolean(key));
2250
+ const alreadyRelated = itemKeys.some((key) => relatedKeys.has(key));
2251
+ const isRelated = alreadyRelated || matchesPromotion(item.item_list_name, promotion);
2252
+ if (!isRelated) return item;
2253
+ if (!alreadyRelated) {
2254
+ newKeys.push(...itemKeys);
2255
+ }
2256
+ return {
2257
+ ...item,
2258
+ promotion_name: promotion.promotion_name,
2259
+ creative_name: promotion.creative_name,
2260
+ creative_slot: promotion.creative_slot
2261
+ };
2262
+ });
2263
+ if (newKeys.length) {
2264
+ writePromotion({
2265
+ ...promotion,
2266
+ related_keys: [...promotion.related_keys, ...newKeys].slice(0, MAX_RELATED_KEYS)
2267
+ });
2268
+ }
2269
+ return enrichedItems;
2270
+ }
2271
+
2142
2272
  // src/formatter.ts
2143
2273
  var adapters = {
2144
2274
  DECO: decoAdapter,
@@ -2164,7 +2294,11 @@ async function getParameters(context, eventName, config2) {
2164
2294
  return [param, await getter(context, config2)];
2165
2295
  })
2166
2296
  );
2167
- return Object.fromEntries(entries);
2297
+ const parameters = Object.fromEntries(entries);
2298
+ if (Array.isArray(parameters.items) && parameters.items.length) {
2299
+ parameters.items = itemsWithPromotion(parameters.items);
2300
+ }
2301
+ return parameters;
2168
2302
  }
2169
2303
 
2170
2304
  // src/index.ts
@@ -2173,18 +2307,30 @@ function initTracker(c) {
2173
2307
  config = c;
2174
2308
  }
2175
2309
  async function trackWebEvent(event, context) {
2310
+ var _a, _b, _c, _d;
2176
2311
  try {
2177
2312
  const parameters = await getParameters(context, event, config);
2178
- if (isDebugMode()) {
2179
- console.log(`[DT v2] ${event}, context e parameters: `, {
2180
- context,
2181
- parameters
2313
+ if (event === "SELECT_PROMOTION" && parameters) {
2314
+ savePromotion({
2315
+ promotion_name: (_a = parameters.promotion_name) != null ? _a : null,
2316
+ creative_name: (_b = parameters.creative_name) != null ? _b : null,
2317
+ creative_slot: (_c = parameters.creative_slot) != null ? _c : null,
2318
+ promotion_url: (_d = parameters.promotion_url) != null ? _d : null
2182
2319
  });
2183
2320
  }
2184
- return await pushToDataLayer(
2185
- event,
2186
- Object.assign({}, parameters, { window: context.window })
2187
- );
2321
+ if (isDebugMode()) {
2322
+ console.log(
2323
+ `%c [DT v2] %c ${event} `,
2324
+ "background: #6366f1; color: #fff; font-weight: bold; padding: 2px 6px; border-radius: 4px 0 0 4px;",
2325
+ "background: #1e1e2e; color: #a5b4fc; font-weight: bold; padding: 2px 6px; border-radius: 0 4px 4px 0;",
2326
+ { context, parameters }
2327
+ );
2328
+ }
2329
+ const result = await pushToDataLayer(event, Object.assign({}, parameters, { window: context.window }));
2330
+ if (event === "PURCHASE" || event === "CUSTOM_PURCHASE") {
2331
+ clearPromotion();
2332
+ }
2333
+ return result;
2188
2334
  } catch (err) {
2189
2335
  return console.error(`[DT] Error tracking event ${event}:`, err);
2190
2336
  }