@anker-in/shopify-react 0.1.1-beta.0 → 0.1.1-beta.1

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.
@@ -597,8 +597,6 @@ function useAddCartLines(options) {
597
597
  );
598
598
  return useSWRMutation("add-cart-lines", addLines, options);
599
599
  }
600
-
601
- // src/tracking/ga.ts
602
600
  var gaTrack = (data) => {
603
601
  if (typeof window === "undefined") {
604
602
  return;
@@ -616,18 +614,15 @@ var gaTrack = (data) => {
616
614
  };
617
615
  var trackAddToCartGA = ({
618
616
  lineItems = [],
619
- gtmParams = {},
620
- brand
617
+ gtmParams = {}
621
618
  }) => {
622
619
  if (!lineItems.length || !lineItems[0]?.variant) {
623
620
  return;
624
621
  }
625
622
  const { variant } = lineItems[0];
626
- const currencyCode = variant?.price?.currencyCode;
627
- const totalPrice = lineItems.reduce((sum, item) => {
628
- const price = parseFloat(item.variant?.price?.amount || "0");
629
- return sum + price;
630
- }, 0);
623
+ const currencyCode = variant.product?.price?.currencyCode;
624
+ const price = variant.compareAtPrice?.amount ?? (variant.price?.amount || 0);
625
+ const totalPrice = lineItems?.reduce((prev, { variant: variant2 }) => prev.plus(variant2?.finalPrice?.amount ?? price), new Decimal2(0)).toNumber();
631
626
  gaTrack({
632
627
  event: "ga4Event",
633
628
  event_name: "add_to_cart",
@@ -638,11 +633,11 @@ var trackAddToCartGA = ({
638
633
  position: gtmParams?.position || "",
639
634
  items: lineItems.map(({ variant: variant2, quantity }) => ({
640
635
  item_id: variant2?.sku,
641
- item_name: variant2?.product?.title || variant2?.product?.name,
642
- item_brand: brand || gtmParams?.brand || "",
636
+ item_name: variant2?.product?.title || variant2?.product?.title,
637
+ item_brand: gtmParams?.brand || "",
643
638
  item_category: variant2?.product?.productType || "",
644
- item_variant: variant2?.title || variant2?.name,
645
- price: variant2?.finalPrice?.amount ?? variant2?.price?.amount,
639
+ item_variant: variant2?.title || variant2?.title,
640
+ price,
646
641
  quantity: quantity || 1
647
642
  })),
648
643
  ...gtmParams?.ga4Params
@@ -651,19 +646,15 @@ var trackAddToCartGA = ({
651
646
  };
652
647
  var trackBuyNowGA = ({
653
648
  lineItems = [],
654
- gtmParams = {},
655
- brand
649
+ gtmParams = {}
656
650
  }) => {
657
651
  if (!lineItems.length || !lineItems[0]?.variant) {
658
652
  return;
659
653
  }
660
654
  const { variant } = lineItems[0];
661
655
  const currencyCode = variant.price?.currencyCode;
662
- const totalPrice = lineItems.reduce((sum, item) => {
663
- const price = parseFloat(item.finalPrice?.amount || item.variant?.price?.amount || "0");
664
- const quantity = item.quantity || 1;
665
- return sum + price * quantity;
666
- }, 0);
656
+ const price = variant.compareAtPrice?.amount ?? (variant.price?.amount || 0);
657
+ const totalPrice = lineItems?.reduce((prev, { variant: variant2 }) => prev.plus(variant2?.finalPrice?.amount ?? price), new Decimal2(0)).toNumber();
667
658
  gaTrack({
668
659
  event: "ga4Event",
669
660
  event_name: "begin_checkout",
@@ -675,10 +666,10 @@ var trackBuyNowGA = ({
675
666
  items: lineItems.map((item) => ({
676
667
  item_id: item.variant?.sku,
677
668
  item_name: item.variant?.product?.title || item.variant?.title,
678
- item_brand: item.variant?.product?.vendor || brand || gtmParams?.brand || "",
669
+ item_brand: gtmParams?.brand || "",
679
670
  item_category: item.variant?.product?.productType || "",
680
671
  item_variant: item.variant?.title,
681
- price: item.finalPrice?.amount || item.variant?.price?.amount,
672
+ price,
682
673
  quantity: item.quantity || 1
683
674
  })),
684
675
  ...gtmParams?.ga4Params
@@ -692,10 +683,10 @@ var trackAddToCartFBQ = ({ lineItems = [] }) => {
692
683
  return;
693
684
  }
694
685
  if (lineItems.length && lineItems[0]?.variant) {
695
- const { variant, quantity, finalPrice } = lineItems[0];
686
+ const { variant, quantity } = lineItems[0];
696
687
  try {
697
688
  window.fbq("track", "AddToCart", {
698
- value: finalPrice?.amount || variant?.price?.amount,
689
+ value: variant?.compareAtPrice?.amount ?? (variant?.price?.amount || variant?.price || 0),
699
690
  num_items: quantity,
700
691
  currency: variant?.price?.currencyCode,
701
692
  content_name: variant?.product?.title,
@@ -782,9 +773,9 @@ function useRemoveCartCodes(options) {
782
773
  }
783
774
 
784
775
  // src/hooks/cart/use-add-to-cart.ts
785
- function useAddToCart({ withTrack = true, brand } = {}, swrOptions) {
786
- const { client, locale, cartCookieAdapter, userAdapter } = useShopify();
787
- const { mutateCart, cart, metafieldIdentifiers } = useCartContext();
776
+ function useAddToCart({ withTrack = true } = {}, swrOptions) {
777
+ const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
778
+ const { cart } = useCartContext();
788
779
  const { trigger: applyCartCodes } = useApplyCartCodes();
789
780
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
790
781
  const { trigger: addCartLines2 } = useAddCartLines();
@@ -806,7 +797,8 @@ function useAddToCart({ withTrack = true, brand } = {}, swrOptions) {
806
797
  const lines = lineItems.map((item) => ({
807
798
  merchandiseId: item.variant?.id || "",
808
799
  quantity: item.quantity || 1,
809
- attributes: item.attributes
800
+ attributes: item.attributes,
801
+ sellingPlanId: item.sellingPlanId
810
802
  })).filter((item) => item.merchandiseId && item.quantity);
811
803
  if (lines.length === 0) {
812
804
  return;
@@ -841,34 +833,16 @@ function useAddToCart({ withTrack = true, brand } = {}, swrOptions) {
841
833
  discountCodes
842
834
  });
843
835
  }
844
- if (withTrack && resultCart.lineItems) {
845
- const trackingLineItems = resultCart.lineItems.map((line) => ({
846
- variant: {
847
- id: line.variant.id,
848
- sku: line.variant.sku || "",
849
- title: line.variant.name,
850
- price: {
851
- amount: String(line.variant.price),
852
- currencyCode: resultCart.currency.code
853
- },
854
- product: line.product ? {
855
- title: line.product.title || line.name,
856
- productType: line.product.productType,
857
- vendor: line.product.vendor
858
- } : void 0
859
- },
860
- quantity: line.quantity
861
- }));
836
+ if (withTrack) {
862
837
  trackAddToCartGA({
863
- lineItems: trackingLineItems,
864
- gtmParams: { ...gtmParams, brand },
865
- brand
838
+ lineItems,
839
+ gtmParams: { ...gtmParams, brand: config.getBrand() }
866
840
  });
867
- trackAddToCartFBQ({ lineItems: trackingLineItems });
841
+ trackAddToCartFBQ({ lineItems });
868
842
  }
869
843
  return resultCart;
870
844
  },
871
- [client, locale, cartCookieAdapter, userAdapter, cart, withTrack, brand]
845
+ [client, locale, cartCookieAdapter, userAdapter, cart, withTrack]
872
846
  );
873
847
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
874
848
  }
@@ -949,8 +923,8 @@ function useUpdateCartAttributes(mutate, metafieldIdentifiers, options) {
949
923
  );
950
924
  return useSWRMutation("update-cart-attributes", updateAttributes, options);
951
925
  }
952
- function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
953
- const { client, locale, cartCookieAdapter, userAdapter } = useShopify();
926
+ function useBuyNow({ withTrack = true } = {}, swrOptions) {
927
+ const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
954
928
  const isLoggedIn = userAdapter?.isLoggedIn || false;
955
929
  const buyNow = useCallback(
956
930
  async (_key, { arg }) => {
@@ -968,9 +942,10 @@ function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
968
942
  return;
969
943
  }
970
944
  const lines = lineItems.map((item) => ({
971
- merchandiseId: item.variant?.id || item.variantId || "",
945
+ merchandiseId: item.variant?.id || "",
972
946
  quantity: item.quantity || 1,
973
- attributes: item.attributes
947
+ attributes: item.attributes,
948
+ sellingPlanId: item.sellingPlanId
974
949
  })).filter((item) => item.merchandiseId && item.quantity);
975
950
  if (lines.length === 0) {
976
951
  return;
@@ -987,27 +962,9 @@ function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
987
962
  throw new Error("Failed to create cart for buy now");
988
963
  }
989
964
  if (withTrack && resultCart.lineItems) {
990
- const trackingLineItems = resultCart.lineItems.map((line) => ({
991
- variant: {
992
- id: line.variantId,
993
- sku: line.variant.sku || "",
994
- title: line.variant.name,
995
- price: {
996
- amount: String(line.variant.price),
997
- currencyCode: resultCart.currency.code
998
- },
999
- product: line.product ? {
1000
- title: line.product.title || line.name,
1001
- productType: line.product.productType,
1002
- vendor: line.product.vendor
1003
- } : void 0
1004
- },
1005
- quantity: line.quantity
1006
- }));
1007
965
  trackBuyNowGA({
1008
- lineItems: trackingLineItems,
1009
- gtmParams: { ...gtmParams, brand },
1010
- brand
966
+ lineItems,
967
+ gtmParams: { ...gtmParams, brand: config.getBrand() }
1011
968
  });
1012
969
  if (fbqTrackConfig) {
1013
970
  trackBuyNowFBQ({ trackConfig: fbqTrackConfig });
@@ -1024,7 +981,7 @@ function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
1024
981
  }
1025
982
  return resultCart;
1026
983
  },
1027
- [client, locale, isLoggedIn, cartCookieAdapter, withTrack, brand]
984
+ [client, locale, isLoggedIn, cartCookieAdapter, withTrack]
1028
985
  );
1029
986
  return useSWRMutation("buy-now", buyNow, swrOptions);
1030
987
  }