@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.
@@ -1102,6 +1102,43 @@ var AzzasTracker = function() {
1102
1102
  return null;
1103
1103
  }
1104
1104
  };
1105
+ var setCookie = function setCookie(name, value) {
1106
+ var days = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 7, domain = arguments.length > 3 ? arguments[3] : void 0;
1107
+ if (typeof document === "undefined") return;
1108
+ var encodedValue = encodeURIComponent(value);
1109
+ var expires = "";
1110
+ if (days) {
1111
+ var date = /* @__PURE__ */ new Date();
1112
+ date.setTime(date.getTime() + days * 24 * 60 * 60 * 1e3);
1113
+ expires = "; expires=" + date.toUTCString();
1114
+ }
1115
+ var domainAttr = domain ? "; domain=.".concat(domain.replace(/^\./, "")) : "";
1116
+ document.cookie = "".concat(name, "=").concat(encodedValue).concat(expires).concat(domainAttr, "; path=/;");
1117
+ };
1118
+ var getCookie = function getCookie(name) {
1119
+ if (typeof document === "undefined") return null;
1120
+ var match = document.cookie.split("; ").find(function(row) {
1121
+ return row.startsWith("".concat(name, "="));
1122
+ });
1123
+ return match ? decodeURIComponent(match.slice(name.length + 1)) : null;
1124
+ };
1125
+ var cleanPath = function cleanPath(path) {
1126
+ try {
1127
+ return decodeURIComponent(path).toLowerCase().replace(/\/+$/, "");
1128
+ } catch (unused) {
1129
+ return path.toLowerCase().replace(/\/+$/, "");
1130
+ }
1131
+ };
1132
+ var matchesPromotion = function matchesPromotion(listName, promotion) {
1133
+ if (!listName || !promotion.promotion_url) return false;
1134
+ var promotionPath;
1135
+ try {
1136
+ promotionPath = new URL(promotion.promotion_url).pathname;
1137
+ } catch (unused) {
1138
+ promotionPath = promotion.promotion_url;
1139
+ }
1140
+ return cleanPath(promotionPath) === cleanPath(listName);
1141
+ };
1105
1142
  var paymentTypeFromOrderForm = // src/params/legacy/resolvers/paymentType/fromOrderForm.ts
1106
1143
  function paymentTypeFromOrderForm(context) {
1107
1144
  var _context_orderForm_paymentData, _context_orderForm_paymentData1, _system_name;
@@ -1350,9 +1387,95 @@ var AzzasTracker = function() {
1350
1387
  });
1351
1388
  return (selectedSla === null || selectedSla === void 0 ? void 0 : (_selectedSla_pickupStoreInfo = selectedSla.pickupStoreInfo) === null || _selectedSla_pickupStoreInfo === void 0 ? void 0 : _selectedSla_pickupStoreInfo.friendlyName) ? "retirada em loja: ".concat(selectedSla.pickupStoreInfo.friendlyName) : (selectedSla === null || selectedSla === void 0 ? void 0 : selectedSla.name) ? "Receba em casa: ".concat(selectedSla.name) : null;
1352
1389
  };
1390
+ var resolveRootDomain = function resolveRootDomain() {
1391
+ if (cachedRootDomain !== void 0) return cachedRootDomain;
1392
+ if (typeof document === "undefined") return null;
1393
+ var hostname = window.location.hostname;
1394
+ var isLocalhost = hostname === "localhost";
1395
+ var isIpAddress = /^[\d.]+$/.test(hostname);
1396
+ if (!hostname || isLocalhost || isIpAddress) {
1397
+ cachedRootDomain = null;
1398
+ return null;
1399
+ }
1400
+ var parts = hostname.split(".");
1401
+ var PROBE = "__azzas_trk_probe";
1402
+ for(var i = parts.length - 2; i >= 0; i--){
1403
+ var candidate = parts.slice(i).join(".");
1404
+ document.cookie = "".concat(PROBE, "=1; domain=.").concat(candidate, "; path=/;");
1405
+ if (document.cookie.includes("".concat(PROBE, "="))) {
1406
+ document.cookie = "".concat(PROBE, "=; domain=.").concat(candidate, "; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT");
1407
+ cachedRootDomain = candidate;
1408
+ return candidate;
1409
+ }
1410
+ }
1411
+ cachedRootDomain = null;
1412
+ return null;
1413
+ };
1414
+ var writePromotion = function writePromotion(promotion) {
1415
+ var remainingDays = (promotion.saved_at + PROMO_TTL_MS - Date.now()) / (24 * 60 * 60 * 1e3);
1416
+ if (remainingDays <= 0) return;
1417
+ setCookie(PROMO_COOKIE, JSON.stringify(promotion), remainingDays, resolveRootDomain());
1418
+ };
1419
+ var savePromotion = function savePromotion(input) {
1420
+ writePromotion(_object_spread_props(_object_spread({}, input), {
1421
+ related_keys: [],
1422
+ saved_at: Date.now()
1423
+ }));
1424
+ };
1425
+ var getPromotion = function getPromotion() {
1426
+ var raw = getCookie(PROMO_COOKIE);
1427
+ if (!raw) return null;
1428
+ try {
1429
+ var promotion = JSON.parse(raw);
1430
+ var isExpired = !promotion.saved_at || Date.now() - promotion.saved_at > PROMO_TTL_MS;
1431
+ if (isExpired) {
1432
+ clearPromotion();
1433
+ return null;
1434
+ }
1435
+ return promotion;
1436
+ } catch (unused) {
1437
+ return null;
1438
+ }
1439
+ };
1440
+ var clearPromotion = function clearPromotion() {
1441
+ setCookie(PROMO_COOKIE, "", -1, resolveRootDomain());
1442
+ };
1443
+ var itemsWithPromotion = function itemsWithPromotion(items) {
1444
+ var promotion = getPromotion();
1445
+ if (!promotion) return items;
1446
+ var relatedKeys = new Set(promotion.related_keys);
1447
+ var newKeys = [];
1448
+ var enrichedItems = items.map(function(item) {
1449
+ var itemKeys = [
1450
+ item.item_id
1451
+ ].filter(function(key) {
1452
+ return Boolean(key);
1453
+ });
1454
+ var alreadyRelated = itemKeys.some(function(key) {
1455
+ return relatedKeys.has(key);
1456
+ });
1457
+ var isRelated = alreadyRelated || matchesPromotion(item.item_list_name, promotion);
1458
+ if (!isRelated) return item;
1459
+ if (!alreadyRelated) {
1460
+ var _newKeys;
1461
+ (_newKeys = newKeys).push.apply(_newKeys, _to_consumable_array(itemKeys));
1462
+ }
1463
+ return _object_spread_props(_object_spread({}, item), {
1464
+ promotion_name: promotion.promotion_name,
1465
+ creative_name: promotion.creative_name,
1466
+ creative_slot: promotion.creative_slot
1467
+ });
1468
+ });
1469
+ if (newKeys.length) {
1470
+ writePromotion(_object_spread_props(_object_spread({}, promotion), {
1471
+ related_keys: _to_consumable_array(promotion.related_keys).concat(_to_consumable_array(newKeys)).slice(0, MAX_RELATED_KEYS)
1472
+ }));
1473
+ }
1474
+ return enrichedItems;
1475
+ };
1353
1476
  var getParameters = function getParameters(context, eventName, config2) {
1354
1477
  return _async_to_generator(function() {
1355
- var _adapters_currentAdapter, eventConfig, currentAdapter, getters, entries;
1478
+ var _adapters_currentAdapter, eventConfig, currentAdapter, getters, entries, parameters;
1356
1479
  return _ts_generator(this, function(_state) {
1357
1480
  switch(_state.label){
1358
1481
  case 0:
@@ -1402,9 +1525,13 @@ var AzzasTracker = function() {
1402
1525
  ];
1403
1526
  case 1:
1404
1527
  entries = _state.sent();
1528
+ parameters = Object.fromEntries(entries);
1529
+ if (Array.isArray(parameters.items) && parameters.items.length) {
1530
+ parameters.items = itemsWithPromotion(parameters.items);
1531
+ }
1405
1532
  return [
1406
1533
  2,
1407
- Object.fromEntries(entries)
1534
+ parameters
1408
1535
  ];
1409
1536
  }
1410
1537
  });
@@ -1415,7 +1542,7 @@ var AzzasTracker = function() {
1415
1542
  };
1416
1543
  var trackWebEvent = function trackWebEvent(event, context) {
1417
1544
  return _async_to_generator(function() {
1418
- var parameters, err;
1545
+ var parameters, _parameters_promotion_name, _parameters_creative_name, _parameters_creative_slot, _parameters_promotion_url, result, err;
1419
1546
  return _ts_generator(this, function(_state) {
1420
1547
  switch(_state.label){
1421
1548
  case 0:
@@ -1431,8 +1558,17 @@ var AzzasTracker = function() {
1431
1558
  ];
1432
1559
  case 1:
1433
1560
  parameters = _state.sent();
1561
+ if (event === "SELECT_PROMOTION" && parameters) {
1562
+ ;
1563
+ savePromotion({
1564
+ promotion_name: (_parameters_promotion_name = parameters.promotion_name) !== null && _parameters_promotion_name !== void 0 ? _parameters_promotion_name : null,
1565
+ creative_name: (_parameters_creative_name = parameters.creative_name) !== null && _parameters_creative_name !== void 0 ? _parameters_creative_name : null,
1566
+ creative_slot: (_parameters_creative_slot = parameters.creative_slot) !== null && _parameters_creative_slot !== void 0 ? _parameters_creative_slot : null,
1567
+ promotion_url: (_parameters_promotion_url = parameters.promotion_url) !== null && _parameters_promotion_url !== void 0 ? _parameters_promotion_url : null
1568
+ });
1569
+ }
1434
1570
  if (isDebugMode()) {
1435
- console.log("[DT v2] ".concat(event, ", context e parameters: "), {
1571
+ console.log("%c [DT v2] %c ".concat(event, " "), "background: #6366f1; color: #fff; font-weight: bold; padding: 2px 6px; border-radius: 4px 0 0 4px;", "background: #1e1e2e; color: #a5b4fc; font-weight: bold; padding: 2px 6px; border-radius: 0 4px 4px 0;", {
1436
1572
  context: context,
1437
1573
  parameters: parameters
1438
1574
  });
@@ -1444,9 +1580,13 @@ var AzzasTracker = function() {
1444
1580
  }))
1445
1581
  ];
1446
1582
  case 2:
1583
+ result = _state.sent();
1584
+ if (event === "PURCHASE" || event === "CUSTOM_PURCHASE") {
1585
+ clearPromotion();
1586
+ }
1447
1587
  return [
1448
1588
  2,
1449
- _state.sent()
1589
+ result
1450
1590
  ];
1451
1591
  case 3:
1452
1592
  err = _state.sent();
@@ -1685,6 +1825,7 @@ var AzzasTracker = function() {
1685
1825
  "region",
1686
1826
  "currency",
1687
1827
  "promotion_name",
1828
+ "promotion_url",
1688
1829
  "creative_slot",
1689
1830
  "creative_name"
1690
1831
  ]
@@ -2479,6 +2620,9 @@ var AzzasTracker = function() {
2479
2620
  promotion_name: function(context) {
2480
2621
  return context.meta.promotion_name || null;
2481
2622
  },
2623
+ promotion_url: function(context) {
2624
+ return context.meta.promotion_url || null;
2625
+ },
2482
2626
  creative_name: function(context) {
2483
2627
  return context.meta.creative_name || null;
2484
2628
  },
@@ -2651,7 +2795,7 @@ var AzzasTracker = function() {
2651
2795
  var getItemCategory23 = function(item) {
2652
2796
  var _ref, _validHierarchy_;
2653
2797
  var _item_similarItems_, _item_similarItems;
2654
- var AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção)$/i;
2798
+ var AVOID_CATEGORIES = /^(outlet|bazar|sale|coleção|promoção)$/i;
2655
2799
  var categories = (_ref = item === null || item === void 0 ? void 0 : (_item_similarItems = item.similarItems) === null || _item_similarItems === void 0 ? void 0 : (_item_similarItems_ = _item_similarItems[0]) === null || _item_similarItems_ === void 0 ? void 0 : _item_similarItems_.categories) !== null && _ref !== void 0 ? _ref : [];
2656
2800
  var validHierarchy = categories.filter(function(c) {
2657
2801
  return c && !AVOID_CATEGORIES.test(c);
@@ -2667,7 +2811,7 @@ var AzzasTracker = function() {
2667
2811
  if (context === null || context === void 0 ? void 0 : context.products) {
2668
2812
  return context.products.map(function(item) {
2669
2813
  var _ref, _ref1, _item_isVariantOf_name, _ref2, _ref3, _item_groupRefId, _getItemCategory5, _getItemCategory23, _ref4, _ref5, _ref6, _resizeVtexImage2, _ref7, _ref8;
2670
- var _item_offers_offers, _item_offers, _item_brand_name, _item_brand, _item_offers1, _item_offers2, _item_name_split_, _item_name, _item_name1, _item_isVariantOf, _item_seo, _item_image, _item_offers_offers__seller, _item_offers_offers_, _item_offers_offers1, _item_offers3;
2814
+ var _item_offers_offers, _item_offers, _item_brand_name, _item_brand, _item_offers1, _item_offers2, _item_name_split_, _item_name, _item_name_split_1, _item_name1, _item_isVariantOf, _item_seo, _item_image, _item_offers_offers__seller, _item_offers_offers_, _item_offers_offers1, _item_offers3;
2671
2815
  var listPrice = item === null || item === void 0 ? void 0 : (_item_offers = item.offers) === null || _item_offers === void 0 ? void 0 : (_item_offers_offers = _item_offers.offers) === null || _item_offers_offers === void 0 ? void 0 : _item_offers_offers[0].listPrice;
2672
2816
  return {
2673
2817
  quantity: 1,
@@ -2678,7 +2822,7 @@ var AzzasTracker = function() {
2678
2822
  discount: Math.round((Number(listPrice) - Number(item === null || item === void 0 ? void 0 : (_item_offers2 = item.offers) === null || _item_offers2 === void 0 ? void 0 : _item_offers2.lowPrice)) * 100) / 100,
2679
2823
  item_variant: (item === null || item === void 0 ? void 0 : (_item_name = item.name) === null || _item_name === void 0 ? void 0 : (_item_name_split_ = _item_name.split(" - ")[0]) === null || _item_name_split_ === void 0 ? void 0 : _item_name_split_.replace(/^[^_]+_/, "").replace(/_/g, " ")) || null,
2680
2824
  // cor
2681
- item_variant2: (_ref3 = item === null || item === void 0 ? void 0 : (_item_name1 = item.name) === null || _item_name1 === void 0 ? void 0 : _item_name1.split(" - ")[1].toUpperCase()) !== null && _ref3 !== void 0 ? _ref3 : null,
2825
+ item_variant2: (_ref3 = item === null || item === void 0 ? void 0 : (_item_name1 = item.name) === null || _item_name1 === void 0 ? void 0 : (_item_name_split_1 = _item_name1.split(" - ")[1]) === null || _item_name_split_1 === void 0 ? void 0 : _item_name_split_1.toUpperCase()) !== null && _ref3 !== void 0 ? _ref3 : null,
2682
2826
  // tamanho
2683
2827
  item_shipping_tier: "",
2684
2828
  item_ref: (_item_groupRefId = item.groupRefId) !== null && _item_groupRefId !== void 0 ? _item_groupRefId : null,
@@ -3295,6 +3439,11 @@ var AzzasTracker = function() {
3295
3439
  return context.appliedSellerCodName || (context === null || context === void 0 ? void 0 : context.orderForm.marketingData.utmiCampaign) || null;
3296
3440
  }
3297
3441
  });
3442
+ // src/core/promotion.ts
3443
+ var PROMO_COOKIE = "__azzas_trk_promo";
3444
+ var PROMO_TTL_MS = 10 * 60 * 1e3;
3445
+ var MAX_RELATED_KEYS = 30;
3446
+ var cachedRootDomain;
3298
3447
  // src/formatter.ts
3299
3448
  var adapters = {
3300
3449
  DECO: decoAdapter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azzas/azzas-tracker-web",
3
- "version": "2.0.2-preview.3",
3
+ "version": "2.0.2-preview.5",
4
4
  "type": "module",
5
5
  "main": "./dist/mod.cjs",
6
6
  "module": "./dist/mod.js",