@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.
package/dist/index.mjs CHANGED
@@ -659,8 +659,6 @@ function useAddCartLines(options) {
659
659
  );
660
660
  return useSWRMutation("add-cart-lines", addLines, options);
661
661
  }
662
-
663
- // src/tracking/ga.ts
664
662
  var gaTrack = (data) => {
665
663
  if (typeof window === "undefined") {
666
664
  return;
@@ -678,18 +676,15 @@ var gaTrack = (data) => {
678
676
  };
679
677
  var trackAddToCartGA = ({
680
678
  lineItems = [],
681
- gtmParams = {},
682
- brand
679
+ gtmParams = {}
683
680
  }) => {
684
681
  if (!lineItems.length || !lineItems[0]?.variant) {
685
682
  return;
686
683
  }
687
684
  const { variant } = lineItems[0];
688
- const currencyCode = variant?.price?.currencyCode;
689
- const totalPrice = lineItems.reduce((sum, item) => {
690
- const price = parseFloat(item.variant?.price?.amount || "0");
691
- return sum + price;
692
- }, 0);
685
+ const currencyCode = variant.product?.price?.currencyCode;
686
+ const price = variant.compareAtPrice?.amount ?? (variant.price?.amount || 0);
687
+ const totalPrice = lineItems?.reduce((prev, { variant: variant2 }) => prev.plus(variant2?.finalPrice?.amount ?? price), new Decimal2(0)).toNumber();
693
688
  gaTrack({
694
689
  event: "ga4Event",
695
690
  event_name: "add_to_cart",
@@ -700,11 +695,11 @@ var trackAddToCartGA = ({
700
695
  position: gtmParams?.position || "",
701
696
  items: lineItems.map(({ variant: variant2, quantity }) => ({
702
697
  item_id: variant2?.sku,
703
- item_name: variant2?.product?.title || variant2?.product?.name,
704
- item_brand: brand || gtmParams?.brand || "",
698
+ item_name: variant2?.product?.title || variant2?.product?.title,
699
+ item_brand: gtmParams?.brand || "",
705
700
  item_category: variant2?.product?.productType || "",
706
- item_variant: variant2?.title || variant2?.name,
707
- price: variant2?.finalPrice?.amount ?? variant2?.price?.amount,
701
+ item_variant: variant2?.title || variant2?.title,
702
+ price,
708
703
  quantity: quantity || 1
709
704
  })),
710
705
  ...gtmParams?.ga4Params
@@ -714,13 +709,14 @@ var trackAddToCartGA = ({
714
709
  var trackBeginCheckoutGA = ({
715
710
  lineItems = [],
716
711
  currencyCode,
717
- totalPrice,
718
- gtmParams = {},
719
- brand
712
+ gtmParams = {}
720
713
  }) => {
721
714
  if (!lineItems.length) {
722
715
  return;
723
716
  }
717
+ const { variant } = lineItems[0] || {};
718
+ const price = variant?.compareAtPrice?.amount ?? (variant?.price?.amount || 0);
719
+ const totalPrice = lineItems?.reduce((prev, { variant: variant2 }) => prev.plus(variant2?.finalPrice?.amount ?? price), new Decimal2(0)).toNumber();
724
720
  gaTrack({
725
721
  event: "ga4Event",
726
722
  event_name: "begin_checkout",
@@ -732,10 +728,10 @@ var trackBeginCheckoutGA = ({
732
728
  items: lineItems.map((item) => ({
733
729
  item_id: item.variant?.sku,
734
730
  item_name: item.variant?.product?.title,
735
- item_brand: brand || gtmParams?.brand || "",
731
+ item_brand: gtmParams?.brand || "",
736
732
  item_category: item.variant?.product?.productType,
737
733
  item_variant: item.variant?.title,
738
- price: item.finalPrice?.amount || item.variant?.price?.amount,
734
+ price,
739
735
  quantity: item.quantity || 1
740
736
  })),
741
737
  ...gtmParams?.ga4Params
@@ -744,19 +740,15 @@ var trackBeginCheckoutGA = ({
744
740
  };
745
741
  var trackBuyNowGA = ({
746
742
  lineItems = [],
747
- gtmParams = {},
748
- brand
743
+ gtmParams = {}
749
744
  }) => {
750
745
  if (!lineItems.length || !lineItems[0]?.variant) {
751
746
  return;
752
747
  }
753
748
  const { variant } = lineItems[0];
754
749
  const currencyCode = variant.price?.currencyCode;
755
- const totalPrice = lineItems.reduce((sum, item) => {
756
- const price = parseFloat(item.finalPrice?.amount || item.variant?.price?.amount || "0");
757
- const quantity = item.quantity || 1;
758
- return sum + price * quantity;
759
- }, 0);
750
+ const price = variant.compareAtPrice?.amount ?? (variant.price?.amount || 0);
751
+ const totalPrice = lineItems?.reduce((prev, { variant: variant2 }) => prev.plus(variant2?.finalPrice?.amount ?? price), new Decimal2(0)).toNumber();
760
752
  gaTrack({
761
753
  event: "ga4Event",
762
754
  event_name: "begin_checkout",
@@ -768,10 +760,10 @@ var trackBuyNowGA = ({
768
760
  items: lineItems.map((item) => ({
769
761
  item_id: item.variant?.sku,
770
762
  item_name: item.variant?.product?.title || item.variant?.title,
771
- item_brand: item.variant?.product?.vendor || brand || gtmParams?.brand || "",
763
+ item_brand: gtmParams?.brand || "",
772
764
  item_category: item.variant?.product?.productType || "",
773
765
  item_variant: item.variant?.title,
774
- price: item.finalPrice?.amount || item.variant?.price?.amount,
766
+ price,
775
767
  quantity: item.quantity || 1
776
768
  })),
777
769
  ...gtmParams?.ga4Params
@@ -785,10 +777,10 @@ var trackAddToCartFBQ = ({ lineItems = [] }) => {
785
777
  return;
786
778
  }
787
779
  if (lineItems.length && lineItems[0]?.variant) {
788
- const { variant, quantity, finalPrice } = lineItems[0];
780
+ const { variant, quantity } = lineItems[0];
789
781
  try {
790
782
  window.fbq("track", "AddToCart", {
791
- value: finalPrice?.amount || variant?.price?.amount,
783
+ value: variant?.compareAtPrice?.amount ?? (variant?.price?.amount || variant?.price || 0),
792
784
  num_items: quantity,
793
785
  currency: variant?.price?.currencyCode,
794
786
  content_name: variant?.product?.title,
@@ -875,9 +867,9 @@ function useRemoveCartCodes(options) {
875
867
  }
876
868
 
877
869
  // src/hooks/cart/use-add-to-cart.ts
878
- function useAddToCart({ withTrack = true, brand } = {}, swrOptions) {
879
- const { client, locale, cartCookieAdapter, userAdapter } = useShopify();
880
- const { mutateCart, cart, metafieldIdentifiers } = useCartContext();
870
+ function useAddToCart({ withTrack = true } = {}, swrOptions) {
871
+ const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
872
+ const { cart } = useCartContext();
881
873
  const { trigger: applyCartCodes } = useApplyCartCodes();
882
874
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
883
875
  const { trigger: addCartLines2 } = useAddCartLines();
@@ -899,7 +891,8 @@ function useAddToCart({ withTrack = true, brand } = {}, swrOptions) {
899
891
  const lines = lineItems.map((item) => ({
900
892
  merchandiseId: item.variant?.id || "",
901
893
  quantity: item.quantity || 1,
902
- attributes: item.attributes
894
+ attributes: item.attributes,
895
+ sellingPlanId: item.sellingPlanId
903
896
  })).filter((item) => item.merchandiseId && item.quantity);
904
897
  if (lines.length === 0) {
905
898
  return;
@@ -934,34 +927,16 @@ function useAddToCart({ withTrack = true, brand } = {}, swrOptions) {
934
927
  discountCodes
935
928
  });
936
929
  }
937
- if (withTrack && resultCart.lineItems) {
938
- const trackingLineItems = resultCart.lineItems.map((line) => ({
939
- variant: {
940
- id: line.variant.id,
941
- sku: line.variant.sku || "",
942
- title: line.variant.name,
943
- price: {
944
- amount: String(line.variant.price),
945
- currencyCode: resultCart.currency.code
946
- },
947
- product: line.product ? {
948
- title: line.product.title || line.name,
949
- productType: line.product.productType,
950
- vendor: line.product.vendor
951
- } : void 0
952
- },
953
- quantity: line.quantity
954
- }));
930
+ if (withTrack) {
955
931
  trackAddToCartGA({
956
- lineItems: trackingLineItems,
957
- gtmParams: { ...gtmParams, brand },
958
- brand
932
+ lineItems,
933
+ gtmParams: { ...gtmParams, brand: config.getBrand() }
959
934
  });
960
- trackAddToCartFBQ({ lineItems: trackingLineItems });
935
+ trackAddToCartFBQ({ lineItems });
961
936
  }
962
937
  return resultCart;
963
938
  },
964
- [client, locale, cartCookieAdapter, userAdapter, cart, withTrack, brand]
939
+ [client, locale, cartCookieAdapter, userAdapter, cart, withTrack]
965
940
  );
966
941
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
967
942
  }
@@ -1042,8 +1017,8 @@ function useUpdateCartAttributes(mutate, metafieldIdentifiers, options) {
1042
1017
  );
1043
1018
  return useSWRMutation("update-cart-attributes", updateAttributes, options);
1044
1019
  }
1045
- function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
1046
- const { client, locale, cartCookieAdapter, userAdapter } = useShopify();
1020
+ function useBuyNow({ withTrack = true } = {}, swrOptions) {
1021
+ const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
1047
1022
  const isLoggedIn = userAdapter?.isLoggedIn || false;
1048
1023
  const buyNow = useCallback(
1049
1024
  async (_key, { arg }) => {
@@ -1061,9 +1036,10 @@ function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
1061
1036
  return;
1062
1037
  }
1063
1038
  const lines = lineItems.map((item) => ({
1064
- merchandiseId: item.variant?.id || item.variantId || "",
1039
+ merchandiseId: item.variant?.id || "",
1065
1040
  quantity: item.quantity || 1,
1066
- attributes: item.attributes
1041
+ attributes: item.attributes,
1042
+ sellingPlanId: item.sellingPlanId
1067
1043
  })).filter((item) => item.merchandiseId && item.quantity);
1068
1044
  if (lines.length === 0) {
1069
1045
  return;
@@ -1080,27 +1056,9 @@ function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
1080
1056
  throw new Error("Failed to create cart for buy now");
1081
1057
  }
1082
1058
  if (withTrack && resultCart.lineItems) {
1083
- const trackingLineItems = resultCart.lineItems.map((line) => ({
1084
- variant: {
1085
- id: line.variantId,
1086
- sku: line.variant.sku || "",
1087
- title: line.variant.name,
1088
- price: {
1089
- amount: String(line.variant.price),
1090
- currencyCode: resultCart.currency.code
1091
- },
1092
- product: line.product ? {
1093
- title: line.product.title || line.name,
1094
- productType: line.product.productType,
1095
- vendor: line.product.vendor
1096
- } : void 0
1097
- },
1098
- quantity: line.quantity
1099
- }));
1100
1059
  trackBuyNowGA({
1101
- lineItems: trackingLineItems,
1102
- gtmParams: { ...gtmParams, brand },
1103
- brand
1060
+ lineItems,
1061
+ gtmParams: { ...gtmParams, brand: config.getBrand() }
1104
1062
  });
1105
1063
  if (fbqTrackConfig) {
1106
1064
  trackBuyNowFBQ({ trackConfig: fbqTrackConfig });
@@ -1117,7 +1075,7 @@ function useBuyNow({ withTrack = true, brand } = {}, swrOptions) {
1117
1075
  }
1118
1076
  return resultCart;
1119
1077
  },
1120
- [client, locale, isLoggedIn, cartCookieAdapter, withTrack, brand]
1078
+ [client, locale, isLoggedIn, cartCookieAdapter, withTrack]
1121
1079
  );
1122
1080
  return useSWRMutation("buy-now", buyNow, swrOptions);
1123
1081
  }