@azzas/azzas-tracker-web 1.0.79 → 1.0.81

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.
@@ -340,6 +340,19 @@ var AzzasTracker = (() => {
340
340
  ["deb", "cart\xE3o"]
341
341
  ];
342
342
  var toNum = (val) => typeof val === "string" ? Number(val.replace(/[^\d,]/g, "").replace(",", ".")) : val;
343
+ var setLocalStorage = (key, value) => {
344
+ localStorage.setItem(key, value);
345
+ };
346
+ var getLocalStorage = (key) => {
347
+ return localStorage.getItem(key);
348
+ };
349
+ var documentToHash = async (document2) => {
350
+ const encoder = new TextEncoder();
351
+ const data = encoder.encode(document2);
352
+ const hashBuffer = await crypto.subtle.digest("SHA-256", data);
353
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
354
+ return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
355
+ };
343
356
 
344
357
  // src/adapters/dito.ts
345
358
  function pushToDito(event, ecomm) {
@@ -617,10 +630,158 @@ var AzzasTracker = (() => {
617
630
  );
618
631
  }
619
632
 
633
+ // src/params/utils/itemListAttribution.ts
634
+ var LAST_SELECTED_ITEM_KEY = "last_selected_item";
635
+ var STORAGE_VIEWED = "last_viewed_item";
636
+ var CART_LIST_HISTORY_KEY = "cart_list_history";
637
+ var DAY_IN_MS = 24 * 60 * 60 * 1e3;
638
+ function getStorage() {
639
+ try {
640
+ if (typeof window === "undefined" || !window.localStorage) return null;
641
+ return window.localStorage;
642
+ } catch (e) {
643
+ return null;
644
+ }
645
+ }
646
+ function parseJSON(value) {
647
+ if (!value) return null;
648
+ try {
649
+ return JSON.parse(value);
650
+ } catch (e) {
651
+ return null;
652
+ }
653
+ }
654
+ function isFreshTimestamp(timestamp) {
655
+ const elapsed = Date.now() - timestamp;
656
+ return elapsed >= 0 && elapsed < DAY_IN_MS;
657
+ }
658
+ function isStoredItemMemory(value) {
659
+ if (!value || typeof value !== "object") return false;
660
+ const candidate = value;
661
+ return typeof candidate.id === "string" && typeof candidate.list === "string" && typeof candidate.timestamp === "number";
662
+ }
663
+ function resolveIdFromUnknownItem(item) {
664
+ var _a, _b, _c, _d, _e;
665
+ const id = (_e = (_d = (_c = (_b = (_a = item == null ? void 0 : item.productID) != null ? _a : item == null ? void 0 : item.productId) != null ? _b : item == null ? void 0 : item.item_group_id) != null ? _c : item == null ? void 0 : item.item_id) != null ? _d : item == null ? void 0 : item.id) != null ? _e : null;
666
+ return id == null ? null : String(id);
667
+ }
668
+ function resolveItemId(item) {
669
+ return resolveIdFromUnknownItem(item);
670
+ }
671
+ function readStoredItem(key) {
672
+ const storage = getStorage();
673
+ if (!storage) return null;
674
+ try {
675
+ const parsed = parseJSON(storage.getItem(key));
676
+ return isStoredItemMemory(parsed) ? parsed : null;
677
+ } catch (e) {
678
+ return null;
679
+ }
680
+ }
681
+ function writeStoredItem(key, value) {
682
+ const storage = getStorage();
683
+ if (!storage) return;
684
+ try {
685
+ storage.setItem(key, JSON.stringify(value));
686
+ } catch (e) {
687
+ }
688
+ }
689
+ function readCartHistory() {
690
+ const storage = getStorage();
691
+ if (!storage) return [];
692
+ try {
693
+ const parsed = parseJSON(storage.getItem(CART_LIST_HISTORY_KEY));
694
+ if (!Array.isArray(parsed)) return [];
695
+ return parsed.filter((entry) => !!entry && typeof entry === "object" && typeof entry.id === "string" && typeof entry.list === "string");
696
+ } catch (e) {
697
+ return [];
698
+ }
699
+ }
700
+ function writeCartHistory(history) {
701
+ const storage = getStorage();
702
+ if (!storage) return;
703
+ try {
704
+ storage.setItem(CART_LIST_HISTORY_KEY, JSON.stringify(history));
705
+ } catch (e) {
706
+ }
707
+ }
708
+ function saveSelectedItem(productId, listName) {
709
+ if (productId == null || !listName) return;
710
+ const payload = {
711
+ id: String(productId),
712
+ list: String(listName),
713
+ timestamp: Date.now()
714
+ };
715
+ writeStoredItem(LAST_SELECTED_ITEM_KEY, payload);
716
+ }
717
+ function saveViewedItem(productId, listName) {
718
+ if (productId == null) return;
719
+ const payload = {
720
+ id: String(productId),
721
+ list: String(listName != null ? listName : "PDP"),
722
+ timestamp: Date.now()
723
+ };
724
+ writeStoredItem(STORAGE_VIEWED, payload);
725
+ }
726
+ function resolveItemListName(params) {
727
+ const { event, item, itemListName } = params;
728
+ const currentListName = itemListName != null ? itemListName : null;
729
+ const isResolvableEvent = event === "VIEW_ITEM" || event === "ADD_TO_CART";
730
+ if (!isResolvableEvent) return currentListName;
731
+ const currentItemId = resolveIdFromUnknownItem(item);
732
+ if (event === "VIEW_ITEM") {
733
+ if (!currentItemId) return "PDP";
734
+ const selectedItem = readStoredItem(LAST_SELECTED_ITEM_KEY);
735
+ if (!selectedItem) return "PDP";
736
+ if (selectedItem.id === currentItemId && isFreshTimestamp(selectedItem.timestamp)) {
737
+ return selectedItem.list;
738
+ }
739
+ return "PDP";
740
+ }
741
+ if (currentListName !== "PDP" && currentListName !== null) return currentListName;
742
+ if (!currentItemId) return currentListName;
743
+ const viewedItem = readStoredItem(STORAGE_VIEWED);
744
+ if (viewedItem && viewedItem.id === currentItemId && isFreshTimestamp(viewedItem.timestamp)) {
745
+ return viewedItem.list;
746
+ }
747
+ return currentListName === "PDP" ? "PDP" : null;
748
+ }
749
+ function appendCartHistory(productId, listName) {
750
+ if (productId == null) return;
751
+ const history = readCartHistory();
752
+ history.push({
753
+ id: String(productId),
754
+ list: listName ? String(listName) : "PDP"
755
+ });
756
+ writeCartHistory(history);
757
+ }
758
+ function pickSelectedItemFromResolvedItems(items, source) {
759
+ var _a;
760
+ if (!Array.isArray(items) || items.length === 0) return null;
761
+ if (typeof (source == null ? void 0 : source.index) === "number") {
762
+ const indexed = (_a = items.find((item) => (item == null ? void 0 : item.index) === source.index)) != null ? _a : items[source.index];
763
+ if (indexed && typeof indexed === "object") return indexed;
764
+ }
765
+ return items[0];
766
+ }
767
+ function persistSelectItemOriginFromResolvedItems(items, source) {
768
+ var _a, _b, _c;
769
+ const selectedItem = pickSelectedItemFromResolvedItems(items, source);
770
+ if (!selectedItem) return;
771
+ const productId = resolveIdFromUnknownItem(selectedItem);
772
+ const listName = (_c = (_b = (_a = selectedItem == null ? void 0 : selectedItem.item_list_name) != null ? _a : source == null ? void 0 : source.itemListName) != null ? _b : source == null ? void 0 : source.item_list_name) != null ? _c : null;
773
+ saveSelectedItem(productId, listName);
774
+ }
775
+
620
776
  // src/params/resolvers/items/fromItem.ts
621
777
  async function itemsFromItem(context, event) {
622
778
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
623
779
  const { item, orderForm, itemListName } = context;
780
+ const resolvedListName = resolveItemListName({ event, item, itemListName });
781
+ const resolvedItemId = resolveItemId(item);
782
+ if (event === "ADD_TO_CART") {
783
+ appendCartHistory(resolvedItemId, resolvedListName);
784
+ }
624
785
  return [
625
786
  {
626
787
  index: typeof (item == null ? void 0 : item.index) === "number" ? item.index : null,
@@ -638,7 +799,7 @@ var AzzasTracker = (() => {
638
799
  item_variant: ((item == null ? void 0 : item.skuName) || (item == null ? void 0 : item.item_variant) || (item == null ? void 0 : item.name) || (item == null ? void 0 : item.itemOffered.name)).split(/[-/]/)[0].trim() || null,
639
800
  item_variant2: (item == null ? void 0 : item.size) || ((_n = item == null ? void 0 : item.itemAttributes) == null ? void 0 : _n.size) || ((_p = (_o = (item == null ? void 0 : item.skuName) || (item == null ? void 0 : item.name) || (item == null ? void 0 : item.itemOffered.name)) == null ? void 0 : _o.split(/[-/]/).pop()) == null ? void 0 : _p.trim()) || null,
640
801
  discount: getDiscount(item),
641
- item_list_name: itemListName || null,
802
+ item_list_name: resolvedListName,
642
803
  item_url: (item == null ? void 0 : item.item_url) || null,
643
804
  // add_to_cart dito
644
805
  image_url: resizeVtexImage(item == null ? void 0 : item.image) || resizeVtexImage(item == null ? void 0 : item.imageUrl) || null
@@ -668,9 +829,14 @@ var AzzasTracker = (() => {
668
829
  }
669
830
 
670
831
  // src/params/resolvers/items/fromPdp.ts
671
- async function itemsFromPDP(context) {
832
+ async function itemsFromPDP(context, event) {
672
833
  var _a, _b, _c, _d;
673
834
  const { item, value, brand } = context;
835
+ const resolvedListName = resolveItemListName({ event, item, itemListName: null });
836
+ const resolvedItemId = resolveItemId(item);
837
+ if (event === "VIEW_ITEM") {
838
+ saveViewedItem(resolvedItemId, resolvedListName);
839
+ }
674
840
  return [
675
841
  {
676
842
  price: value || 0,
@@ -687,7 +853,8 @@ var AzzasTracker = (() => {
687
853
  item_sku: (item == null ? void 0 : item.sku) || (item == null ? void 0 : item.productId) || null,
688
854
  item_url: (item == null ? void 0 : item.url) || (item == null ? void 0 : item.item_url) || (item == null ? void 0 : item.link) || null,
689
855
  image_url: resizeVtexImage(item == null ? void 0 : item.image) || resizeVtexImage(item == null ? void 0 : item.imageUrl) || null,
690
- seller_id: (item == null ? void 0 : item.seller) || null
856
+ seller_id: (item == null ? void 0 : item.seller) || null,
857
+ item_list_name: resolvedListName
691
858
  }
692
859
  ];
693
860
  }
@@ -730,11 +897,15 @@ var AzzasTracker = (() => {
730
897
  case "REMOVE_FROM_CART":
731
898
  return itemsFromItem(context, eventName);
732
899
  case "SELECT_ITEM":
900
+ return itemsFromList(context).then((items) => {
901
+ persistSelectItemOriginFromResolvedItems(items, context);
902
+ return items;
903
+ });
733
904
  case "VIEW_ITEM_LIST":
734
905
  return itemsFromList(context);
735
906
  case "VIEW_ITEM":
736
907
  case "CUSTOM_VIEW_ITEM":
737
- return itemsFromPDP(context);
908
+ return itemsFromPDP(context, eventName);
738
909
  case "VIEW_PROMOTION":
739
910
  case "SELECT_PROMOTION":
740
911
  return itemsFromBanner(context);
@@ -826,7 +997,16 @@ var AzzasTracker = (() => {
826
997
  if (response.status !== 200) {
827
998
  return { customer: { email: context.userEmail, id: context == null ? void 0 : context.userId }, address: null };
828
999
  }
829
- return await response.json();
1000
+ const res = await response.json();
1001
+ const cpfFromStorage = getLocalStorage("user_doc_dt");
1002
+ if (cpfFromStorage) {
1003
+ return { ...res, customer: { ...res.customer, cpf: cpfFromStorage } };
1004
+ } else {
1005
+ const hashedDocument = await documentToHash(res.customer.document);
1006
+ console.log("[DT] set localstorage");
1007
+ setLocalStorage("user_doc_dt", hashedDocument);
1008
+ return { ...res, customer: { ...res.customer, cpf: hashedDocument } };
1009
+ }
830
1010
  } catch (_) {
831
1011
  return { customer: { email: context.userEmail, id: context == null ? void 0 : context.userId }, address: null };
832
1012
  }