@anker-in/shopify-react 1.2.2-beta.2 → 1.2.2-beta.4

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/index.mjs CHANGED
@@ -858,17 +858,11 @@ var trackAddToCartGA = ({
858
858
  lineItems = [],
859
859
  gtmParams = {}
860
860
  }) => {
861
- if (!lineItems.length || !lineItems[0]?.variant) {
861
+ if (!lineItems.length) {
862
862
  return;
863
863
  }
864
- const { variant } = lineItems[0];
865
- const currencyCode = variant.product?.price?.currencyCode;
866
- const totalPrice = lineItems?.reduce(
867
- (prev, { variant: variant2 }) => prev.plus(
868
- variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
869
- ),
870
- new Decimal2(0)
871
- ).toNumber();
864
+ const currencyCode = lineItems[0].product?.price?.currencyCode;
865
+ const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.variant.price || 0), new Decimal2(0)).toNumber();
872
866
  gaTrack({
873
867
  event: "ga4Event",
874
868
  event_name: "add_to_cart",
@@ -878,19 +872,19 @@ var trackAddToCartGA = ({
878
872
  value: totalPrice,
879
873
  position: gtmParams?.position || "",
880
874
  items: lineItems.map((item) => {
881
- const imageUrl = item.variant?.image?.url || item.variant?.product?.images?.[0]?.url;
875
+ const imageUrl = item.variant.image?.url || item.product?.images?.[0]?.url;
882
876
  const itemCategoryId = item.gtmParams?.item_category_id;
883
- const itemVariantId = item.variant?.id ? atobID(item.variant.id) : void 0;
877
+ const itemVariantId = item.variant.id ? atobID(item.variant.id) : void 0;
884
878
  return {
885
- item_id: item.variant?.sku,
886
- item_name: item.variant?.product?.title || item.variant?.product?.title,
879
+ item_id: item.variant.sku,
880
+ item_name: item.product?.title,
887
881
  item_brand: gtmParams?.brand || "",
888
- item_category: item.variant?.product?.productType || "",
889
- item_variant: item.variant?.title || item.variant?.title,
890
- price: item.variant?.compareAtPrice?.amount ?? item.variant?.price?.amount,
882
+ item_category: item.product?.productType || "",
883
+ item_variant: item.variant.name,
884
+ price: item.variant.listPrice ?? item.variant.price,
891
885
  quantity: item.quantity || 1,
892
886
  ...imageUrl && { image_url: imageUrl },
893
- ...itemCategoryId && { item_category_id: itemCategoryId },
887
+ ...itemCategoryId !== void 0 && { item_category_id: itemCategoryId },
894
888
  ...itemVariantId && { item_variant_id: itemVariantId }
895
889
  };
896
890
  }),
@@ -1071,21 +1065,24 @@ var getGA4Data = async (measurementId = "G-R0BRMRK4CY") => {
1071
1065
  };
1072
1066
 
1073
1067
  // src/tracking/fbq.ts
1074
- var trackAddToCartFBQ = ({ lineItems = [] }) => {
1068
+ var trackAddToCartFBQ = ({
1069
+ lineItems = []
1070
+ }) => {
1075
1071
  if (typeof window === "undefined" || !window.fbq) {
1076
1072
  return;
1077
1073
  }
1078
- if (lineItems.length && lineItems[0]?.variant) {
1079
- const { variant, quantity } = lineItems[0];
1074
+ if (lineItems.length) {
1075
+ const item = lineItems[0];
1076
+ const { variant, quantity, product } = item;
1080
1077
  try {
1081
1078
  window.fbq("track", "AddToCart", {
1082
- value: variant?.compareAtPrice?.amount ?? (variant?.price?.amount || variant?.price || 0),
1079
+ value: variant.listPrice || variant.price,
1083
1080
  num_items: quantity,
1084
- currency: variant?.price?.currencyCode,
1085
- content_name: variant?.product?.title,
1081
+ currency: product?.price?.currencyCode,
1082
+ content_name: product?.title,
1086
1083
  content_type: "product_group",
1087
- content_ids: String(variant?.id),
1088
- content_category: variant?.product?.metafields?.global?.trafficType || "public"
1084
+ content_ids: String(variant.id),
1085
+ content_category: product?.metafields?.global?.trafficType || "public"
1089
1086
  });
1090
1087
  } catch (error) {
1091
1088
  console.error("FBQ tracking error:", error);
@@ -1201,13 +1198,14 @@ var initSameLinesAttributes = ({
1201
1198
  var initDiscountAttributes = ({ line }) => {
1202
1199
  let itemAttributes = line.attributes || [];
1203
1200
  const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
1204
- if (!functionEnvAttribute) {
1201
+ const priceAmount = line.variant?.finalPrice?.amount ?? line.variant?.price?.amount;
1202
+ if (!functionEnvAttribute && priceAmount !== void 0 && priceAmount !== null) {
1205
1203
  itemAttributes = itemAttributes.concat([
1206
1204
  {
1207
1205
  key: CUSTOMER_ATTRIBUTE_KEY,
1208
1206
  value: JSON.stringify({
1209
1207
  is_gift: false,
1210
- discounted_amount: line.variant?.finalPrice?.amount === void 0 ? Number(line.variant?.price?.amount) * (line.quantity || 1) : Number(line.variant?.finalPrice?.amount) * (line.quantity || 1)
1208
+ discounted_amount: new Decimal2(priceAmount).times(line.quantity || 1).toNumber()
1211
1209
  })
1212
1210
  }
1213
1211
  ]);
@@ -2546,12 +2544,25 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2546
2544
  addCustomAttributes(customAttributes);
2547
2545
  }
2548
2546
  }
2549
- if (withTrack) {
2547
+ if (withTrack && resultCart) {
2548
+ const trackingLineItems = lineItems.map((inputLine) => {
2549
+ const cartLine = resultCart.lineItems.find(
2550
+ (line) => line.variant.id === inputLine.variant?.id
2551
+ );
2552
+ if (!cartLine) {
2553
+ return null;
2554
+ }
2555
+ return {
2556
+ ...cartLine,
2557
+ quantity: inputLine.quantity,
2558
+ gtmParams: inputLine.gtmParams
2559
+ };
2560
+ }).filter(Boolean);
2550
2561
  trackAddToCartGA({
2551
- lineItems,
2562
+ lineItems: trackingLineItems,
2552
2563
  gtmParams: { ...gtmParams, brand: config.getBrand() }
2553
2564
  });
2554
- trackAddToCartFBQ({ lineItems });
2565
+ trackAddToCartFBQ({ lineItems: trackingLineItems });
2555
2566
  }
2556
2567
  performanceAdapter?.addToCartEnd();
2557
2568
  return resultCart;