@anker-in/shopify-react 1.1.2 → 1.2.0-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.
@@ -1,4 +1,5 @@
1
- import { NormalizedProductVariant, NormalizedProduct, MoneyV2, NormalizedCart } from '@anker-in/shopify-sdk';
1
+ import { NormalizedProductVariant, NormalizedProduct, MoneyV2, NormalizedCart, NormalizedLineItem } from '@anker-in/shopify-sdk';
2
+ import Decimal from 'decimal.js';
2
3
 
3
4
  /**
4
5
  * Tracking Types
@@ -18,14 +19,20 @@ interface GtmParams {
18
19
 
19
20
  declare global {
20
21
  interface Window {
21
- dataLayer?: any[];
22
- gtag?: (...args: any) => void;
22
+ dataLayer?: Array<Record<string, unknown>>;
23
+ gtag?: {
24
+ (command: 'config', targetId: string, config?: Record<string, unknown>): void;
25
+ (command: 'set', config: Record<string, unknown>): void;
26
+ (command: 'event', eventName: string, eventParams?: Record<string, unknown>): void;
27
+ (command: 'get', targetId: string, fieldName: string, callback?: (value: string) => void): void;
28
+ (command: 'consent', consentArg: string, consentParams?: Record<string, unknown>): void;
29
+ };
23
30
  }
24
31
  }
25
32
  /**
26
33
  * Push event to GA4 dataLayer
27
34
  */
28
- declare const gaTrack: (data: any) => void;
35
+ declare const gaTrack: (data: Record<string, unknown>) => void;
29
36
  /**
30
37
  * Track add to cart event in GA4
31
38
  */
@@ -49,16 +56,41 @@ declare const trackBuyNowGA: ({ lineItems, gtmParams, }: {
49
56
  lineItems: AddToCartLineItem[];
50
57
  gtmParams?: GtmParams;
51
58
  }) => void;
59
+ /**
60
+ * GA4 Data
61
+ */
62
+ interface GA4Data {
63
+ clientId: string;
64
+ sessionId: string;
65
+ }
66
+ /**
67
+ * Wait for gtag to be ready
68
+ */
69
+ declare function waitForGtagReady(timeout?: number): Promise<void>;
70
+ /**
71
+ * Get GA4 client ID
72
+ */
73
+ declare const getGA4ClientId: (measurementId?: string) => Promise<string>;
74
+ /**
75
+ * Get GA4 session ID
76
+ */
77
+ declare const getGA4SessionId: (measurementId?: string) => Promise<string>;
78
+ /**
79
+ * Get GA4 data (client ID and session ID)
80
+ */
81
+ declare const getGA4Data: (measurementId?: string) => Promise<GA4Data>;
52
82
 
53
83
  /**
54
84
  * Facebook Pixel Tracking
55
85
  * FBQ event tracking utilities
56
86
  */
57
87
 
88
+ type FbqCommand = 'track' | 'trackCustom' | 'trackSingle' | 'trackCustom';
89
+ type FbqEventName = 'AddToCart' | 'Purchase' | 'InitiateCheckout' | 'ViewContent' | string;
90
+ type FbqEventParams = Record<string, unknown>;
58
91
  declare global {
59
92
  interface Window {
60
- fbq?: (...args: any[]) => void;
61
- gtag?: (...args: any[]) => void;
93
+ fbq?: (command: FbqCommand, eventName: FbqEventName, params?: FbqEventParams) => void;
62
94
  }
63
95
  }
64
96
  /**
@@ -102,6 +134,9 @@ type AddToCartLineItem = {
102
134
  code: string;
103
135
  amount: number;
104
136
  };
137
+ gtmParams?: {
138
+ item_category_id?: number | string;
139
+ };
105
140
  };
106
141
  interface AddToCartInput {
107
142
  /** Lines to add */
@@ -134,6 +169,131 @@ interface UseAddToCartOptions {
134
169
  withTrack?: boolean;
135
170
  }
136
171
 
172
+ /**
173
+ * Auto Remove Free Gifts Hook
174
+ *
175
+ * Automatically removes Function and Script free gifts from cart on page load.
176
+ * This ensures gifts are only added at checkout time, not kept in cart permanently.
177
+ *
178
+ * Based on storefront-anker production implementation.
179
+ */
180
+
181
+ interface UseAutoRemoveFreeGiftsOptions {
182
+ /**
183
+ * Whether to remove Function gifts (with _discounts_function_env attribute)
184
+ * @default true
185
+ */
186
+ removeFunctionGifts?: boolean;
187
+ /**
188
+ * Whether to remove Script gifts (with _giveaway_gradient_gifts attribute)
189
+ * @default true
190
+ */
191
+ removeScriptGifts?: boolean;
192
+ /**
193
+ * Custom function to identify additional gift line items
194
+ * @param line - Line item to check
195
+ * @returns true if line item is a gift that should be removed
196
+ */
197
+ isGiftLineItem?: (line: NormalizedLineItem) => boolean;
198
+ }
199
+ interface UseAutoRemoveFreeGiftsResult {
200
+ /** Whether removal is in progress */
201
+ isRemoving: boolean;
202
+ }
203
+ /**
204
+ * Automatically removes free gifts from cart on initialization
205
+ *
206
+ * Function满赠和Script满赠的赠品都会被添加到购物车,需要在返回购物车页面时自动删除。
207
+ * - Function 赠品:带 _discounts_function_env 属性
208
+ * - Script 赠品:带 _giveaway_gradient_gifts 属性
209
+ *
210
+ * @example
211
+ * ```tsx
212
+ * function CartPage() {
213
+ * // Auto-remove all gifts on page load
214
+ * useAutoRemoveFreeGifts()
215
+ *
216
+ * return <Cart />
217
+ * }
218
+ * ```
219
+ *
220
+ * @example
221
+ * ```tsx
222
+ * // Only remove Function gifts, keep Script gifts
223
+ * useAutoRemoveFreeGifts({
224
+ * removeFunctionGifts: true,
225
+ * removeScriptGifts: false
226
+ * })
227
+ * ```
228
+ *
229
+ * @example
230
+ * ```tsx
231
+ * // Custom gift identification
232
+ * useAutoRemoveFreeGifts({
233
+ * isGiftLineItem: (line) => {
234
+ * // Also remove other campaign gifts
235
+ * return line.customAttributes?.some(
236
+ * attr => attr.key === '_giveaway_summer_campaign'
237
+ * )
238
+ * }
239
+ * })
240
+ * ```
241
+ */
242
+ declare function useAutoRemoveFreeGifts(options?: UseAutoRemoveFreeGiftsOptions): UseAutoRemoveFreeGiftsResult;
243
+ /**
244
+ * Helper function to check if a line item is a Function gift
245
+ */
246
+ declare function isFunctionGift(line: NormalizedLineItem): boolean;
247
+ /**
248
+ * Helper function to check if a line item is a Script gift
249
+ */
250
+ declare function isScriptGift(line: NormalizedLineItem): boolean;
251
+ /**
252
+ * Helper function to check if a line item is a buy-get gift (买赠)
253
+ */
254
+ declare function isBuyGetGift(line: NormalizedLineItem): boolean;
255
+ /**
256
+ * Helper function to check if a line item is any type of gift
257
+ */
258
+ declare function isAnyGift(line: NormalizedLineItem): boolean;
259
+
260
+ interface GiveawayProduct {
261
+ handle: string;
262
+ sku: string;
263
+ }
264
+ interface Breakpoint {
265
+ breakpoint: string;
266
+ giveawayProducts: GiveawayProduct[];
267
+ }
268
+ interface ScriptAutoFreeGiftConfig {
269
+ activityAvailableQuery?: string;
270
+ activityQroperty?: string;
271
+ breakpoints?: Array<{
272
+ breakpoint: string;
273
+ giveawayProducts: GiveawayProduct[];
274
+ }>;
275
+ includeTags?: string[];
276
+ useTotalAmount?: boolean;
277
+ requireLogin?: boolean;
278
+ }
279
+ interface UseScriptAutoFreeGiftResult {
280
+ involvedLines: NormalizedLineItem[];
281
+ reorder: (a: NormalizedLineItem, b: NormalizedLineItem) => number;
282
+ disableCodeRemove: boolean;
283
+ nextFreeGiftLevel: Breakpoint | null;
284
+ freeGiftLevel: Breakpoint | null;
285
+ involvedSubTotal: Decimal;
286
+ giftProductsResult?: NormalizedProduct[];
287
+ }
288
+ declare const useScriptAutoFreeGift: ({ campaign, _giveaway, cart, locale: providedLocale, lines, profile, }: {
289
+ campaign?: ScriptAutoFreeGiftConfig;
290
+ _giveaway: string;
291
+ cart: NormalizedCart | undefined;
292
+ locale?: string;
293
+ lines?: AddToCartLineItem[];
294
+ profile?: any;
295
+ }) => UseScriptAutoFreeGiftResult;
296
+
137
297
  /**
138
298
  * Plus Member Types
139
299
  * Type definitions for Plus Member functionality
@@ -510,4 +670,4 @@ type DeliveryCustomData = {
510
670
  plus_type?: DeliveryPlusType;
511
671
  };
512
672
 
513
- export { type AddToCartLineItem as A, type BuyNowTrackConfig as B, DeliveryPlusType as D, type GtmParams as G, 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 SelectedPlusMemberVariant as f, type DeliveryCustomData as g, gaTrack as h, trackBeginCheckoutGA as i, trackBuyNowGA as j, trackAddToCartFBQ as k, trackBuyNowFBQ as l, trackAddToCartGA as t };
673
+ export { type AddToCartLineItem as A, trackAddToCartFBQ as B, type BuyNowTrackConfig as C, DeliveryPlusType as D, trackBuyNowFBQ as E, type GtmParams as G, PLUS_MEMBER_TYPE as P, type ScriptAutoFreeGiftConfig as S, type UseAutoRemoveFreeGiftsOptions as U, type UseAutoRemoveFreeGiftsResult as a, isScriptGift as b, isBuyGetGift as c, isAnyGift as d, type UseScriptAutoFreeGiftResult as e, useScriptAutoFreeGift as f, type AddToCartInput as g, type UseAddToCartOptions as h, isFunctionGift as i, PlusMemberMode as j, ShippingMethodMode as k, type PlusMemberShippingMethodMetafields as l, type PlusMemberShippingMethodConfig as m, type PlusMemberSettingsMetafields as n, type SelectedPlusMemberVariant as o, type DeliveryCustomData as p, gaTrack as q, trackBeginCheckoutGA as r, trackBuyNowGA as s, trackAddToCartGA as t, useAutoRemoveFreeGifts as u, type GA4Data as v, waitForGtagReady as w, getGA4ClientId as x, getGA4SessionId as y, getGA4Data as z };
@@ -1,4 +1,5 @@
1
- import { NormalizedProductVariant, NormalizedProduct, MoneyV2, NormalizedCart } from '@anker-in/shopify-sdk';
1
+ import { NormalizedProductVariant, NormalizedProduct, MoneyV2, NormalizedCart, NormalizedLineItem } from '@anker-in/shopify-sdk';
2
+ import Decimal from 'decimal.js';
2
3
 
3
4
  /**
4
5
  * Tracking Types
@@ -18,14 +19,20 @@ interface GtmParams {
18
19
 
19
20
  declare global {
20
21
  interface Window {
21
- dataLayer?: any[];
22
- gtag?: (...args: any) => void;
22
+ dataLayer?: Array<Record<string, unknown>>;
23
+ gtag?: {
24
+ (command: 'config', targetId: string, config?: Record<string, unknown>): void;
25
+ (command: 'set', config: Record<string, unknown>): void;
26
+ (command: 'event', eventName: string, eventParams?: Record<string, unknown>): void;
27
+ (command: 'get', targetId: string, fieldName: string, callback?: (value: string) => void): void;
28
+ (command: 'consent', consentArg: string, consentParams?: Record<string, unknown>): void;
29
+ };
23
30
  }
24
31
  }
25
32
  /**
26
33
  * Push event to GA4 dataLayer
27
34
  */
28
- declare const gaTrack: (data: any) => void;
35
+ declare const gaTrack: (data: Record<string, unknown>) => void;
29
36
  /**
30
37
  * Track add to cart event in GA4
31
38
  */
@@ -49,16 +56,41 @@ declare const trackBuyNowGA: ({ lineItems, gtmParams, }: {
49
56
  lineItems: AddToCartLineItem[];
50
57
  gtmParams?: GtmParams;
51
58
  }) => void;
59
+ /**
60
+ * GA4 Data
61
+ */
62
+ interface GA4Data {
63
+ clientId: string;
64
+ sessionId: string;
65
+ }
66
+ /**
67
+ * Wait for gtag to be ready
68
+ */
69
+ declare function waitForGtagReady(timeout?: number): Promise<void>;
70
+ /**
71
+ * Get GA4 client ID
72
+ */
73
+ declare const getGA4ClientId: (measurementId?: string) => Promise<string>;
74
+ /**
75
+ * Get GA4 session ID
76
+ */
77
+ declare const getGA4SessionId: (measurementId?: string) => Promise<string>;
78
+ /**
79
+ * Get GA4 data (client ID and session ID)
80
+ */
81
+ declare const getGA4Data: (measurementId?: string) => Promise<GA4Data>;
52
82
 
53
83
  /**
54
84
  * Facebook Pixel Tracking
55
85
  * FBQ event tracking utilities
56
86
  */
57
87
 
88
+ type FbqCommand = 'track' | 'trackCustom' | 'trackSingle' | 'trackCustom';
89
+ type FbqEventName = 'AddToCart' | 'Purchase' | 'InitiateCheckout' | 'ViewContent' | string;
90
+ type FbqEventParams = Record<string, unknown>;
58
91
  declare global {
59
92
  interface Window {
60
- fbq?: (...args: any[]) => void;
61
- gtag?: (...args: any[]) => void;
93
+ fbq?: (command: FbqCommand, eventName: FbqEventName, params?: FbqEventParams) => void;
62
94
  }
63
95
  }
64
96
  /**
@@ -102,6 +134,9 @@ type AddToCartLineItem = {
102
134
  code: string;
103
135
  amount: number;
104
136
  };
137
+ gtmParams?: {
138
+ item_category_id?: number | string;
139
+ };
105
140
  };
106
141
  interface AddToCartInput {
107
142
  /** Lines to add */
@@ -134,6 +169,131 @@ interface UseAddToCartOptions {
134
169
  withTrack?: boolean;
135
170
  }
136
171
 
172
+ /**
173
+ * Auto Remove Free Gifts Hook
174
+ *
175
+ * Automatically removes Function and Script free gifts from cart on page load.
176
+ * This ensures gifts are only added at checkout time, not kept in cart permanently.
177
+ *
178
+ * Based on storefront-anker production implementation.
179
+ */
180
+
181
+ interface UseAutoRemoveFreeGiftsOptions {
182
+ /**
183
+ * Whether to remove Function gifts (with _discounts_function_env attribute)
184
+ * @default true
185
+ */
186
+ removeFunctionGifts?: boolean;
187
+ /**
188
+ * Whether to remove Script gifts (with _giveaway_gradient_gifts attribute)
189
+ * @default true
190
+ */
191
+ removeScriptGifts?: boolean;
192
+ /**
193
+ * Custom function to identify additional gift line items
194
+ * @param line - Line item to check
195
+ * @returns true if line item is a gift that should be removed
196
+ */
197
+ isGiftLineItem?: (line: NormalizedLineItem) => boolean;
198
+ }
199
+ interface UseAutoRemoveFreeGiftsResult {
200
+ /** Whether removal is in progress */
201
+ isRemoving: boolean;
202
+ }
203
+ /**
204
+ * Automatically removes free gifts from cart on initialization
205
+ *
206
+ * Function满赠和Script满赠的赠品都会被添加到购物车,需要在返回购物车页面时自动删除。
207
+ * - Function 赠品:带 _discounts_function_env 属性
208
+ * - Script 赠品:带 _giveaway_gradient_gifts 属性
209
+ *
210
+ * @example
211
+ * ```tsx
212
+ * function CartPage() {
213
+ * // Auto-remove all gifts on page load
214
+ * useAutoRemoveFreeGifts()
215
+ *
216
+ * return <Cart />
217
+ * }
218
+ * ```
219
+ *
220
+ * @example
221
+ * ```tsx
222
+ * // Only remove Function gifts, keep Script gifts
223
+ * useAutoRemoveFreeGifts({
224
+ * removeFunctionGifts: true,
225
+ * removeScriptGifts: false
226
+ * })
227
+ * ```
228
+ *
229
+ * @example
230
+ * ```tsx
231
+ * // Custom gift identification
232
+ * useAutoRemoveFreeGifts({
233
+ * isGiftLineItem: (line) => {
234
+ * // Also remove other campaign gifts
235
+ * return line.customAttributes?.some(
236
+ * attr => attr.key === '_giveaway_summer_campaign'
237
+ * )
238
+ * }
239
+ * })
240
+ * ```
241
+ */
242
+ declare function useAutoRemoveFreeGifts(options?: UseAutoRemoveFreeGiftsOptions): UseAutoRemoveFreeGiftsResult;
243
+ /**
244
+ * Helper function to check if a line item is a Function gift
245
+ */
246
+ declare function isFunctionGift(line: NormalizedLineItem): boolean;
247
+ /**
248
+ * Helper function to check if a line item is a Script gift
249
+ */
250
+ declare function isScriptGift(line: NormalizedLineItem): boolean;
251
+ /**
252
+ * Helper function to check if a line item is a buy-get gift (买赠)
253
+ */
254
+ declare function isBuyGetGift(line: NormalizedLineItem): boolean;
255
+ /**
256
+ * Helper function to check if a line item is any type of gift
257
+ */
258
+ declare function isAnyGift(line: NormalizedLineItem): boolean;
259
+
260
+ interface GiveawayProduct {
261
+ handle: string;
262
+ sku: string;
263
+ }
264
+ interface Breakpoint {
265
+ breakpoint: string;
266
+ giveawayProducts: GiveawayProduct[];
267
+ }
268
+ interface ScriptAutoFreeGiftConfig {
269
+ activityAvailableQuery?: string;
270
+ activityQroperty?: string;
271
+ breakpoints?: Array<{
272
+ breakpoint: string;
273
+ giveawayProducts: GiveawayProduct[];
274
+ }>;
275
+ includeTags?: string[];
276
+ useTotalAmount?: boolean;
277
+ requireLogin?: boolean;
278
+ }
279
+ interface UseScriptAutoFreeGiftResult {
280
+ involvedLines: NormalizedLineItem[];
281
+ reorder: (a: NormalizedLineItem, b: NormalizedLineItem) => number;
282
+ disableCodeRemove: boolean;
283
+ nextFreeGiftLevel: Breakpoint | null;
284
+ freeGiftLevel: Breakpoint | null;
285
+ involvedSubTotal: Decimal;
286
+ giftProductsResult?: NormalizedProduct[];
287
+ }
288
+ declare const useScriptAutoFreeGift: ({ campaign, _giveaway, cart, locale: providedLocale, lines, profile, }: {
289
+ campaign?: ScriptAutoFreeGiftConfig;
290
+ _giveaway: string;
291
+ cart: NormalizedCart | undefined;
292
+ locale?: string;
293
+ lines?: AddToCartLineItem[];
294
+ profile?: any;
295
+ }) => UseScriptAutoFreeGiftResult;
296
+
137
297
  /**
138
298
  * Plus Member Types
139
299
  * Type definitions for Plus Member functionality
@@ -510,4 +670,4 @@ type DeliveryCustomData = {
510
670
  plus_type?: DeliveryPlusType;
511
671
  };
512
672
 
513
- export { type AddToCartLineItem as A, type BuyNowTrackConfig as B, DeliveryPlusType as D, type GtmParams as G, 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 SelectedPlusMemberVariant as f, type DeliveryCustomData as g, gaTrack as h, trackBeginCheckoutGA as i, trackBuyNowGA as j, trackAddToCartFBQ as k, trackBuyNowFBQ as l, trackAddToCartGA as t };
673
+ export { type AddToCartLineItem as A, trackAddToCartFBQ as B, type BuyNowTrackConfig as C, DeliveryPlusType as D, trackBuyNowFBQ as E, type GtmParams as G, PLUS_MEMBER_TYPE as P, type ScriptAutoFreeGiftConfig as S, type UseAutoRemoveFreeGiftsOptions as U, type UseAutoRemoveFreeGiftsResult as a, isScriptGift as b, isBuyGetGift as c, isAnyGift as d, type UseScriptAutoFreeGiftResult as e, useScriptAutoFreeGift as f, type AddToCartInput as g, type UseAddToCartOptions as h, isFunctionGift as i, PlusMemberMode as j, ShippingMethodMode as k, type PlusMemberShippingMethodMetafields as l, type PlusMemberShippingMethodConfig as m, type PlusMemberSettingsMetafields as n, type SelectedPlusMemberVariant as o, type DeliveryCustomData as p, gaTrack as q, trackBeginCheckoutGA as r, trackBuyNowGA as s, trackAddToCartGA as t, useAutoRemoveFreeGifts as u, type GA4Data as v, waitForGtagReady as w, getGA4ClientId as x, getGA4SessionId as y, getGA4Data as z };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anker-in/shopify-react",
3
- "version": "1.1.2",
3
+ "version": "1.2.0-beta.01",
4
4
  "description": "React hooks and components for Shopify SDK",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -32,7 +32,7 @@
32
32
  "js-cookie": "^3.0.5",
33
33
  "swr": "^2.2.0",
34
34
  "@anker-in/shopify-core": "1.0.1",
35
- "@anker-in/shopify-sdk": "1.1.0"
35
+ "@anker-in/shopify-sdk": "1.1.0-beta.01"
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/typescript-config": "0.0.1",
51
- "@repo/eslint-config": "0.0.1"
50
+ "@repo/eslint-config": "0.0.1",
51
+ "@repo/typescript-config": "0.0.1"
52
52
  },
53
53
  "publishConfig": {
54
54
  "access": "public"