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

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.
@@ -1,78 +1,17 @@
1
- import { NormalizedCart, NormalizedProduct, NormalizedProductVariant } from '@anker-in/shopify-sdk';
1
+ import * as swr_mutation from 'swr/mutation';
2
+ import { SWRMutationConfiguration } from 'swr/mutation';
3
+ import { NormalizedProductVariant, NormalizedProduct, MoneyV2, NormalizedCart } from '@anker-in/shopify-sdk';
2
4
 
3
5
  /**
4
6
  * Tracking Types
5
7
  * Type definitions for analytics and tracking
6
8
  */
7
-
8
- interface TrackingLineItem {
9
- variant?: {
10
- id: string;
11
- sku?: string;
12
- title?: string;
13
- name?: string;
14
- price?: {
15
- amount: string;
16
- currencyCode: string;
17
- };
18
- finalPrice?: {
19
- amount: string;
20
- };
21
- product?: {
22
- title?: string;
23
- name?: string;
24
- vendor?: string;
25
- productType?: string;
26
- metafields?: {
27
- global?: {
28
- trafficType?: string;
29
- };
30
- };
31
- };
32
- };
33
- quantity?: number;
34
- finalPrice?: {
35
- amount: string;
36
- };
37
- }
38
9
  interface GtmParams {
39
10
  pageGroup?: string;
40
11
  position?: string;
41
12
  brand?: string;
42
13
  ga4Params?: Record<string, any>;
43
14
  }
44
- interface TrackingOptions {
45
- /** Enable Google Analytics tracking */
46
- enableGA?: boolean;
47
- /** Enable Facebook Pixel tracking */
48
- enableFBQ?: boolean;
49
- /** GTM parameters */
50
- gtmParams?: GtmParams;
51
- /** Brand name for tracking */
52
- brand?: string;
53
- }
54
- interface TrackingAdapter {
55
- /** Track add to cart event */
56
- trackAddToCart?: (params: {
57
- cart: NormalizedCart;
58
- lineItems: TrackingLineItem[];
59
- gtmParams?: GtmParams;
60
- brand?: string;
61
- }) => void;
62
- /** Track remove from cart event */
63
- trackRemoveFromCart?: (params: {
64
- cart: NormalizedCart;
65
- lineItems: TrackingLineItem[];
66
- gtmParams?: GtmParams;
67
- brand?: string;
68
- }) => void;
69
- /** Track begin checkout event */
70
- trackBeginCheckout?: (params: {
71
- cart: NormalizedCart;
72
- gtmParams?: GtmParams;
73
- brand?: string;
74
- }) => void;
75
- }
76
15
 
77
16
  /**
78
17
  * Google Analytics Tracking
@@ -92,29 +31,25 @@ declare const gaTrack: (data: any) => void;
92
31
  /**
93
32
  * Track add to cart event in GA4
94
33
  */
95
- declare const trackAddToCartGA: ({ lineItems, gtmParams, brand, }: {
96
- lineItems: TrackingLineItem[];
34
+ declare const trackAddToCartGA: ({ lineItems, gtmParams, }: {
35
+ lineItems: AddToCartLineItem[];
97
36
  gtmParams?: GtmParams;
98
- brand?: string;
99
37
  }) => void;
100
38
  /**
101
39
  * Track begin checkout event in GA4
102
40
  */
103
- declare const trackBeginCheckoutGA: ({ lineItems, currencyCode, totalPrice, gtmParams, brand, }: {
104
- lineItems: TrackingLineItem[];
41
+ declare const trackBeginCheckoutGA: ({ lineItems, currencyCode, gtmParams, }: {
42
+ lineItems: AddToCartLineItem[];
105
43
  currencyCode?: string;
106
- totalPrice?: number;
107
44
  gtmParams?: GtmParams;
108
- brand?: string;
109
45
  }) => void;
110
46
  /**
111
47
  * Track buy now event in GA4
112
48
  * This triggers begin_checkout event for buy now actions
113
49
  */
114
- declare const trackBuyNowGA: ({ lineItems, gtmParams, brand, }: {
115
- lineItems: TrackingLineItem[];
50
+ declare const trackBuyNowGA: ({ lineItems, gtmParams, }: {
51
+ lineItems: AddToCartLineItem[];
116
52
  gtmParams?: GtmParams;
117
- brand?: string;
118
53
  }) => void;
119
54
 
120
55
  /**
@@ -132,7 +67,7 @@ declare global {
132
67
  * Track add to cart event in Facebook Pixel
133
68
  */
134
69
  declare const trackAddToCartFBQ: ({ lineItems }: {
135
- lineItems: TrackingLineItem[];
70
+ lineItems: AddToCartLineItem[];
136
71
  }) => void;
137
72
  /**
138
73
  * Configuration for Buy Now tracking events
@@ -154,6 +89,80 @@ declare const trackBuyNowFBQ: ({ trackConfig }: {
154
89
  trackConfig?: BuyNowTrackConfig;
155
90
  }) => void;
156
91
 
92
+ type AddToCartLineItem = {
93
+ variant?: NormalizedProductVariant & {
94
+ product?: NormalizedProduct;
95
+ finalPrice?: MoneyV2;
96
+ };
97
+ sellingPlanId?: string;
98
+ quantity?: number;
99
+ attributes?: Array<{
100
+ key: string;
101
+ value: string;
102
+ }>;
103
+ };
104
+ interface AddToCartInput {
105
+ /** Lines to add */
106
+ lineItems: Array<AddToCartLineItem>;
107
+ /** Cart ID (optional) */
108
+ cartId?: string;
109
+ /** Discount codes to apply */
110
+ discountCodes?: string[];
111
+ /** Custom attributes for the cart */
112
+ customAttributes?: Array<{
113
+ key: string;
114
+ value: string;
115
+ }>;
116
+ /** 全量更新 codes,默认是追加 */
117
+ replaceExistingCodes?: boolean;
118
+ /** Buyer identity for cart creation */
119
+ buyerIdentity?: {
120
+ email?: string;
121
+ countryCode?: string;
122
+ };
123
+ /** GTM tracking parameters */
124
+ gtmParams?: Omit<GtmParams, 'brand'>;
125
+ /** Force create new cart */
126
+ needCreateCart?: boolean;
127
+ /** Callback when discount codes are invalid */
128
+ onCodesInvalid?: (updatedCart: NormalizedCart, invalidCodes: string[]) => Promise<NormalizedCart | undefined>;
129
+ }
130
+ interface UseAddToCartOptions {
131
+ /** Enable tracking (GA and FBQ) */
132
+ withTrack?: boolean;
133
+ }
134
+ /**
135
+ * Hook for adding items to cart with tracking support
136
+ *
137
+ * This is the enhanced version that includes:
138
+ * - Automatic cart creation if needed
139
+ * - Discount code validation
140
+ * - Google Analytics tracking
141
+ * - Facebook Pixel tracking
142
+ * - Invalid code handling
143
+ *
144
+ * @param options - Hook configuration
145
+ * @param swrOptions - SWR mutation configuration
146
+ * @returns SWR mutation with trigger function
147
+ *
148
+ * @example
149
+ * ```tsx
150
+ * const { trigger, isMutating } = useAddToCart({ withTrack: true})
151
+ *
152
+ * await trigger({
153
+ * lineItems: [{
154
+ * variantId: 'gid://shopify/ProductVariant/123',
155
+ * quantity: 2
156
+ * }],
157
+ * gtmParams: {
158
+ * pageGroup: 'PDP',
159
+ * position: 'Add to Cart Button'
160
+ * }
161
+ * })
162
+ * ```
163
+ */
164
+ declare function useAddToCart({ withTrack }?: UseAddToCartOptions, swrOptions?: SWRMutationConfiguration<NormalizedCart | undefined, Error, string, AddToCartInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, string, AddToCartInput>;
165
+
157
166
  /**
158
167
  * Plus Member Types
159
168
  * Type definitions for Plus Member functionality
@@ -547,4 +556,4 @@ type DeliveryData = {
547
556
  deliveryCustomData: DeliveryCustomData;
548
557
  };
549
558
 
550
- export { type BuyNowTrackConfig as B, DeliveryPlusType as D, type GtmParams as G, type MailingAddress as M, PLUS_MEMBER_TYPE as P, ShippingMethodMode as S, type TrackingLineItem as T, PlusMemberMode as a, type PlusMemberShippingMethodMetafields as b, type PlusMemberShippingMethodConfig as c, type PlusMemberSettingsMetafields as d, type SelectedPlusMemberProduct as e, type DeliveryOption as f, type DeliveryCustomData as g, type DeliveryData as h, type TrackingOptions as i, type TrackingAdapter as j, gaTrack as k, trackBeginCheckoutGA as l, trackBuyNowGA as m, trackAddToCartFBQ as n, trackBuyNowFBQ as o, trackAddToCartGA as t };
559
+ export { type AddToCartLineItem as A, type BuyNowTrackConfig as B, DeliveryPlusType as D, type GtmParams as G, type MailingAddress as M, PLUS_MEMBER_TYPE as P, ShippingMethodMode as S, type UseAddToCartOptions as U, type AddToCartInput as a, PlusMemberMode as b, type PlusMemberShippingMethodMetafields as c, type PlusMemberShippingMethodConfig as d, type PlusMemberSettingsMetafields as e, type SelectedPlusMemberProduct as f, type DeliveryOption as g, type DeliveryCustomData as h, type DeliveryData as i, gaTrack as j, trackBeginCheckoutGA as k, trackBuyNowGA as l, trackAddToCartFBQ as m, trackBuyNowFBQ as n, trackAddToCartGA as t, useAddToCart as u };
@@ -1,78 +1,17 @@
1
- import { NormalizedCart, NormalizedProduct, NormalizedProductVariant } from '@anker-in/shopify-sdk';
1
+ import * as swr_mutation from 'swr/mutation';
2
+ import { SWRMutationConfiguration } from 'swr/mutation';
3
+ import { NormalizedProductVariant, NormalizedProduct, MoneyV2, NormalizedCart } from '@anker-in/shopify-sdk';
2
4
 
3
5
  /**
4
6
  * Tracking Types
5
7
  * Type definitions for analytics and tracking
6
8
  */
7
-
8
- interface TrackingLineItem {
9
- variant?: {
10
- id: string;
11
- sku?: string;
12
- title?: string;
13
- name?: string;
14
- price?: {
15
- amount: string;
16
- currencyCode: string;
17
- };
18
- finalPrice?: {
19
- amount: string;
20
- };
21
- product?: {
22
- title?: string;
23
- name?: string;
24
- vendor?: string;
25
- productType?: string;
26
- metafields?: {
27
- global?: {
28
- trafficType?: string;
29
- };
30
- };
31
- };
32
- };
33
- quantity?: number;
34
- finalPrice?: {
35
- amount: string;
36
- };
37
- }
38
9
  interface GtmParams {
39
10
  pageGroup?: string;
40
11
  position?: string;
41
12
  brand?: string;
42
13
  ga4Params?: Record<string, any>;
43
14
  }
44
- interface TrackingOptions {
45
- /** Enable Google Analytics tracking */
46
- enableGA?: boolean;
47
- /** Enable Facebook Pixel tracking */
48
- enableFBQ?: boolean;
49
- /** GTM parameters */
50
- gtmParams?: GtmParams;
51
- /** Brand name for tracking */
52
- brand?: string;
53
- }
54
- interface TrackingAdapter {
55
- /** Track add to cart event */
56
- trackAddToCart?: (params: {
57
- cart: NormalizedCart;
58
- lineItems: TrackingLineItem[];
59
- gtmParams?: GtmParams;
60
- brand?: string;
61
- }) => void;
62
- /** Track remove from cart event */
63
- trackRemoveFromCart?: (params: {
64
- cart: NormalizedCart;
65
- lineItems: TrackingLineItem[];
66
- gtmParams?: GtmParams;
67
- brand?: string;
68
- }) => void;
69
- /** Track begin checkout event */
70
- trackBeginCheckout?: (params: {
71
- cart: NormalizedCart;
72
- gtmParams?: GtmParams;
73
- brand?: string;
74
- }) => void;
75
- }
76
15
 
77
16
  /**
78
17
  * Google Analytics Tracking
@@ -92,29 +31,25 @@ declare const gaTrack: (data: any) => void;
92
31
  /**
93
32
  * Track add to cart event in GA4
94
33
  */
95
- declare const trackAddToCartGA: ({ lineItems, gtmParams, brand, }: {
96
- lineItems: TrackingLineItem[];
34
+ declare const trackAddToCartGA: ({ lineItems, gtmParams, }: {
35
+ lineItems: AddToCartLineItem[];
97
36
  gtmParams?: GtmParams;
98
- brand?: string;
99
37
  }) => void;
100
38
  /**
101
39
  * Track begin checkout event in GA4
102
40
  */
103
- declare const trackBeginCheckoutGA: ({ lineItems, currencyCode, totalPrice, gtmParams, brand, }: {
104
- lineItems: TrackingLineItem[];
41
+ declare const trackBeginCheckoutGA: ({ lineItems, currencyCode, gtmParams, }: {
42
+ lineItems: AddToCartLineItem[];
105
43
  currencyCode?: string;
106
- totalPrice?: number;
107
44
  gtmParams?: GtmParams;
108
- brand?: string;
109
45
  }) => void;
110
46
  /**
111
47
  * Track buy now event in GA4
112
48
  * This triggers begin_checkout event for buy now actions
113
49
  */
114
- declare const trackBuyNowGA: ({ lineItems, gtmParams, brand, }: {
115
- lineItems: TrackingLineItem[];
50
+ declare const trackBuyNowGA: ({ lineItems, gtmParams, }: {
51
+ lineItems: AddToCartLineItem[];
116
52
  gtmParams?: GtmParams;
117
- brand?: string;
118
53
  }) => void;
119
54
 
120
55
  /**
@@ -132,7 +67,7 @@ declare global {
132
67
  * Track add to cart event in Facebook Pixel
133
68
  */
134
69
  declare const trackAddToCartFBQ: ({ lineItems }: {
135
- lineItems: TrackingLineItem[];
70
+ lineItems: AddToCartLineItem[];
136
71
  }) => void;
137
72
  /**
138
73
  * Configuration for Buy Now tracking events
@@ -154,6 +89,80 @@ declare const trackBuyNowFBQ: ({ trackConfig }: {
154
89
  trackConfig?: BuyNowTrackConfig;
155
90
  }) => void;
156
91
 
92
+ type AddToCartLineItem = {
93
+ variant?: NormalizedProductVariant & {
94
+ product?: NormalizedProduct;
95
+ finalPrice?: MoneyV2;
96
+ };
97
+ sellingPlanId?: string;
98
+ quantity?: number;
99
+ attributes?: Array<{
100
+ key: string;
101
+ value: string;
102
+ }>;
103
+ };
104
+ interface AddToCartInput {
105
+ /** Lines to add */
106
+ lineItems: Array<AddToCartLineItem>;
107
+ /** Cart ID (optional) */
108
+ cartId?: string;
109
+ /** Discount codes to apply */
110
+ discountCodes?: string[];
111
+ /** Custom attributes for the cart */
112
+ customAttributes?: Array<{
113
+ key: string;
114
+ value: string;
115
+ }>;
116
+ /** 全量更新 codes,默认是追加 */
117
+ replaceExistingCodes?: boolean;
118
+ /** Buyer identity for cart creation */
119
+ buyerIdentity?: {
120
+ email?: string;
121
+ countryCode?: string;
122
+ };
123
+ /** GTM tracking parameters */
124
+ gtmParams?: Omit<GtmParams, 'brand'>;
125
+ /** Force create new cart */
126
+ needCreateCart?: boolean;
127
+ /** Callback when discount codes are invalid */
128
+ onCodesInvalid?: (updatedCart: NormalizedCart, invalidCodes: string[]) => Promise<NormalizedCart | undefined>;
129
+ }
130
+ interface UseAddToCartOptions {
131
+ /** Enable tracking (GA and FBQ) */
132
+ withTrack?: boolean;
133
+ }
134
+ /**
135
+ * Hook for adding items to cart with tracking support
136
+ *
137
+ * This is the enhanced version that includes:
138
+ * - Automatic cart creation if needed
139
+ * - Discount code validation
140
+ * - Google Analytics tracking
141
+ * - Facebook Pixel tracking
142
+ * - Invalid code handling
143
+ *
144
+ * @param options - Hook configuration
145
+ * @param swrOptions - SWR mutation configuration
146
+ * @returns SWR mutation with trigger function
147
+ *
148
+ * @example
149
+ * ```tsx
150
+ * const { trigger, isMutating } = useAddToCart({ withTrack: true})
151
+ *
152
+ * await trigger({
153
+ * lineItems: [{
154
+ * variantId: 'gid://shopify/ProductVariant/123',
155
+ * quantity: 2
156
+ * }],
157
+ * gtmParams: {
158
+ * pageGroup: 'PDP',
159
+ * position: 'Add to Cart Button'
160
+ * }
161
+ * })
162
+ * ```
163
+ */
164
+ declare function useAddToCart({ withTrack }?: UseAddToCartOptions, swrOptions?: SWRMutationConfiguration<NormalizedCart | undefined, Error, string, AddToCartInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, string, AddToCartInput>;
165
+
157
166
  /**
158
167
  * Plus Member Types
159
168
  * Type definitions for Plus Member functionality
@@ -547,4 +556,4 @@ type DeliveryData = {
547
556
  deliveryCustomData: DeliveryCustomData;
548
557
  };
549
558
 
550
- export { type BuyNowTrackConfig as B, DeliveryPlusType as D, type GtmParams as G, type MailingAddress as M, PLUS_MEMBER_TYPE as P, ShippingMethodMode as S, type TrackingLineItem as T, PlusMemberMode as a, type PlusMemberShippingMethodMetafields as b, type PlusMemberShippingMethodConfig as c, type PlusMemberSettingsMetafields as d, type SelectedPlusMemberProduct as e, type DeliveryOption as f, type DeliveryCustomData as g, type DeliveryData as h, type TrackingOptions as i, type TrackingAdapter as j, gaTrack as k, trackBeginCheckoutGA as l, trackBuyNowGA as m, trackAddToCartFBQ as n, trackBuyNowFBQ as o, trackAddToCartGA as t };
559
+ export { type AddToCartLineItem as A, type BuyNowTrackConfig as B, DeliveryPlusType as D, type GtmParams as G, type MailingAddress as M, PLUS_MEMBER_TYPE as P, ShippingMethodMode as S, type UseAddToCartOptions as U, type AddToCartInput as a, PlusMemberMode as b, type PlusMemberShippingMethodMetafields as c, type PlusMemberShippingMethodConfig as d, type PlusMemberSettingsMetafields as e, type SelectedPlusMemberProduct as f, type DeliveryOption as g, type DeliveryCustomData as h, type DeliveryData as i, gaTrack as j, trackBeginCheckoutGA as k, trackBuyNowGA as l, trackAddToCartFBQ as m, trackBuyNowFBQ as n, trackAddToCartGA as t, useAddToCart as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anker-in/shopify-react",
3
- "version": "0.1.1-beta.0",
3
+ "version": "0.1.1-beta.2",
4
4
  "description": "React hooks and components for Shopify SDK",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -31,8 +31,8 @@
31
31
  "decimal.js": "^10.6.0",
32
32
  "js-cookie": "^3.0.5",
33
33
  "swr": "^2.2.0",
34
- "@anker-in/shopify-sdk": "0.1.1-beta.0",
35
- "@anker-in/shopify-core": "0.1.1-beta.0"
34
+ "@anker-in/shopify-core": "0.1.1-beta.0",
35
+ "@anker-in/shopify-sdk": "0.1.1-beta.1"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "react": ">=18.0.0",
@@ -47,8 +47,8 @@
47
47
  "react-dom": "^19.1.0",
48
48
  "tsup": "^8.0.0",
49
49
  "typescript": "5.9.2",
50
- "@repo/eslint-config": "0.0.0",
51
- "@repo/typescript-config": "0.0.0"
50
+ "@repo/typescript-config": "0.0.0",
51
+ "@repo/eslint-config": "0.0.0"
52
52
  },
53
53
  "publishConfig": {
54
54
  "access": "public"