@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.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) {
@@ -1067,6 +1102,10 @@ var PROD_DOMAINS = {
1067
1102
  "lojafabula": {
1068
1103
  "store": "https://lojafabula.myvtex.com",
1069
1104
  "checkout": "https://secure.lojafabula.com.br"
1105
+ },
1106
+ "lojausereserva": {
1107
+ "store": "https://lojausereserva.myvtex.com",
1108
+ "checkout": "https://lojausereserva.myvtex.com"
1070
1109
  }
1071
1110
  };
1072
1111
  async function getUserInfo(context) {
@@ -1368,6 +1407,9 @@ var metaAdapter = {
1368
1407
  promotion_name: (context) => {
1369
1408
  return context.meta.promotion_name || null;
1370
1409
  },
1410
+ promotion_url: (context) => {
1411
+ return context.meta.promotion_url || null;
1412
+ },
1371
1413
  creative_name: (context) => {
1372
1414
  return context.meta.creative_name || null;
1373
1415
  },
@@ -1555,7 +1597,7 @@ var getItemCategory5 = (item) => {
1555
1597
  };
1556
1598
  var getItemCategory23 = (item) => {
1557
1599
  var _a, _b, _c, _d;
1558
- const AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção)$/i;
1600
+ const AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção|promoção)$/i;
1559
1601
  const categories = (_c = (_b = (_a = item == null ? void 0 : item.similarItems) == null ? void 0 : _a[0]) == null ? void 0 : _b.categories) != null ? _c : [];
1560
1602
  const validHierarchy = categories.filter((c) => c && !AVOID_CATEGORIES.test(c));
1561
1603
  const deepest = (_d = validHierarchy[validHierarchy.length - 1]) != null ? _d : "";
@@ -1906,7 +1948,7 @@ var getItemCategory7 = (item) => {
1906
1948
  };
1907
1949
  var getItemCategory25 = (item) => {
1908
1950
  var _a, _b;
1909
- const AVOID_CATEGORIES = /outlet|bazar|sale/i;
1951
+ const AVOID_CATEGORIES = /outlet|bazar|sale|coleção|promoção/i;
1910
1952
  const categories = Object.values((_a = item == null ? void 0 : item.productCategories) != null ? _a : {});
1911
1953
  const validHierarchy = categories.map((c) => c.trim()).filter((c) => c && !AVOID_CATEGORIES.test(c));
1912
1954
  const deepest = (_b = validHierarchy[validHierarchy.length - 1]) != null ? _b : "";
@@ -1945,28 +1987,28 @@ var orderFormAdapter = {
1945
1987
  if (context == null ? void 0 : context.items) return context.items;
1946
1988
  if ((context == null ? void 0 : context.orderForm) && (context == null ? void 0 : context.orderForm.items)) {
1947
1989
  return getOrderFormItems(context.orderForm).map((item, index) => {
1948
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
1990
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
1949
1991
  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();
1950
1992
  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();
1951
1993
  return {
1952
1994
  quantity: (_f = item == null ? void 0 : item.quantity) != null ? _f : 1,
1953
1995
  index: (_h = (_g = context == null ? void 0 : context.orderForm) == null ? void 0 : _g.index) != null ? _h : index + 1,
1954
- item_brand: (_j = (_i = item.additionalInfo) == null ? void 0 : _i.brandName) != null ? _j : null,
1955
- item_name: (_k = item == null ? void 0 : item.name.replace(item == null ? void 0 : item.skuName, "").trim()) != null ? _k : null,
1996
+ item_brand: (_k = (_j = (_i = item.additionalInfo) == null ? void 0 : _i.brandName) == null ? void 0 : _j.toLowerCase()) != null ? _k : null,
1997
+ item_name: (_l = item == null ? void 0 : item.name.replace(item == null ? void 0 : item.skuName, "").trim()) != null ? _l : null,
1956
1998
  price: (item == null ? void 0 : item.sellingPrice) / 100,
1957
1999
  discount: ((item == null ? void 0 : item.listPrice) - (item == null ? void 0 : item.sellingPrice)) / 100,
1958
2000
  item_variant: color != null ? color : null,
1959
2001
  item_variant2: size != null ? size : null,
1960
2002
  item_shipping_tier: getItemShippingTier2(context.orderForm, item),
1961
2003
  item_ref: (item == null ? void 0 : item.productRefId) || (item == null ? void 0 : item.refId.replace(/_\d+$/, "")) || null,
1962
- item_category: (_l = getItemCategory7(item)) != null ? _l : null,
1963
- item_category2: (_m = getItemCategory25(item)) != null ? _m : null,
1964
- item_id: (_n = item == null ? void 0 : item.productId) != null ? _n : null,
1965
- item_sku: (_o = item == null ? void 0 : item.id) != null ? _o : null,
1966
- item_url: (_p = item.detailUrl) != null ? _p : null,
1967
- image_url: (_q = resizeVtexImage2(item.imageUrl)) != null ? _q : null,
1968
- seller_id: (_r = item == null ? void 0 : item.seller) != null ? _r : null,
1969
- item_list_name: (_t = (_s = context == null ? void 0 : context.orderForm) == null ? void 0 : _s.item_list_name) != null ? _t : null
2004
+ item_category: (_m = getItemCategory7(item)) != null ? _m : null,
2005
+ item_category2: (_n = getItemCategory25(item)) != null ? _n : null,
2006
+ item_id: (_o = item == null ? void 0 : item.productId) != null ? _o : null,
2007
+ item_sku: (_p = item == null ? void 0 : item.id) != null ? _p : null,
2008
+ item_url: (_q = item.detailUrl) != null ? _q : null,
2009
+ image_url: (_r = resizeVtexImage2(item.imageUrl)) != null ? _r : null,
2010
+ seller_id: (_s = item == null ? void 0 : item.seller) != null ? _s : null,
2011
+ item_list_name: (_u = (_t = context == null ? void 0 : context.orderForm) == null ? void 0 : _t.item_list_name) != null ? _u : null
1970
2012
  };
1971
2013
  });
1972
2014
  }
@@ -2110,6 +2152,94 @@ var orderFormAdapter = {
2110
2152
  }
2111
2153
  };
2112
2154
 
2155
+ // src/core/promotion.ts
2156
+ var PROMO_COOKIE = "__azzas_trk_promo";
2157
+ var PROMO_TTL_MS = 10 * 60 * 1e3;
2158
+ var MAX_RELATED_KEYS = 30;
2159
+ var cachedRootDomain;
2160
+ function resolveRootDomain() {
2161
+ if (cachedRootDomain !== void 0) return cachedRootDomain;
2162
+ if (typeof document === "undefined") return null;
2163
+ const hostname = window.location.hostname;
2164
+ const isLocalhost = hostname === "localhost";
2165
+ const isIpAddress = /^[\d.]+$/.test(hostname);
2166
+ if (!hostname || isLocalhost || isIpAddress) {
2167
+ cachedRootDomain = null;
2168
+ return null;
2169
+ }
2170
+ const parts = hostname.split(".");
2171
+ const PROBE = "__azzas_trk_probe";
2172
+ for (let i = parts.length - 2; i >= 0; i--) {
2173
+ const candidate = parts.slice(i).join(".");
2174
+ document.cookie = `${PROBE}=1; domain=.${candidate}; path=/;`;
2175
+ if (document.cookie.includes(`${PROBE}=`)) {
2176
+ document.cookie = `${PROBE}=; domain=.${candidate}; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
2177
+ cachedRootDomain = candidate;
2178
+ return candidate;
2179
+ }
2180
+ }
2181
+ cachedRootDomain = null;
2182
+ return null;
2183
+ }
2184
+ function writePromotion(promotion) {
2185
+ const remainingDays = (promotion.saved_at + PROMO_TTL_MS - Date.now()) / (24 * 60 * 60 * 1e3);
2186
+ if (remainingDays <= 0) return;
2187
+ setCookie(PROMO_COOKIE, JSON.stringify(promotion), remainingDays, resolveRootDomain());
2188
+ }
2189
+ function savePromotion(input) {
2190
+ writePromotion({
2191
+ ...input,
2192
+ related_keys: [],
2193
+ saved_at: Date.now()
2194
+ });
2195
+ }
2196
+ function getPromotion() {
2197
+ const raw = getCookie(PROMO_COOKIE);
2198
+ if (!raw) return null;
2199
+ try {
2200
+ const promotion = JSON.parse(raw);
2201
+ const isExpired = !promotion.saved_at || Date.now() - promotion.saved_at > PROMO_TTL_MS;
2202
+ if (isExpired) {
2203
+ clearPromotion();
2204
+ return null;
2205
+ }
2206
+ return promotion;
2207
+ } catch (e) {
2208
+ return null;
2209
+ }
2210
+ }
2211
+ function clearPromotion() {
2212
+ setCookie(PROMO_COOKIE, "", -1, resolveRootDomain());
2213
+ }
2214
+ function itemsWithPromotion(items) {
2215
+ const promotion = getPromotion();
2216
+ if (!promotion) return items;
2217
+ const relatedKeys = new Set(promotion.related_keys);
2218
+ const newKeys = [];
2219
+ const enrichedItems = items.map((item) => {
2220
+ const itemKeys = [item.item_id].filter((key) => Boolean(key));
2221
+ const alreadyRelated = itemKeys.some((key) => relatedKeys.has(key));
2222
+ const isRelated = alreadyRelated || matchesPromotion(item.item_list_name, promotion);
2223
+ if (!isRelated) return item;
2224
+ if (!alreadyRelated) {
2225
+ newKeys.push(...itemKeys);
2226
+ }
2227
+ return {
2228
+ ...item,
2229
+ promotion_name: promotion.promotion_name,
2230
+ creative_name: promotion.creative_name,
2231
+ creative_slot: promotion.creative_slot
2232
+ };
2233
+ });
2234
+ if (newKeys.length) {
2235
+ writePromotion({
2236
+ ...promotion,
2237
+ related_keys: [...promotion.related_keys, ...newKeys].slice(0, MAX_RELATED_KEYS)
2238
+ });
2239
+ }
2240
+ return enrichedItems;
2241
+ }
2242
+
2113
2243
  // src/formatter.ts
2114
2244
  var adapters = {
2115
2245
  DECO: decoAdapter,
@@ -2135,7 +2265,11 @@ async function getParameters(context, eventName, config2) {
2135
2265
  return [param, await getter(context, config2)];
2136
2266
  })
2137
2267
  );
2138
- return Object.fromEntries(entries);
2268
+ const parameters = Object.fromEntries(entries);
2269
+ if (Array.isArray(parameters.items) && parameters.items.length) {
2270
+ parameters.items = itemsWithPromotion(parameters.items);
2271
+ }
2272
+ return parameters;
2139
2273
  }
2140
2274
 
2141
2275
  // src/index.ts
@@ -2144,18 +2278,30 @@ function initTracker(c) {
2144
2278
  config = c;
2145
2279
  }
2146
2280
  async function trackWebEvent(event, context) {
2281
+ var _a, _b, _c, _d;
2147
2282
  try {
2148
2283
  const parameters = await getParameters(context, event, config);
2149
- if (isDebugMode()) {
2150
- console.log(`[DT v2] ${event}, context e parameters: `, {
2151
- context,
2152
- parameters
2284
+ if (event === "SELECT_PROMOTION" && parameters) {
2285
+ savePromotion({
2286
+ promotion_name: (_a = parameters.promotion_name) != null ? _a : null,
2287
+ creative_name: (_b = parameters.creative_name) != null ? _b : null,
2288
+ creative_slot: (_c = parameters.creative_slot) != null ? _c : null,
2289
+ promotion_url: (_d = parameters.promotion_url) != null ? _d : null
2153
2290
  });
2154
2291
  }
2155
- return await pushToDataLayer(
2156
- event,
2157
- Object.assign({}, parameters, { window: context.window })
2158
- );
2292
+ if (isDebugMode()) {
2293
+ console.log(
2294
+ `%c [DT v2] %c ${event} `,
2295
+ "background: #6366f1; color: #fff; font-weight: bold; padding: 2px 6px; border-radius: 4px 0 0 4px;",
2296
+ "background: #1e1e2e; color: #a5b4fc; font-weight: bold; padding: 2px 6px; border-radius: 0 4px 4px 0;",
2297
+ { context, parameters }
2298
+ );
2299
+ }
2300
+ const result = await pushToDataLayer(event, Object.assign({}, parameters, { window: context.window }));
2301
+ if (event === "PURCHASE" || event === "CUSTOM_PURCHASE") {
2302
+ clearPromotion();
2303
+ }
2304
+ return result;
2159
2305
  } catch (err) {
2160
2306
  return console.error(`[DT] Error tracking event ${event}:`, err);
2161
2307
  }