shopkit-analytics 1.0.0 → 1.0.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.
Files changed (41) hide show
  1. package/README.md +258 -3
  2. package/dist/adapters/index.d.mts +2 -2
  3. package/dist/adapters/index.d.ts +2 -2
  4. package/dist/adapters/index.js +1039 -600
  5. package/dist/adapters/index.js.map +1 -1
  6. package/dist/adapters/index.mjs +2 -2
  7. package/dist/{chunk-3TQR5DOP.mjs → chunk-3NR2AKE4.mjs} +1 -31
  8. package/dist/chunk-3NR2AKE4.mjs.map +1 -0
  9. package/dist/{chunk-P4OJDCEZ.mjs → chunk-BNV3EVHH.mjs} +3 -3
  10. package/dist/{chunk-JVEGG6JV.mjs → chunk-HCA4E2RA.mjs} +19 -13
  11. package/dist/chunk-HCA4E2RA.mjs.map +1 -0
  12. package/dist/{chunk-4MZH5OLR.mjs → chunk-U3UOXFS4.mjs} +1040 -603
  13. package/dist/chunk-U3UOXFS4.mjs.map +1 -0
  14. package/dist/events/index.d.mts +9 -41
  15. package/dist/events/index.d.ts +9 -41
  16. package/dist/events/index.js +801 -490
  17. package/dist/events/index.js.map +1 -1
  18. package/dist/events/index.mjs +5 -7
  19. package/dist/{index-GODWc1s6.d.mts → index-Bym1_EAp.d.mts} +74 -17
  20. package/dist/{index-BnNRgdUv.d.ts → index-DS9OI5Mz.d.ts} +74 -17
  21. package/dist/index.d.mts +6 -6
  22. package/dist/index.d.ts +6 -6
  23. package/dist/index.js +1057 -645
  24. package/dist/index.js.map +1 -1
  25. package/dist/index.mjs +4 -6
  26. package/dist/index.mjs.map +1 -1
  27. package/dist/{subscriber-43gnCKWe.d.ts → subscriber-BDAm_BAi.d.ts} +38 -2
  28. package/dist/{subscriber-sWesj_5p.d.mts → subscriber-BoyOlh9t.d.mts} +38 -2
  29. package/dist/subscriber-VF3IYUCU.mjs +8 -0
  30. package/dist/types.d.mts +4 -340
  31. package/dist/types.d.ts +4 -340
  32. package/dist/types.js +0 -30
  33. package/dist/types.js.map +1 -1
  34. package/dist/types.mjs +1 -1
  35. package/package.json +2 -3
  36. package/dist/chunk-3TQR5DOP.mjs.map +0 -1
  37. package/dist/chunk-4MZH5OLR.mjs.map +0 -1
  38. package/dist/chunk-JVEGG6JV.mjs.map +0 -1
  39. package/dist/subscriber-IFZJU57V.mjs +0 -8
  40. /package/dist/{chunk-P4OJDCEZ.mjs.map → chunk-BNV3EVHH.mjs.map} +0 -0
  41. /package/dist/{subscriber-IFZJU57V.mjs.map → subscriber-VF3IYUCU.mjs.map} +0 -0
package/README.md CHANGED
@@ -7,6 +7,8 @@ A comprehensive analytics package for e-commerce applications with support for m
7
7
  - **Multiple Analytics Platforms**: Support for 7+ analytics adapters
8
8
  - **Event-Driven Architecture**: Publisher-subscriber pattern for flexible event handling
9
9
  - **TypeScript Support**: Full type safety and IntelliSense
10
+ - **Adapter-Specific Parameters**: Customize event names and parameters per adapter
11
+ - **Server-Side Tracking**: Facebook CAPI integration with event deduplication
10
12
  - **Affiliate Tracking**: Built-in UTM parameter and attribution tracking
11
13
  - **React Integration**: Easy-to-use React components
12
14
  - **Selective Adapter Routing**: Route events to specific adapters
@@ -77,6 +79,95 @@ publishEvent({
77
79
  });
78
80
  ```
79
81
 
82
+ ## 🎯 Adapter-Specific Parameters
83
+
84
+ All 7 adapters now support adapter-specific parameter customization, allowing you to override event names and add custom parameters per adapter while maintaining a single event call.
85
+
86
+ ### Basic Usage
87
+
88
+ ```tsx
89
+ import { publishEvent, EventType } from "@shopkit/analytics";
90
+
91
+ // Standard event (works with all adapters)
92
+ publishEvent({
93
+ type: EventType.ADD_TO_CART,
94
+ productId: "123",
95
+ productName: "Cool Product",
96
+ price: 29.99,
97
+ });
98
+
99
+ // Advanced usage with adapter-specific parameters
100
+ publishEvent({
101
+ type: EventType.ADD_TO_CART,
102
+ productId: "123",
103
+ productName: "Cool Product",
104
+ price: 29.99,
105
+ }, {
106
+ // Customize for each adapter
107
+ GoogleAnalytics: {
108
+ event_name: "custom_add_to_cart",
109
+ custom_dimension_1: "premium_user",
110
+ campaign_id: "summer_campaign"
111
+ },
112
+ FacebookPixel: {
113
+ event_name: "CustomAddToCart",
114
+ custom_parameter: "special_value",
115
+ pixel_specific_data: { campaign: "summer2024" }
116
+ },
117
+ PostHog: {
118
+ event_name: "add_to_cart_premium",
119
+ feature_flag: "new_checkout_flow"
120
+ },
121
+ MoEngage: {
122
+ event_name: "cart_item_added",
123
+ user_segment: "premium_customers"
124
+ },
125
+ KwikCheckout: {
126
+ event_name: "gokwik_add_to_cart",
127
+ checkout_flow: "express"
128
+ },
129
+ KwikPass: {
130
+ event_name: "kwikpass_cart_add",
131
+ pass_type: "premium"
132
+ },
133
+ ShopifyAnalytics: {
134
+ event_name: "shopify_add_to_cart",
135
+ pageType: "product",
136
+ custom_shopify_param: "value"
137
+ }
138
+ });
139
+ ```
140
+
141
+ ### TypeScript Support
142
+
143
+ Full TypeScript support with IntelliSense for all adapter names:
144
+
145
+ ```tsx
146
+ import type { IAdapterParams } from "@shopkit/analytics";
147
+
148
+ const customParams: IAdapterParams = {
149
+ GoogleAnalytics: {
150
+ event_name: "custom_event",
151
+ // Full type safety for parameters
152
+ },
153
+ FacebookPixel: {
154
+ event_name: "CustomEvent",
155
+ // Adapter-specific parameters
156
+ },
157
+ // ... other adapters
158
+ };
159
+
160
+ publishEvent(eventData, customParams);
161
+ ```
162
+
163
+ ### Key Benefits
164
+
165
+ - **🎯 Flexibility**: Customize event names and parameters per adapter
166
+ - **🔄 Backward Compatibility**: Optional parameters maintain existing API
167
+ - **🛡️ Type Safety**: Full TypeScript support with proper interfaces
168
+ - **⚡ Performance**: No overhead when not using custom parameters
169
+ - **🧹 Clean API**: Single event call handles all adapters
170
+
80
171
  ## 🔧 Analytics Adapters
81
172
 
82
173
  ### 1. Google Analytics 4
@@ -105,13 +196,14 @@ Track events to Google Analytics 4 with enhanced e-commerce support.
105
196
 
106
197
  ### 2. Facebook Pixel
107
198
 
108
- Track conversions and optimize Facebook ad campaigns.
199
+ Track conversions and optimize Facebook ad campaigns with both client-side and server-side tracking support.
109
200
 
110
201
  ```tsx
111
202
  <ShopkitAnalytics
112
203
  config={{
113
204
  facebookPixel: {
114
205
  pixelId: "123456789",
206
+ enableCAPI: true, // Enable server-side CAPI backup
115
207
  enableDebugLogs: true,
116
208
  enableAdvancedMatching: true,
117
209
  },
@@ -124,6 +216,13 @@ Track conversions and optimize Facebook ad campaigns.
124
216
  - InitiateCheckout, Search
125
217
  - Custom conversions
126
218
 
219
+ **Server-Side Tracking (CAPI):**
220
+ - Automatic server-side backup for improved data reliability
221
+ - Event deduplication using unique event IDs
222
+ - Enhanced browser fingerprinting for better user matching
223
+ - Fallback mechanism when client-side tracking fails
224
+ - Requires `/api/events` endpoint for server-side processing
225
+
127
226
  ### 3. MoEngage
128
227
 
129
228
  Engage users with personalized campaigns and analytics.
@@ -196,7 +295,7 @@ NEXT_PUBLIC_POSTHOG_KEY="phc_xxxxxxxxxx"
196
295
 
197
296
  ### 5. Shopify Analytics
198
297
 
199
- Native Shopify Admin Analytics integration.
298
+ Native Shopify Admin Analytics integration with full adapter parameter support.
200
299
 
201
300
  ```tsx
202
301
  <ShopkitAnalytics
@@ -222,6 +321,23 @@ NEXT_PUBLIC_SHOPIFY_CURRENCY="USD"
222
321
  - Add to cart, checkout events
223
322
  - Session tracking with Shopify cookies
224
323
 
324
+ **Adapter Parameter Support:**
325
+ ```tsx
326
+ publishEvent({
327
+ type: EventType.ADD_TO_CART,
328
+ productId: "123",
329
+ productName: "Product",
330
+ price: 29.99,
331
+ }, {
332
+ ShopifyAnalytics: {
333
+ event_name: "custom_shopify_event",
334
+ pageType: "product",
335
+ collectionHandle: "featured-products",
336
+ custom_shopify_param: "value"
337
+ }
338
+ });
339
+ ```
340
+
225
341
  ### 6. KwikPass Analytics
226
342
 
227
343
  Custom analytics adapter for KwikPass platform integration.
@@ -285,7 +401,146 @@ NEXT_PUBLIC_STORE_ID="your-store-id"
285
401
  - `ORDER_SUCCESS` - Order success confirmation
286
402
  - `ORDER_COMPLETED` - Order fulfillment
287
403
 
288
- ## 📝 Professional Logging System
404
+ ## 🔄 Server-Side Tracking (CAPI)
405
+
406
+ The Facebook Pixel adapter supports server-side tracking through the Conversions API (CAPI) for improved data reliability and privacy compliance.
407
+
408
+ ### Features
409
+
410
+ - **Automatic Fallback**: Server-side tracking runs as a backup to client-side tracking
411
+ - **Event Deduplication**: Unique event IDs prevent duplicate tracking across client/server
412
+ - **Enhanced Matching**: Browser fingerprinting for better user matching
413
+ - **Privacy Compliant**: Respects user privacy while improving data quality
414
+ - **Built-in CAPI Service**: Uses the integrated `FacebookCAPIService` for server-side processing
415
+
416
+ ### Environment Variables
417
+
418
+ Set up the required environment variables for CAPI:
419
+
420
+ ```bash
421
+ # Facebook CAPI Configuration
422
+ FACEBOOK_CAPI_ACCESS_TOKEN="your_facebook_access_token"
423
+ NEXT_PUBLIC_PIXEL_ID="123456789"
424
+ FACEBOOK_TEST_EVENT_CODE="TEST12345" # Optional: for testing
425
+ ```
426
+
427
+ ### Server-Side Endpoint Implementation
428
+
429
+ Create an `/api/events` endpoint to handle server-side tracking:
430
+
431
+ ```tsx
432
+ // app/api/events/route.ts
433
+ import { createFacebookCAPIService } from "@shopkit/analytics/services/facebook-capi";
434
+ import { TEvent } from "@shopkit/analytics/types";
435
+
436
+ /**
437
+ * Get client IP address from request headers
438
+ */
439
+ function getClientIpAddress(request: Request): string | undefined {
440
+ // Try different headers for IP address
441
+ const forwarded = request.headers.get("x-forwarded-for");
442
+ const realIp = request.headers.get("x-real-ip");
443
+ const cfConnectingIp = request.headers.get("cf-connecting-ip");
444
+
445
+ return forwarded?.split(",")[0] || realIp || cfConnectingIp || undefined;
446
+ }
447
+
448
+ export async function POST(request: Request) {
449
+ try {
450
+ const { events, userInfo } = await request.json();
451
+
452
+ // Validate events array
453
+ if (!Array.isArray(events) || events.length === 0) {
454
+ return Response.json({ error: "Invalid events data" }, { status: 400 });
455
+ }
456
+
457
+ // Create CAPI service with explicit configuration
458
+ const capiService = createFacebookCAPIService({
459
+ accessToken: process.env.FACEBOOK_CAPI_ACCESS_TOKEN!,
460
+ pixelId: process.env.NEXT_PUBLIC_PIXEL_ID!,
461
+ testEventCode: process.env.FACEBOOK_TEST_EVENT_CODE,
462
+ });
463
+
464
+ if (!capiService) {
465
+ console.warn("Facebook CAPI service not available - missing configuration");
466
+ return Response.json(
467
+ { error: "CAPI service not configured" },
468
+ { status: 500 }
469
+ );
470
+ }
471
+
472
+ // Enhance user info with server-side data
473
+ const enhancedUserInfo = {
474
+ ...userInfo,
475
+ clientIpAddress: getClientIpAddress(request),
476
+ };
477
+
478
+ // Send events to Facebook CAPI
479
+ await capiService.sendEvents(events as TEvent[], enhancedUserInfo);
480
+
481
+ return Response.json({
482
+ success: true,
483
+ message: `Successfully sent ${events.length} events to Facebook CAPI`,
484
+ });
485
+ } catch (error) {
486
+ console.error("API Events Error:", error);
487
+ return Response.json(
488
+ { error: "Failed to process events" },
489
+ { status: 500 }
490
+ );
491
+ }
492
+ }
493
+ ```
494
+
495
+ ### Supported Events
496
+
497
+ The CAPI service currently supports these events (matching the PixelAdapter implementation):
498
+
499
+ - `PAGE_VIEW` → Facebook `PageView`
500
+ - `PRODUCT_VIEW` → Facebook `ViewContent`
501
+ - `ADD_TO_CART` → Facebook `AddToCart`
502
+ - `SEARCH` → Facebook `Search`
503
+
504
+ ### Event ID Generation
505
+
506
+ Events automatically receive unique IDs for deduplication:
507
+
508
+ ```tsx
509
+ // Automatic ID generation
510
+ publishEvent({
511
+ type: EventType.ADD_TO_CART,
512
+ productId: "123",
513
+ // eventId is automatically generated if not provided
514
+ });
515
+
516
+ // Manual ID specification
517
+ publishEvent({
518
+ type: EventType.ADD_TO_CART,
519
+ productId: "123",
520
+ eventId: "custom_event_id_123", // Custom event ID
521
+ });
522
+ ```
523
+
524
+ ### Browser Information Collection
525
+
526
+ The system automatically collects browser information for enhanced server-side matching:
527
+
528
+ - Client user agent
529
+ - Facebook click ID (fbc) from URL parameters or cookies
530
+ - Facebook browser ID (fbp) from cookies
531
+ - Client IP address (server-side)
532
+
533
+ ### Testing
534
+
535
+ Use the test event code for development:
536
+
537
+ ```bash
538
+ FACEBOOK_TEST_EVENT_CODE="TEST12345"
539
+ ```
540
+
541
+ This allows you to test CAPI events without affecting production data.
542
+
543
+ ## Professional Logging System
289
544
 
290
545
  The analytics package includes a comprehensive logging system built on [Pino](https://github.com/pinojs/pino), a high-performance JSON logger for Node.js and browsers.
291
546
 
@@ -1,4 +1,4 @@
1
- export { B as BaseAdapter, d as GoogleAdapter, G as GoogleAdapterConfig, i as KwikCheckoutAdapter, b as KwikCheckoutConfig, h as KwikPassAdapter, K as KwikPassConfig, e as MoengageAdapter, M as MoengageAdapterConfig, c as PixelAdapter, P as PixelAdapterConfig, f as PostHogAdapter, a as PostHogAdapterConfig, g as ShopifyAdapter, S as ShopifyAdapterConfig } from '../index-GODWc1s6.mjs';
1
+ export { B as BaseAdapter, d as GoogleAdapter, G as GoogleAdapterConfig, i as KwikCheckoutAdapter, b as KwikCheckoutConfig, h as KwikPassAdapter, K as KwikPassConfig, e as MoengageAdapter, M as MoengageAdapterConfig, c as PixelAdapter, P as PixelAdapterConfig, f as PostHogAdapter, a as PostHogAdapterConfig, g as ShopifyAdapter, S as ShopifyAdapterConfig } from '../index-Bym1_EAp.mjs';
2
2
  import '../types.mjs';
3
- import '../subscriber-sWesj_5p.mjs';
3
+ import '../subscriber-BoyOlh9t.mjs';
4
4
  import '@shopify/hydrogen-react';
@@ -1,4 +1,4 @@
1
- export { B as BaseAdapter, d as GoogleAdapter, G as GoogleAdapterConfig, i as KwikCheckoutAdapter, b as KwikCheckoutConfig, h as KwikPassAdapter, K as KwikPassConfig, e as MoengageAdapter, M as MoengageAdapterConfig, c as PixelAdapter, P as PixelAdapterConfig, f as PostHogAdapter, a as PostHogAdapterConfig, g as ShopifyAdapter, S as ShopifyAdapterConfig } from '../index-BnNRgdUv.js';
1
+ export { B as BaseAdapter, d as GoogleAdapter, G as GoogleAdapterConfig, i as KwikCheckoutAdapter, b as KwikCheckoutConfig, h as KwikPassAdapter, K as KwikPassConfig, e as MoengageAdapter, M as MoengageAdapterConfig, c as PixelAdapter, P as PixelAdapterConfig, f as PostHogAdapter, a as PostHogAdapterConfig, g as ShopifyAdapter, S as ShopifyAdapterConfig } from '../index-DS9OI5Mz.js';
2
2
  import '../types.js';
3
- import '../subscriber-43gnCKWe.js';
3
+ import '../subscriber-BDAm_BAi.js';
4
4
  import '@shopify/hydrogen-react';