@azzas/azzas-tracker-web 1.0.98 → 1.0.99-preview.0

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
@@ -294,7 +294,7 @@ var EVENTS = {
294
294
  }
295
295
  };
296
296
 
297
- // src/adapters/datalayer.ts
297
+ // src/pushToDatalayer.ts
298
298
  function pushToDataLayer(event, context) {
299
299
  const targetWindow = context.window || (typeof window !== "undefined" ? window : null);
300
300
  if (!targetWindow) return;
@@ -305,74 +305,13 @@ function pushToDataLayer(event, context) {
305
305
  targetWindow.dataLayer = targetWindow.dataLayer || [];
306
306
  if (eventConfig == null ? void 0 : eventConfig.hasEcommerce) {
307
307
  targetWindow.dataLayer.push({ ecommerce: null });
308
- targetWindow.dataLayer.push({ event, ecommerce: payload });
308
+ targetWindow.dataLayer.push({ event: event.toLowerCase(), ecommerce: payload });
309
309
  } else {
310
- targetWindow.dataLayer.push(Object.assign({ event }, payload));
310
+ targetWindow.dataLayer.push(Object.assign({ event: event.toLowerCase() }, payload));
311
311
  }
312
312
  }
313
313
 
314
- // src/core/utils.ts
315
- function dataTrackingLog(data, color) {
316
- console.log(
317
- `%c[DT] EVENTO DISPARADO:%c `,
318
- `background: ${color}; color: white; font-weight: bold; padding: 2px 4px; border-radius: 3px;`,
319
- "color: #fff;",
320
- data || ""
321
- );
322
- }
323
- var paymentTypeMap = [
324
- ["vale", "vale cr\xE9dito"],
325
- ["pagaleve", "pix"],
326
- ["pix parcel", "pix parcelado"],
327
- ["pix", "pix"],
328
- ["apple", "apple pay"],
329
- ["google", "google pay"],
330
- ["boleto", "boleto"],
331
- ["paypal", "paypal"],
332
- ["cred", "cart\xE3o"],
333
- ["visa", "cart\xE3o"],
334
- ["master", "cart\xE3o"],
335
- ["amex", "cart\xE3o"],
336
- ["elo", "cart\xE3o"],
337
- ["hiper", "cart\xE3o"],
338
- ["diner", "cart\xE3o"],
339
- ["deb", "cart\xE3o"]
340
- ];
341
- var toNum = (val) => typeof val === "string" ? Number(val.replace(/[^\d,]/g, "").replace(",", ".")) : val;
342
- var documentToHash = async (document2) => {
343
- if (!document2) return null;
344
- const encoder = new TextEncoder();
345
- const data = encoder.encode(document2);
346
- const hashBuffer = await crypto.subtle.digest("SHA-256", data);
347
- const hashArray = Array.from(new Uint8Array(hashBuffer));
348
- return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
349
- };
350
-
351
- // src/adapters/dito.ts
352
- function pushToDito(event, ecomm) {
353
- dataTrackingLog({ id: event, custom_data: ecomm }, "#00c851");
354
- }
355
-
356
- // src/adapters/index.ts
357
- var adapters = {
358
- DataLayer: pushToDataLayer,
359
- Dito: pushToDito
360
- };
361
- async function dispatchTrackEvent(eventName, data) {
362
- const eventConfig = EVENTS[eventName];
363
- if (!eventConfig) return;
364
- await Promise.allSettled(
365
- eventConfig.destinations.map(async (dest) => {
366
- try {
367
- await adapters[dest](eventName.toLowerCase(), data);
368
- } catch (err) {
369
- console.error(`[DT] Failed to send ${eventName} to ${dest}:`, err);
370
- }
371
- })
372
- );
373
- }
374
-
375
- // src/params/utils/index.ts
314
+ // src/params/legacy/utils/index.ts
376
315
  async function getProductDataById(id) {
377
316
  try {
378
317
  const response = await fetch(
@@ -560,7 +499,7 @@ function normalizeBrand(input) {
560
499
  return uniqueSortedCodes.length > 0 ? uniqueSortedCodes.join("_") : null;
561
500
  }
562
501
 
563
- // src/params/resolvers/brand/fromOrderForm.ts
502
+ // src/params/legacy/resolvers/brand/fromOrderForm.ts
564
503
  function brandFromOrderForm(context) {
565
504
  var _a;
566
505
  if (!context.orderForm && !((_a = context.orderForm.items) == null ? void 0 : _a.length)) return null;
@@ -568,14 +507,14 @@ function brandFromOrderForm(context) {
568
507
  return normalizeBrand(items);
569
508
  }
570
509
 
571
- // src/params/resolvers/brand/fromItem.ts
510
+ // src/params/legacy/resolvers/brand/fromItem.ts
572
511
  function brandFromItem(context) {
573
512
  var _a;
574
513
  const { item } = context;
575
514
  return normalizeBrand(((_a = item == null ? void 0 : item.additionalInfo) == null ? void 0 : _a.brandName) || item.brand || item.item_brand);
576
515
  }
577
516
 
578
- // src/params/brand.ts
517
+ // src/params/legacy/brand.ts
579
518
  function getBrand(context, eventName) {
580
519
  if (context == null ? void 0 : context.brand) return normalizeBrand(context.brand) || context.brand || null;
581
520
  switch (eventName) {
@@ -599,7 +538,7 @@ function getBrand(context, eventName) {
599
538
  }
600
539
  }
601
540
 
602
- // src/params/utils/itemListAttribution.ts
541
+ // src/params/legacy/utils/itemListAttribution.ts
603
542
  var LAST_SELECTED_ITEM_KEY = "last_selected_item";
604
543
  var STORAGE_VIEWED = "last_viewed_item";
605
544
  var CART_LIST_HISTORY_KEY = "cart_list_history";
@@ -780,7 +719,7 @@ function persistSelectItemOriginFromResolvedItems(items, source) {
780
719
  saveSelectedItem(productId, listName);
781
720
  }
782
721
 
783
- // src/params/resolvers/items/fromOrderForm.ts
722
+ // src/params/legacy/resolvers/items/fromOrderForm.ts
784
723
  async function itemsFromOrderForm(context) {
785
724
  var _a;
786
725
  const items = ((_a = context.orderForm) == null ? void 0 : _a.items) || [];
@@ -812,7 +751,7 @@ async function itemsFromOrderForm(context) {
812
751
  );
813
752
  }
814
753
 
815
- // src/params/resolvers/items/fromItem.ts
754
+ // src/params/legacy/resolvers/items/fromItem.ts
816
755
  async function itemsFromItem(context, event) {
817
756
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
818
757
  const { item, orderForm, itemListName } = context;
@@ -848,7 +787,7 @@ async function itemsFromItem(context, event) {
848
787
  ];
849
788
  }
850
789
 
851
- // src/params/resolvers/items/fromList.ts
790
+ // src/params/legacy/resolvers/items/fromList.ts
852
791
  async function itemsFromList(context) {
853
792
  return ((context == null ? void 0 : context.list) || []).map((item) => {
854
793
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
@@ -869,7 +808,7 @@ async function itemsFromList(context) {
869
808
  });
870
809
  }
871
810
 
872
- // src/params/resolvers/items/fromPdp.ts
811
+ // src/params/legacy/resolvers/items/fromPdp.ts
873
812
  async function itemsFromPDP(context, event) {
874
813
  var _a, _b, _c, _d;
875
814
  const { item, value, brand } = context;
@@ -900,7 +839,7 @@ async function itemsFromPDP(context, event) {
900
839
  ];
901
840
  }
902
841
 
903
- // src/params/resolvers/items/fromBanner.ts
842
+ // src/params/legacy/resolvers/items/fromBanner.ts
904
843
  async function itemsFromBanner(context) {
905
844
  var _a;
906
845
  const isUrlFromPDP = isPDP(context == null ? void 0 : context.bannerUrl);
@@ -921,7 +860,7 @@ async function itemsFromBanner(context) {
921
860
  };
922
861
  }
923
862
 
924
- // src/params/items.ts
863
+ // src/params/legacy/items.ts
925
864
  function getItems(context, eventName) {
926
865
  if (context == null ? void 0 : context.items) return context.items;
927
866
  switch (eventName) {
@@ -955,7 +894,36 @@ function getItems(context, eventName) {
955
894
  }
956
895
  }
957
896
 
958
- // src/params/resolvers/paymentType/fromOrderForm.ts
897
+ // src/core/utils.ts
898
+ var paymentTypeMap = [
899
+ ["vale", "vale cr\xE9dito"],
900
+ ["pagaleve", "pix"],
901
+ ["pix parcel", "pix parcelado"],
902
+ ["pix", "pix"],
903
+ ["apple", "apple pay"],
904
+ ["google", "google pay"],
905
+ ["boleto", "boleto"],
906
+ ["paypal", "paypal"],
907
+ ["cred", "cart\xE3o"],
908
+ ["visa", "cart\xE3o"],
909
+ ["master", "cart\xE3o"],
910
+ ["amex", "cart\xE3o"],
911
+ ["elo", "cart\xE3o"],
912
+ ["hiper", "cart\xE3o"],
913
+ ["diner", "cart\xE3o"],
914
+ ["deb", "cart\xE3o"]
915
+ ];
916
+ var toNum = (val) => typeof val === "string" ? Number(val.replace(/[^\d,]/g, "").replace(",", ".")) : val;
917
+ var documentToHash = async (document2) => {
918
+ if (!document2) return null;
919
+ const encoder = new TextEncoder();
920
+ const data = encoder.encode(document2);
921
+ const hashBuffer = await crypto.subtle.digest("SHA-256", data);
922
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
923
+ return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
924
+ };
925
+
926
+ // src/params/legacy/resolvers/paymentType/fromOrderForm.ts
959
927
  function paymentTypeFromOrderForm(context) {
960
928
  var _a, _b, _c;
961
929
  if (context.payment_type) return context.payment_type;
@@ -972,7 +940,7 @@ function paymentTypeFromOrderForm(context) {
972
940
  return match ? match[1] : null;
973
941
  }
974
942
 
975
- // src/params/resolvers/paymentType/fromOrderGroup.ts
943
+ // src/params/legacy/resolvers/paymentType/fromOrderGroup.ts
976
944
  function paymentTypeFromOrderGroup(context) {
977
945
  var _a, _b, _c, _d;
978
946
  if (!context.orderForm) return null;
@@ -985,7 +953,7 @@ function paymentTypeFromOrderGroup(context) {
985
953
  return match ? match[1] : null;
986
954
  }
987
955
 
988
- // src/params/paymentType.ts
956
+ // src/params/legacy/paymentType.ts
989
957
  function getPaymentType(context, eventName) {
990
958
  if (context == null ? void 0 : context.paymentType) return context.paymentType;
991
959
  switch (eventName) {
@@ -1082,12 +1050,13 @@ async function getUserInfo(context, eventName) {
1082
1050
  }
1083
1051
  }
1084
1052
 
1085
- // src/params/index.ts
1053
+ // src/params/legacy/index.ts
1086
1054
  var paramGetters = {
1087
1055
  brand: getBrand,
1088
1056
  items: getItems,
1089
1057
  payment_type: getPaymentType,
1090
1058
  user_info: getUserInfo,
1059
+ // global
1091
1060
  content_type: (context, eventName) => {
1092
1061
  if (context == null ? void 0 : context.content_type) return context.content_type;
1093
1062
  return `regiao`.concat(":" + context.category).concat(context.subcategory ? ":" + context.subcategory : "").concat(":" + context.componentName) || null;
@@ -1283,30 +1252,586 @@ var paramGetters = {
1283
1252
  }
1284
1253
  };
1285
1254
 
1286
- // src/formatter/index.ts
1255
+ // src/params/adapters/deco/index.ts
1256
+ var IN_STOCK_SCHEMA = "https://schema.org/InStock";
1257
+ var LIST_PRICE_SCHEMA = "https://schema.org/ListPrice";
1258
+ var decoAdapter = {
1259
+ brand: (context, eventName) => {
1260
+ var _a;
1261
+ if (context == null ? void 0 : context.brand) return context.brand;
1262
+ if (context == null ? void 0 : context.products) {
1263
+ return (_a = context.products[0].brand) == null ? void 0 : _a.name;
1264
+ }
1265
+ return null;
1266
+ },
1267
+ line_items: (context, eventName) => {
1268
+ if (context == null ? void 0 : context.line_items) return context.line_items;
1269
+ if (context == null ? void 0 : context.products) {
1270
+ return context.products.map((item) => item == null ? void 0 : item.inProductGroupWithID).filter((id) => Boolean(id));
1271
+ }
1272
+ return null;
1273
+ },
1274
+ currency: (context, eventName) => {
1275
+ var _a, _b;
1276
+ if (context == null ? void 0 : context.currency) return context.currency;
1277
+ if (context == null ? void 0 : context.products) {
1278
+ return ((_b = (_a = context.products) == null ? void 0 : _a[0].offers) == null ? void 0 : _b.priceCurrency) || null;
1279
+ }
1280
+ return null;
1281
+ },
1282
+ value: (context, eventName) => {
1283
+ if (context == null ? void 0 : context.value) return context.value;
1284
+ if (context == null ? void 0 : context.products) {
1285
+ return context.products.map((item) => {
1286
+ var _a;
1287
+ return (_a = item.offers) == null ? void 0 : _a.lowPrice;
1288
+ }).filter((price) => price !== void 0).reduce((sum, price) => sum + price, 0);
1289
+ }
1290
+ return null;
1291
+ },
1292
+ available_grid: (context, eventName) => {
1293
+ if (context == null ? void 0 : context.available_grid) return context.available_grid;
1294
+ if (context == null ? void 0 : context.products) {
1295
+ const result = context.products.flatMap(
1296
+ (product) => {
1297
+ var _a, _b, _c;
1298
+ return (_c = (_b = (_a = product.isVariantOf) == null ? void 0 : _a.hasVariant) == null ? void 0 : _b.filter(
1299
+ (variant) => {
1300
+ var _a2, _b2;
1301
+ return (_b2 = (_a2 = variant.offers) == null ? void 0 : _a2.offers) == null ? void 0 : _b2.some((o) => o.availability === IN_STOCK_SCHEMA);
1302
+ }
1303
+ ).map((variant) => variant.name).filter((name) => Boolean(name))) != null ? _c : [];
1304
+ }
1305
+ ).join(",").trim();
1306
+ return result || null;
1307
+ }
1308
+ return null;
1309
+ },
1310
+ items: (context, eventName) => {
1311
+ if (context == null ? void 0 : context.items) return context.items;
1312
+ if (context == null ? void 0 : context.products) {
1313
+ return context.products.map((item) => {
1314
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
1315
+ const listPrice = (_c = (_b = (_a = item == null ? void 0 : item.offers) == null ? void 0 : _a.offers) == null ? void 0 : _b[0].priceSpecification.find(
1316
+ (s) => s.priceType === LIST_PRICE_SCHEMA
1317
+ )) == null ? void 0 : _c.price;
1318
+ return {
1319
+ quantity: 1,
1320
+ item_brand: (_e = (_d = item.brand) == null ? void 0 : _d.name) != null ? _e : null,
1321
+ item_name: (_f = item.alternateName) != null ? _f : null,
1322
+ price: (_h = (_g = item.offers) == null ? void 0 : _g.lowPrice) != null ? _h : null,
1323
+ discount: Number(listPrice) - Number((_i = item == null ? void 0 : item.offers) == null ? void 0 : _i.lowPrice),
1324
+ item_variant: (_l = (_k = (_j = item.additionalProperty) == null ? void 0 : _j.find((p) => p.name === "Cores")) == null ? void 0 : _k.value) != null ? _l : null,
1325
+ item_ref: (_o = (_n = (_m = item.additionalProperty) == null ? void 0 : _m.find(((i) => i.name === "RefId"))) == null ? void 0 : _n.value) != null ? _o : null,
1326
+ item_category: getItemCategory3(item),
1327
+ item_category2: (_r = (_q = (_p = item.category) == null ? void 0 : _p.split(">").pop()) == null ? void 0 : _q.toLowerCase().replace(/^./, (c) => c.toUpperCase())) != null ? _r : null,
1328
+ item_id: (_t = (_s = item == null ? void 0 : item.isVariantOf) == null ? void 0 : _s.productGroupID) != null ? _t : null,
1329
+ item_sku: (_u = item.sku) != null ? _u : null,
1330
+ item_url: (_v = item.url) != null ? _v : null,
1331
+ image_url: (_x = resizeVtexImage2((_w = item == null ? void 0 : item.image) == null ? void 0 : _w[0].url)) != null ? _x : null,
1332
+ seller_id: (_B = (_A = (_z = (_y = item.offers) == null ? void 0 : _y.offers) == null ? void 0 : _z[0]) == null ? void 0 : _A.seller) != null ? _B : null,
1333
+ item_list_name: ""
1334
+ };
1335
+ });
1336
+ }
1337
+ return null;
1338
+ },
1339
+ // legacy/global params TO BE DONE (move to a global file or other adapter)
1340
+ payment_type: getPaymentType,
1341
+ user_info: getUserInfo,
1342
+ // global
1343
+ content_type: (context, eventName) => {
1344
+ if (context == null ? void 0 : context.content_type) return context.content_type;
1345
+ return `regiao`.concat(":" + context.category).concat(context.subcategory ? ":" + context.subcategory : "").concat(":" + context.componentName) || null;
1346
+ },
1347
+ region: (context, eventName) => {
1348
+ return context.region || null;
1349
+ },
1350
+ total_discount: (context, eventName) => {
1351
+ var _a, _b;
1352
+ if (context == null ? void 0 : context.totalDiscount) return context.totalDiscount;
1353
+ const totalsList = ((_a = context.orderForm) == null ? void 0 : _a.totalizers) || ((_b = context.orderForm) == null ? void 0 : _b.totals) || [];
1354
+ const items = context.orderForm.items;
1355
+ const totalDiscount = items.reduce((acc, item) => {
1356
+ const unitDiscount = Math.max(0, item.listPrice - item.sellingPrice);
1357
+ return acc + unitDiscount * item.quantity;
1358
+ }, 0);
1359
+ const discounts = totalsList.find((t) => t.id === "Discounts");
1360
+ return Math.abs(((discounts == null ? void 0 : discounts.value) || 0) + totalDiscount) / 100 || null;
1361
+ },
1362
+ subtotal: (context, eventName) => {
1363
+ var _a, _b;
1364
+ const totalsList = ((_a = context.orderForm) == null ? void 0 : _a.totalizers) || ((_b = context.orderForm) == null ? void 0 : _b.totals) || [];
1365
+ const subtotal = totalsList.filter((t) => t.id === "Items").reduce((acc, t) => acc + (t.value || 0), 0) / 100 || 0;
1366
+ return subtotal;
1367
+ },
1368
+ coupon_message: (context, eventName) => {
1369
+ var _a;
1370
+ if (context.couponMessage) return context.couponMessage;
1371
+ const messages = ((_a = context.orderForm) == null ? void 0 : _a.messages) || [];
1372
+ if (messages.length === 0) return null;
1373
+ const couponMessages = messages.filter(
1374
+ (msg) => msg.text.toLowerCase().includes("cupom")
1375
+ );
1376
+ if (couponMessages.length === 0) return null;
1377
+ return couponMessages.map((msg) => msg.text).join(" | ");
1378
+ },
1379
+ seller_cod_name: (context, eventName) => {
1380
+ return context.appliedSellerCodName || (context == null ? void 0 : context.orderForm.marketingData.utmiCampaign) || null;
1381
+ },
1382
+ coupon: (context, eventName) => {
1383
+ var _a, _b;
1384
+ return context.appliedCoupon || ((_b = (_a = context == null ? void 0 : context.orderForm) == null ? void 0 : _a.marketingData) == null ? void 0 : _b.coupon) || null;
1385
+ },
1386
+ pre_filled: (context, eventName) => {
1387
+ return !!context.preFilled;
1388
+ },
1389
+ search_term: (context, eventName) => {
1390
+ return context.searchTerm || null;
1391
+ },
1392
+ search_found: (context, eventName) => {
1393
+ return !!context.searchFound;
1394
+ },
1395
+ search_quantity: (context, eventName) => {
1396
+ return context.searchQuantity || 0;
1397
+ },
1398
+ promotion_name: (context, eventName) => {
1399
+ return context.promotionName || null;
1400
+ },
1401
+ creative_name: (context, eventName) => {
1402
+ return context.creativeName || null;
1403
+ },
1404
+ creative_slot: (context, eventName) => {
1405
+ return context.creativeSlot || null;
1406
+ },
1407
+ zipcode: (context, eventName) => {
1408
+ return context.zipCode || null;
1409
+ },
1410
+ size: (context, eventName) => {
1411
+ return context.size || null;
1412
+ },
1413
+ item_ref: (context, eventName) => {
1414
+ return context.itemRef || null;
1415
+ },
1416
+ flag_pickup: (context, eventName) => {
1417
+ var _a;
1418
+ const orderForm = context.orderForm;
1419
+ if (!orderForm) return null;
1420
+ const logisticsInfo = ((_a = orderForm.shippingData) == null ? void 0 : _a.logisticsInfo) || [];
1421
+ const hasPickup = logisticsInfo.some(
1422
+ (info) => {
1423
+ var _a2;
1424
+ return (_a2 = info.slas) == null ? void 0 : _a2.some((sla) => sla.deliveryChannel === "pickup-in-point");
1425
+ }
1426
+ );
1427
+ return hasPickup;
1428
+ },
1429
+ shippings: (context, eventName) => {
1430
+ const orderForm = context.orderForm;
1431
+ if (!orderForm) return null;
1432
+ const groupShipping = {};
1433
+ for (const info of orderForm.shippingData.logisticsInfo) {
1434
+ const item = orderForm.items[info.itemIndex];
1435
+ if (!item) continue;
1436
+ for (const sla of info.slas) {
1437
+ const key = sla.name;
1438
+ if (!groupShipping[key]) {
1439
+ groupShipping[key] = {
1440
+ shipping_tier: sla.name,
1441
+ delivery_time: parseInt(sla.shippingEstimate),
1442
+ shipping: parseFloat((sla.price / 100).toFixed(1)),
1443
+ line_items: []
1444
+ };
1445
+ }
1446
+ groupShipping[key].line_items.push(item.id);
1447
+ }
1448
+ }
1449
+ const shippings = Object.values(groupShipping).map((s) => ({
1450
+ shipping: s.shipping,
1451
+ shipping_tier: s.shipping_tier,
1452
+ delivery_time: s.delivery_time,
1453
+ line_items: s.line_items.join(",")
1454
+ }));
1455
+ return shippings || null;
1456
+ },
1457
+ shipping: (context, eventName) => {
1458
+ var _a, _b, _c;
1459
+ const logistics = (_c = (_b = (_a = context == null ? void 0 : context.orderForm) == null ? void 0 : _a.shippingData) == null ? void 0 : _b.logisticsInfo) != null ? _c : [];
1460
+ return logistics.reduce((total, item) => {
1461
+ var _a2, _b2;
1462
+ const selected = (_a2 = item.slas) == null ? void 0 : _a2.find((sla) => sla.id === item.selectedSla);
1463
+ return total + ((_b2 = selected == null ? void 0 : selected.price) != null ? _b2 : 0);
1464
+ }, 0);
1465
+ },
1466
+ shipping_tier: (context, eventName) => {
1467
+ var _a, _b, _c;
1468
+ const logistics = (_c = (_b = (_a = context == null ? void 0 : context.orderForm) == null ? void 0 : _a.shippingData) == null ? void 0 : _b.logisticsInfo) != null ? _c : [];
1469
+ const selectedSlas = logistics.map((item) => {
1470
+ var _a2;
1471
+ if (!item.selectedSla) return null;
1472
+ return ((_a2 = item.slas) == null ? void 0 : _a2.find((sla) => sla.id === item.selectedSla)) || null;
1473
+ }).filter(Boolean);
1474
+ if (selectedSlas.length === 0) return null;
1475
+ const uniqueTiers = Array.from(
1476
+ new Map(
1477
+ selectedSlas.map((sla) => {
1478
+ var _a2;
1479
+ const key = sla.deliveryChannel === "pickup-in-point" ? `pickup:${((_a2 = sla.pickupStoreInfo) == null ? void 0 : _a2.friendlyName) || sla.name || ""}` : `delivery:${sla.name || ""}`;
1480
+ return [key, sla];
1481
+ })
1482
+ ).values()
1483
+ );
1484
+ const sortedTiers = uniqueTiers.sort((a, b) => {
1485
+ var _a2, _b2;
1486
+ const aOrder = a.deliveryChannel === "pickup-in-point" ? 0 : 1;
1487
+ const bOrder = b.deliveryChannel === "pickup-in-point" ? 0 : 1;
1488
+ if (aOrder !== bOrder) return aOrder - bOrder;
1489
+ const aLabel = a.deliveryChannel === "pickup-in-point" ? ((_a2 = a.pickupStoreInfo) == null ? void 0 : _a2.friendlyName) || a.name || "" : a.name || "";
1490
+ const bLabel = b.deliveryChannel === "pickup-in-point" ? ((_b2 = b.pickupStoreInfo) == null ? void 0 : _b2.friendlyName) || b.name || "" : b.name || "";
1491
+ return aLabel.localeCompare(bLabel, "pt-BR");
1492
+ });
1493
+ const shippingTier = sortedTiers.map((sla) => {
1494
+ var _a2;
1495
+ return sla.deliveryChannel === "pickup-in-point" ? `retirada em loja${((_a2 = sla.pickupStoreInfo) == null ? void 0 : _a2.friendlyName) ? `: ${sla.pickupStoreInfo.friendlyName}` : ""}` : sla.deliveryChannel === "delivery" ? `receba em casa${sla.name ? `: ${sla.name}` : ""}` : null;
1496
+ }).filter(Boolean).join(", ");
1497
+ return shippingTier || null;
1498
+ },
1499
+ transaction_id: (context, eventName) => {
1500
+ var _a, _b;
1501
+ return ((_b = (_a = context.orderForm) == null ? void 0 : _a.orderGroup) == null ? void 0 : _b.toString()) || null;
1502
+ },
1503
+ method: (context, eventName) => {
1504
+ return context.method || null;
1505
+ },
1506
+ type: (context, eventName) => {
1507
+ return context.type || null;
1508
+ }
1509
+ };
1510
+ var getItemCategory3 = (item) => {
1511
+ const keywords = ["outlet", "bazar", "sale"];
1512
+ const regex = new RegExp(keywords.join("|"), "i");
1513
+ const value = item == null ? void 0 : item.category;
1514
+ if (value) {
1515
+ const textToTest = Array.isArray(value) ? value.join(" ") : String(value);
1516
+ const match = textToTest.match(regex);
1517
+ if (match) {
1518
+ return match[0].toLowerCase();
1519
+ }
1520
+ }
1521
+ return "Cole\xE7\xE3o";
1522
+ };
1523
+ var resizeVtexImage2 = (url, width = 500, height = 500) => {
1524
+ return url == null ? void 0 : url.replace(
1525
+ /(\/arquivos\/ids\/\d+)-\d+-\d+\//,
1526
+ `$1-${width}-${height}/`
1527
+ );
1528
+ };
1529
+
1530
+ // src/params/adapters/IntelligentSearch/index.ts
1531
+ var intelligentSearchAdapter = {
1532
+ brand: (context, eventName) => {
1533
+ if (context == null ? void 0 : context.brand) return context.brand;
1534
+ if (context == null ? void 0 : context.products) {
1535
+ return context.products.map((p) => {
1536
+ const isPDP2 = "items" in p;
1537
+ return isPDP2 ? p.brand : p.brand.name;
1538
+ }).join().trim();
1539
+ }
1540
+ return null;
1541
+ },
1542
+ line_items: (context, eventName) => {
1543
+ if (context == null ? void 0 : context.line_items) return context.line_items;
1544
+ if (context == null ? void 0 : context.products) {
1545
+ return context.products.map((item) => "productId" in item ? item.productId : item.id).filter((id) => Boolean(id));
1546
+ }
1547
+ return null;
1548
+ },
1549
+ currency: (context, eventName) => {
1550
+ if (context == null ? void 0 : context.currency) return context.currency;
1551
+ return "BRL";
1552
+ },
1553
+ value: (context, eventName) => {
1554
+ if (context == null ? void 0 : context.value) return context.value;
1555
+ if (context == null ? void 0 : context.products) {
1556
+ return context.products.map((item) => {
1557
+ var _a;
1558
+ return "items" in item ? (_a = item == null ? void 0 : item.items[0].sellers) == null ? void 0 : _a[0].commertialOffer.Price : item.offers.lowPrice;
1559
+ }).filter((price) => price !== void 0).reduce((sum, price) => sum + price, 0);
1560
+ }
1561
+ return null;
1562
+ },
1563
+ available_grid: (context, eventName) => {
1564
+ var _a;
1565
+ if (context == null ? void 0 : context.available_grid) return context.available_grid;
1566
+ if (context == null ? void 0 : context.products) {
1567
+ const result = (_a = context == null ? void 0 : context.products) == null ? void 0 : _a.map((p) => {
1568
+ const isPDP2 = "items" in p;
1569
+ return isPDP2 ? p.items.map((i) => i.Tamanho[0]) : [];
1570
+ }).join(",").trim();
1571
+ return result || null;
1572
+ }
1573
+ return null;
1574
+ },
1575
+ items: (context, eventName) => {
1576
+ if (context == null ? void 0 : context.items) return context.items;
1577
+ if (context == null ? void 0 : context.products) {
1578
+ return context.products.map((item) => {
1579
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C;
1580
+ const isPDP2 = "items" in item;
1581
+ const listPrice = isPDP2 ? (_a = item == null ? void 0 : item.items[0].sellers) == null ? void 0 : _a[0].commertialOffer.ListPrice : item.offers.highPrice;
1582
+ const price = isPDP2 ? (_b = item == null ? void 0 : item.items[0].sellers) == null ? void 0 : _b[0].commertialOffer.Price : item.offers.lowPrice;
1583
+ return {
1584
+ quantity: 1,
1585
+ item_brand: isPDP2 ? item.brand : (_c = item.brand.name) != null ? _c : null,
1586
+ item_name: isPDP2 ? item.productName : (_d = item.name) != null ? _d : null,
1587
+ price: price != null ? price : null,
1588
+ discount: Number(listPrice) - Number(price),
1589
+ item_variant: isPDP2 ? (_f = (_e = item.items[0]) == null ? void 0 : _e.name) == null ? void 0 : _f.split(" - ")[0] : (_i = (_h = (_g = item.isVariantOf) == null ? void 0 : _g.hasVariant) == null ? void 0 : _h[0].name.split(" - ")[0]) != null ? _i : null,
1590
+ item_variant2: isPDP2 ? (_k = (_j = item.items[0]) == null ? void 0 : _j.name) == null ? void 0 : _k.split(" - ")[1] : (_m = (_l = item.additionalProperty.find((p) => p.name === "Tamanho")) == null ? void 0 : _l.value) != null ? _m : null,
1591
+ item_ref: isPDP2 ? item.productReference : null,
1592
+ // sem ref no retorno do IS na multi-lib produtos PDC
1593
+ item_category: getItemCategory5(item),
1594
+ item_category2: (_n = getItemCategory22(item)) != null ? _n : null,
1595
+ item_id: isPDP2 ? item == null ? void 0 : item.productId : (_o = item.isVariantOf.productGroupID) != null ? _o : null,
1596
+ item_sku: isPDP2 ? (_p = item.items) == null ? void 0 : _p[0].itemId : (_q = item.sku) != null ? _q : null,
1597
+ item_url: isPDP2 ? item.link : null,
1598
+ // sem link/url no retorno do IS na multi-lib produtos PDC
1599
+ image_url: isPDP2 ? resizeVtexImage3((_s = (_r = item == null ? void 0 : item.items) == null ? void 0 : _r[0].images) == null ? void 0 : _s[0].imageUrl) : (_u = resizeVtexImage3((_t = item == null ? void 0 : item.image) == null ? void 0 : _t[0].url)) != null ? _u : null,
1600
+ seller_id: isPDP2 ? (_x = (_w = (_v = item.items) == null ? void 0 : _v[0]) == null ? void 0 : _w.sellers) == null ? void 0 : _x[0].sellerId : (_C = (_B = (_A = (_z = (_y = item.offers) == null ? void 0 : _y.offers) == null ? void 0 : _z.find((o) => o.availability)) == null ? void 0 : _A.seller) == null ? void 0 : _B.identifier) != null ? _C : null,
1601
+ item_list_name: ""
1602
+ };
1603
+ });
1604
+ }
1605
+ return null;
1606
+ },
1607
+ // legacy/global params TO BE DONE (move to a global file or other adapter)
1608
+ payment_type: getPaymentType,
1609
+ user_info: getUserInfo,
1610
+ // global
1611
+ content_type: (context, eventName) => {
1612
+ if (context == null ? void 0 : context.content_type) return context.content_type;
1613
+ return `regiao`.concat(":" + context.category).concat(context.subcategory ? ":" + context.subcategory : "").concat(":" + context.componentName) || null;
1614
+ },
1615
+ region: (context, eventName) => {
1616
+ return context.region || null;
1617
+ },
1618
+ total_discount: (context, eventName) => {
1619
+ var _a, _b;
1620
+ if (context == null ? void 0 : context.totalDiscount) return context.totalDiscount;
1621
+ const totalsList = ((_a = context.orderForm) == null ? void 0 : _a.totalizers) || ((_b = context.orderForm) == null ? void 0 : _b.totals) || [];
1622
+ const items = context.orderForm.items;
1623
+ const totalDiscount = items.reduce((acc, item) => {
1624
+ const unitDiscount = Math.max(0, item.listPrice - item.sellingPrice);
1625
+ return acc + unitDiscount * item.quantity;
1626
+ }, 0);
1627
+ const discounts = totalsList.find((t) => t.id === "Discounts");
1628
+ return Math.abs(((discounts == null ? void 0 : discounts.value) || 0) + totalDiscount) / 100 || null;
1629
+ },
1630
+ subtotal: (context, eventName) => {
1631
+ var _a, _b;
1632
+ const totalsList = ((_a = context.orderForm) == null ? void 0 : _a.totalizers) || ((_b = context.orderForm) == null ? void 0 : _b.totals) || [];
1633
+ const subtotal = totalsList.filter((t) => t.id === "Items").reduce((acc, t) => acc + (t.value || 0), 0) / 100 || 0;
1634
+ return subtotal;
1635
+ },
1636
+ coupon_message: (context, eventName) => {
1637
+ var _a;
1638
+ if (context.couponMessage) return context.couponMessage;
1639
+ const messages = ((_a = context.orderForm) == null ? void 0 : _a.messages) || [];
1640
+ if (messages.length === 0) return null;
1641
+ const couponMessages = messages.filter(
1642
+ (msg) => msg.text.toLowerCase().includes("cupom")
1643
+ );
1644
+ if (couponMessages.length === 0) return null;
1645
+ return couponMessages.map((msg) => msg.text).join(" | ");
1646
+ },
1647
+ seller_cod_name: (context, eventName) => {
1648
+ return context.appliedSellerCodName || (context == null ? void 0 : context.orderForm.marketingData.utmiCampaign) || null;
1649
+ },
1650
+ coupon: (context, eventName) => {
1651
+ var _a, _b;
1652
+ return context.appliedCoupon || ((_b = (_a = context == null ? void 0 : context.orderForm) == null ? void 0 : _a.marketingData) == null ? void 0 : _b.coupon) || null;
1653
+ },
1654
+ pre_filled: (context, eventName) => {
1655
+ return !!context.preFilled;
1656
+ },
1657
+ search_term: (context, eventName) => {
1658
+ return context.searchTerm || null;
1659
+ },
1660
+ search_found: (context, eventName) => {
1661
+ return !!context.searchFound;
1662
+ },
1663
+ search_quantity: (context, eventName) => {
1664
+ return context.searchQuantity || 0;
1665
+ },
1666
+ promotion_name: (context, eventName) => {
1667
+ return context.promotionName || null;
1668
+ },
1669
+ creative_name: (context, eventName) => {
1670
+ return context.creativeName || null;
1671
+ },
1672
+ creative_slot: (context, eventName) => {
1673
+ return context.creativeSlot || null;
1674
+ },
1675
+ zipcode: (context, eventName) => {
1676
+ return context.zipCode || null;
1677
+ },
1678
+ size: (context, eventName) => {
1679
+ return context.size || null;
1680
+ },
1681
+ item_ref: (context, eventName) => {
1682
+ return context.itemRef || null;
1683
+ },
1684
+ flag_pickup: (context, eventName) => {
1685
+ var _a;
1686
+ const orderForm = context.orderForm;
1687
+ if (!orderForm) return null;
1688
+ const logisticsInfo = ((_a = orderForm.shippingData) == null ? void 0 : _a.logisticsInfo) || [];
1689
+ const hasPickup = logisticsInfo.some(
1690
+ (info) => {
1691
+ var _a2;
1692
+ return (_a2 = info.slas) == null ? void 0 : _a2.some((sla) => sla.deliveryChannel === "pickup-in-point");
1693
+ }
1694
+ );
1695
+ return hasPickup;
1696
+ },
1697
+ shippings: (context, eventName) => {
1698
+ const orderForm = context.orderForm;
1699
+ if (!orderForm) return null;
1700
+ const groupShipping = {};
1701
+ for (const info of orderForm.shippingData.logisticsInfo) {
1702
+ const item = orderForm.items[info.itemIndex];
1703
+ if (!item) continue;
1704
+ for (const sla of info.slas) {
1705
+ const key = sla.name;
1706
+ if (!groupShipping[key]) {
1707
+ groupShipping[key] = {
1708
+ shipping_tier: sla.name,
1709
+ delivery_time: parseInt(sla.shippingEstimate),
1710
+ shipping: parseFloat((sla.price / 100).toFixed(1)),
1711
+ line_items: []
1712
+ };
1713
+ }
1714
+ groupShipping[key].line_items.push(item.id);
1715
+ }
1716
+ }
1717
+ const shippings = Object.values(groupShipping).map((s) => ({
1718
+ shipping: s.shipping,
1719
+ shipping_tier: s.shipping_tier,
1720
+ delivery_time: s.delivery_time,
1721
+ line_items: s.line_items.join(",")
1722
+ }));
1723
+ return shippings || null;
1724
+ },
1725
+ shipping: (context, eventName) => {
1726
+ var _a, _b, _c;
1727
+ const logistics = (_c = (_b = (_a = context == null ? void 0 : context.orderForm) == null ? void 0 : _a.shippingData) == null ? void 0 : _b.logisticsInfo) != null ? _c : [];
1728
+ return logistics.reduce((total, item) => {
1729
+ var _a2, _b2;
1730
+ const selected = (_a2 = item.slas) == null ? void 0 : _a2.find((sla) => sla.id === item.selectedSla);
1731
+ return total + ((_b2 = selected == null ? void 0 : selected.price) != null ? _b2 : 0);
1732
+ }, 0);
1733
+ },
1734
+ shipping_tier: (context, eventName) => {
1735
+ var _a, _b, _c;
1736
+ const logistics = (_c = (_b = (_a = context == null ? void 0 : context.orderForm) == null ? void 0 : _a.shippingData) == null ? void 0 : _b.logisticsInfo) != null ? _c : [];
1737
+ const selectedSlas = logistics.map((item) => {
1738
+ var _a2;
1739
+ if (!item.selectedSla) return null;
1740
+ return ((_a2 = item.slas) == null ? void 0 : _a2.find((sla) => sla.id === item.selectedSla)) || null;
1741
+ }).filter(Boolean);
1742
+ if (selectedSlas.length === 0) return null;
1743
+ const uniqueTiers = Array.from(
1744
+ new Map(
1745
+ selectedSlas.map((sla) => {
1746
+ var _a2;
1747
+ const key = sla.deliveryChannel === "pickup-in-point" ? `pickup:${((_a2 = sla.pickupStoreInfo) == null ? void 0 : _a2.friendlyName) || sla.name || ""}` : `delivery:${sla.name || ""}`;
1748
+ return [key, sla];
1749
+ })
1750
+ ).values()
1751
+ );
1752
+ const sortedTiers = uniqueTiers.sort((a, b) => {
1753
+ var _a2, _b2;
1754
+ const aOrder = a.deliveryChannel === "pickup-in-point" ? 0 : 1;
1755
+ const bOrder = b.deliveryChannel === "pickup-in-point" ? 0 : 1;
1756
+ if (aOrder !== bOrder) return aOrder - bOrder;
1757
+ const aLabel = a.deliveryChannel === "pickup-in-point" ? ((_a2 = a.pickupStoreInfo) == null ? void 0 : _a2.friendlyName) || a.name || "" : a.name || "";
1758
+ const bLabel = b.deliveryChannel === "pickup-in-point" ? ((_b2 = b.pickupStoreInfo) == null ? void 0 : _b2.friendlyName) || b.name || "" : b.name || "";
1759
+ return aLabel.localeCompare(bLabel, "pt-BR");
1760
+ });
1761
+ const shippingTier = sortedTiers.map((sla) => {
1762
+ var _a2;
1763
+ return sla.deliveryChannel === "pickup-in-point" ? `retirada em loja${((_a2 = sla.pickupStoreInfo) == null ? void 0 : _a2.friendlyName) ? `: ${sla.pickupStoreInfo.friendlyName}` : ""}` : sla.deliveryChannel === "delivery" ? `receba em casa${sla.name ? `: ${sla.name}` : ""}` : null;
1764
+ }).filter(Boolean).join(", ");
1765
+ return shippingTier || null;
1766
+ },
1767
+ transaction_id: (context, eventName) => {
1768
+ var _a, _b;
1769
+ return ((_b = (_a = context.orderForm) == null ? void 0 : _a.orderGroup) == null ? void 0 : _b.toString()) || null;
1770
+ },
1771
+ method: (context, eventName) => {
1772
+ return context.method || null;
1773
+ },
1774
+ type: (context, eventName) => {
1775
+ return context.type || null;
1776
+ }
1777
+ };
1778
+ var getItemCategory5 = (item) => {
1779
+ const keywords = ["outlet", "bazar", "sale"];
1780
+ const regex = new RegExp(keywords.join("|"), "i");
1781
+ const value = item == null ? void 0 : item.categories;
1782
+ if (value) {
1783
+ const textToTest = Array.isArray(value) ? value.join(" ") : String(value);
1784
+ const match = textToTest.match(regex);
1785
+ if (match) {
1786
+ return match[0].toLowerCase();
1787
+ }
1788
+ }
1789
+ return "Cole\xE7\xE3o";
1790
+ };
1791
+ var getItemCategory22 = (item) => {
1792
+ var _a, _b, _c;
1793
+ const avoidList = /outlet|bazar|sale/i;
1794
+ const lastCategory = (_b = (_a = item.categories) == null ? void 0 : _a[item.categories.length - 1]) != null ? _b : "";
1795
+ const categoryHierarchy = lastCategory.split("/").filter(Boolean);
1796
+ const deepestCategory = (_c = [...categoryHierarchy].reverse().find((c) => !avoidList.test(c))) != null ? _c : "";
1797
+ return deepestCategory && deepestCategory[0].toUpperCase() + deepestCategory.slice(1).toLowerCase();
1798
+ };
1799
+ function resizeVtexImage3(url, width = 500, height = 500) {
1800
+ return url == null ? void 0 : url.replace(
1801
+ /(\/arquivos\/ids\/\d+)-\d+-\d+\//,
1802
+ `$1-${width}-${height}/`
1803
+ );
1804
+ }
1805
+
1806
+ // src/formatter.ts
1807
+ var adapters = {
1808
+ DECO: decoAdapter,
1809
+ IS: intelligentSearchAdapter
1810
+ };
1287
1811
  async function getParameters(context = {}, eventName) {
1812
+ var _a;
1288
1813
  const eventConfig = EVENTS[eventName];
1289
1814
  if (!eventConfig) return;
1290
- const params = Object.fromEntries(
1291
- await Promise.all(
1292
- eventConfig.requiredParams.map(async (param) => {
1293
- const getter = paramGetters[param];
1294
- if (typeof getter !== "function") {
1295
- console.error(`[DT] Missing paramGetter for param: ${param}`);
1296
- return [param, null];
1297
- }
1298
- return [param, await getter(context, eventName)];
1299
- })
1300
- )
1815
+ const source = context.adapter;
1816
+ const sourceAdapters = (_a = adapters[source]) != null ? _a : paramGetters;
1817
+ const entries = await Promise.all(
1818
+ eventConfig.requiredParams.map(async (param) => {
1819
+ const adapter = sourceAdapters[param];
1820
+ if (typeof adapter !== "function") {
1821
+ console.error(`[DT] Missing adapter "${param}" for source "${source != null ? source : "default"}"`);
1822
+ return [param, null];
1823
+ }
1824
+ return [param, await adapter(context, eventName)];
1825
+ })
1301
1826
  );
1302
- return params;
1827
+ return Object.fromEntries(entries);
1303
1828
  }
1304
1829
 
1305
1830
  // src/index.ts
1306
1831
  async function trackWebEvent(event, context = {}) {
1307
1832
  try {
1308
1833
  const parameters = await getParameters(context, event);
1309
- return await dispatchTrackEvent(
1834
+ return await pushToDataLayer(
1310
1835
  event,
1311
1836
  Object.assign({}, parameters, { window: context.window })
1312
1837
  );