@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.
@@ -769,17 +769,11 @@ var trackAddToCartGA = ({
769
769
  lineItems = [],
770
770
  gtmParams = {}
771
771
  }) => {
772
- if (!lineItems.length || !lineItems[0]?.variant) {
772
+ if (!lineItems.length) {
773
773
  return;
774
774
  }
775
- const { variant } = lineItems[0];
776
- const currencyCode = variant.product?.price?.currencyCode;
777
- const totalPrice = lineItems?.reduce(
778
- (prev, { variant: variant2 }) => prev.plus(
779
- variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
780
- ),
781
- new Decimal2(0)
782
- ).toNumber();
775
+ const currencyCode = lineItems[0].product?.price?.currencyCode;
776
+ const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.variant.price || 0), new Decimal2(0)).toNumber();
783
777
  gaTrack({
784
778
  event: "ga4Event",
785
779
  event_name: "add_to_cart",
@@ -789,19 +783,19 @@ var trackAddToCartGA = ({
789
783
  value: totalPrice,
790
784
  position: gtmParams?.position || "",
791
785
  items: lineItems.map((item) => {
792
- const imageUrl = item.variant?.image?.url || item.variant?.product?.images?.[0]?.url;
786
+ const imageUrl = item.variant.image?.url || item.product?.images?.[0]?.url;
793
787
  const itemCategoryId = item.gtmParams?.item_category_id;
794
- const itemVariantId = item.variant?.id ? atobID(item.variant.id) : void 0;
788
+ const itemVariantId = item.variant.id ? atobID(item.variant.id) : void 0;
795
789
  return {
796
- item_id: item.variant?.sku,
797
- item_name: item.variant?.product?.title || item.variant?.product?.title,
790
+ item_id: item.variant.sku,
791
+ item_name: item.product?.title,
798
792
  item_brand: gtmParams?.brand || "",
799
- item_category: item.variant?.product?.productType || "",
800
- item_variant: item.variant?.title || item.variant?.title,
801
- price: item.variant?.compareAtPrice?.amount ?? item.variant?.price?.amount,
793
+ item_category: item.product?.productType || "",
794
+ item_variant: item.variant.name,
795
+ price: item.variant.listPrice ?? item.variant.price,
802
796
  quantity: item.quantity || 1,
803
797
  ...imageUrl && { image_url: imageUrl },
804
- ...itemCategoryId && { item_category_id: itemCategoryId },
798
+ ...itemCategoryId !== void 0 && { item_category_id: itemCategoryId },
805
799
  ...itemVariantId && { item_variant_id: itemVariantId }
806
800
  };
807
801
  }),
@@ -901,21 +895,24 @@ var getGA4Data = async (measurementId = "G-R0BRMRK4CY") => {
901
895
  };
902
896
 
903
897
  // src/tracking/fbq.ts
904
- var trackAddToCartFBQ = ({ lineItems = [] }) => {
898
+ var trackAddToCartFBQ = ({
899
+ lineItems = []
900
+ }) => {
905
901
  if (typeof window === "undefined" || !window.fbq) {
906
902
  return;
907
903
  }
908
- if (lineItems.length && lineItems[0]?.variant) {
909
- const { variant, quantity } = lineItems[0];
904
+ if (lineItems.length) {
905
+ const item = lineItems[0];
906
+ const { variant, quantity, product } = item;
910
907
  try {
911
908
  window.fbq("track", "AddToCart", {
912
- value: variant?.compareAtPrice?.amount ?? (variant?.price?.amount || variant?.price || 0),
909
+ value: variant.listPrice || variant.price,
913
910
  num_items: quantity,
914
- currency: variant?.price?.currencyCode,
915
- content_name: variant?.product?.title,
911
+ currency: product?.price?.currencyCode,
912
+ content_name: product?.title,
916
913
  content_type: "product_group",
917
- content_ids: String(variant?.id),
918
- content_category: variant?.product?.metafields?.global?.trafficType || "public"
914
+ content_ids: String(variant.id),
915
+ content_category: product?.metafields?.global?.trafficType || "public"
919
916
  });
920
917
  } catch (error) {
921
918
  console.error("FBQ tracking error:", error);
@@ -1031,13 +1028,14 @@ var initSameLinesAttributes = ({
1031
1028
  var initDiscountAttributes = ({ line }) => {
1032
1029
  let itemAttributes = line.attributes || [];
1033
1030
  const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
1034
- if (!functionEnvAttribute) {
1031
+ const priceAmount = line.variant?.finalPrice?.amount ?? line.variant?.price?.amount;
1032
+ if (!functionEnvAttribute && priceAmount !== void 0 && priceAmount !== null) {
1035
1033
  itemAttributes = itemAttributes.concat([
1036
1034
  {
1037
1035
  key: CUSTOMER_ATTRIBUTE_KEY,
1038
1036
  value: JSON.stringify({
1039
1037
  is_gift: false,
1040
- discounted_amount: line.variant?.finalPrice?.amount === void 0 ? Number(line.variant?.price?.amount) * (line.quantity || 1) : Number(line.variant?.finalPrice?.amount) * (line.quantity || 1)
1038
+ discounted_amount: new Decimal2(priceAmount).times(line.quantity || 1).toNumber()
1041
1039
  })
1042
1040
  }
1043
1041
  ]);
@@ -2376,12 +2374,25 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2376
2374
  addCustomAttributes(customAttributes);
2377
2375
  }
2378
2376
  }
2379
- if (withTrack) {
2377
+ if (withTrack && resultCart) {
2378
+ const trackingLineItems = lineItems.map((inputLine) => {
2379
+ const cartLine = resultCart.lineItems.find(
2380
+ (line) => line.variant.id === inputLine.variant?.id
2381
+ );
2382
+ if (!cartLine) {
2383
+ return null;
2384
+ }
2385
+ return {
2386
+ ...cartLine,
2387
+ quantity: inputLine.quantity,
2388
+ gtmParams: inputLine.gtmParams
2389
+ };
2390
+ }).filter(Boolean);
2380
2391
  trackAddToCartGA({
2381
- lineItems,
2392
+ lineItems: trackingLineItems,
2382
2393
  gtmParams: { ...gtmParams, brand: config.getBrand() }
2383
2394
  });
2384
- trackAddToCartFBQ({ lineItems });
2395
+ trackAddToCartFBQ({ lineItems: trackingLineItems });
2385
2396
  }
2386
2397
  performanceAdapter?.addToCartEnd();
2387
2398
  return resultCart;