shopkit-analytics 1.0.0
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/LICENSE +21 -0
- package/README.md +769 -0
- package/dist/adapters/index.d.mts +4 -0
- package/dist/adapters/index.d.ts +4 -0
- package/dist/adapters/index.js +2405 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/index.mjs +23 -0
- package/dist/adapters/index.mjs.map +1 -0
- package/dist/affiliate/index.d.mts +138 -0
- package/dist/affiliate/index.d.ts +138 -0
- package/dist/affiliate/index.js +816 -0
- package/dist/affiliate/index.js.map +1 -0
- package/dist/affiliate/index.mjs +74 -0
- package/dist/affiliate/index.mjs.map +1 -0
- package/dist/affiliate-tracker-BgHwibPv.d.mts +144 -0
- package/dist/affiliate-tracker-BgHwibPv.d.ts +144 -0
- package/dist/chunk-3TQR5DOP.mjs +79 -0
- package/dist/chunk-3TQR5DOP.mjs.map +1 -0
- package/dist/chunk-4MZH5OLR.mjs +2375 -0
- package/dist/chunk-4MZH5OLR.mjs.map +1 -0
- package/dist/chunk-JVEGG6JV.mjs +213 -0
- package/dist/chunk-JVEGG6JV.mjs.map +1 -0
- package/dist/chunk-P4OJDCEZ.mjs +57 -0
- package/dist/chunk-P4OJDCEZ.mjs.map +1 -0
- package/dist/chunk-TNXTKEGS.mjs +758 -0
- package/dist/chunk-TNXTKEGS.mjs.map +1 -0
- package/dist/events/index.d.mts +112 -0
- package/dist/events/index.d.ts +112 -0
- package/dist/events/index.js +2131 -0
- package/dist/events/index.js.map +1 -0
- package/dist/events/index.mjs +30 -0
- package/dist/events/index.mjs.map +1 -0
- package/dist/index-BnNRgdUv.d.ts +676 -0
- package/dist/index-GODWc1s6.d.mts +676 -0
- package/dist/index.d.mts +38 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +3269 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +190 -0
- package/dist/index.mjs.map +1 -0
- package/dist/subscriber-43gnCKWe.d.ts +80 -0
- package/dist/subscriber-IFZJU57V.mjs +8 -0
- package/dist/subscriber-IFZJU57V.mjs.map +1 -0
- package/dist/subscriber-sWesj_5p.d.mts +80 -0
- package/dist/types.d.mts +991 -0
- package/dist/types.d.ts +991 -0
- package/dist/types.js +102 -0
- package/dist/types.js.map +1 -0
- package/dist/types.mjs +8 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +110 -0
package/README.md
ADDED
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
# @shopkit/analytics
|
|
2
|
+
|
|
3
|
+
A comprehensive analytics package for e-commerce applications with support for multiple analytics platforms including Google Analytics 4, Facebook Pixel, MoEngage, PostHog, Shopify Analytics, KwikPass, and KwikCheckout.
|
|
4
|
+
|
|
5
|
+
## 🚀 Features
|
|
6
|
+
|
|
7
|
+
- **Multiple Analytics Platforms**: Support for 7+ analytics adapters
|
|
8
|
+
- **Event-Driven Architecture**: Publisher-subscriber pattern for flexible event handling
|
|
9
|
+
- **TypeScript Support**: Full type safety and IntelliSense
|
|
10
|
+
- **Affiliate Tracking**: Built-in UTM parameter and attribution tracking
|
|
11
|
+
- **React Integration**: Easy-to-use React components
|
|
12
|
+
- **Selective Adapter Routing**: Route events to specific adapters
|
|
13
|
+
- **Professional Logging**: High-performance structured logging with Pino
|
|
14
|
+
- **Debug Mode**: Comprehensive logging for development and production
|
|
15
|
+
|
|
16
|
+
## 📦 Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @shopkit/analytics
|
|
20
|
+
# or
|
|
21
|
+
yarn add @shopkit/analytics
|
|
22
|
+
# or
|
|
23
|
+
bun add @shopkit/analytics
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 🎯 Quick Start
|
|
27
|
+
|
|
28
|
+
### Basic Setup
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import { ShopkitAnalytics } from "@shopkit/analytics";
|
|
32
|
+
|
|
33
|
+
export function App() {
|
|
34
|
+
return (
|
|
35
|
+
<ShopkitAnalytics
|
|
36
|
+
config={{
|
|
37
|
+
googleAnalytics: {
|
|
38
|
+
measurementId: "G-XXXXXXXXXX",
|
|
39
|
+
},
|
|
40
|
+
facebookPixel: {
|
|
41
|
+
pixelId: "123456789",
|
|
42
|
+
},
|
|
43
|
+
logger: {
|
|
44
|
+
enabled: true,
|
|
45
|
+
level: "info",
|
|
46
|
+
prettyPrint: process.env.NODE_ENV === "development",
|
|
47
|
+
},
|
|
48
|
+
}}
|
|
49
|
+
onInitialized={() => console.log("Analytics initialized")}
|
|
50
|
+
onError={(error) => console.error("Analytics error:", error)}
|
|
51
|
+
>
|
|
52
|
+
<YourApp />
|
|
53
|
+
</ShopkitAnalytics>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Event Tracking
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
import { publishEvent, EventType } from "@shopkit/analytics";
|
|
62
|
+
|
|
63
|
+
// Track page view
|
|
64
|
+
publishEvent({
|
|
65
|
+
type: EventType.PAGE_VIEW,
|
|
66
|
+
path: "/products/example",
|
|
67
|
+
title: "Product Page",
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Track add to cart
|
|
71
|
+
publishEvent({
|
|
72
|
+
type: EventType.ADD_TO_CART,
|
|
73
|
+
productId: "123",
|
|
74
|
+
productName: "Example Product",
|
|
75
|
+
price: 29.99,
|
|
76
|
+
quantity: 1,
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 🔧 Analytics Adapters
|
|
81
|
+
|
|
82
|
+
### 1. Google Analytics 4
|
|
83
|
+
|
|
84
|
+
Track events to Google Analytics 4 with enhanced e-commerce support.
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
<ShopkitAnalytics
|
|
88
|
+
config={{
|
|
89
|
+
googleAnalytics: {
|
|
90
|
+
measurementId: "G-XXXXXXXXXX",
|
|
91
|
+
enableDebugLogs: true,
|
|
92
|
+
customDimensions: {
|
|
93
|
+
user_type: "premium",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
}}
|
|
97
|
+
/>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**Supported Events:**
|
|
101
|
+
- Page views, product views, add to cart
|
|
102
|
+
- Checkout events, purchases
|
|
103
|
+
- Search, scroll, form interactions
|
|
104
|
+
- Custom events with parameters
|
|
105
|
+
|
|
106
|
+
### 2. Facebook Pixel
|
|
107
|
+
|
|
108
|
+
Track conversions and optimize Facebook ad campaigns.
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
<ShopkitAnalytics
|
|
112
|
+
config={{
|
|
113
|
+
facebookPixel: {
|
|
114
|
+
pixelId: "123456789",
|
|
115
|
+
enableDebugLogs: true,
|
|
116
|
+
enableAdvancedMatching: true,
|
|
117
|
+
},
|
|
118
|
+
}}
|
|
119
|
+
/>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Supported Events:**
|
|
123
|
+
- ViewContent, AddToCart, Purchase
|
|
124
|
+
- InitiateCheckout, Search
|
|
125
|
+
- Custom conversions
|
|
126
|
+
|
|
127
|
+
### 3. MoEngage
|
|
128
|
+
|
|
129
|
+
Engage users with personalized campaigns and analytics.
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
<ShopkitAnalytics
|
|
133
|
+
config={{
|
|
134
|
+
moengage: {
|
|
135
|
+
appId: "YOUR_MOENGAGE_APP_ID",
|
|
136
|
+
enableDebugLogs: true,
|
|
137
|
+
region: "DEFAULT", // or "EU"
|
|
138
|
+
},
|
|
139
|
+
}}
|
|
140
|
+
/>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**Supported Events:**
|
|
144
|
+
- E-commerce events (purchase, cart updates)
|
|
145
|
+
- User lifecycle events (registration, login)
|
|
146
|
+
- Custom events with user attributes
|
|
147
|
+
|
|
148
|
+
### 4. PostHog
|
|
149
|
+
|
|
150
|
+
Product analytics and feature flags platform.
|
|
151
|
+
|
|
152
|
+
> **Important**: PostHog adapter assumes the PostHog script is already loaded in your application. You need to include the PostHog script in your HTML or load it separately.
|
|
153
|
+
|
|
154
|
+
```tsx
|
|
155
|
+
<ShopkitAnalytics
|
|
156
|
+
config={{
|
|
157
|
+
posthog: {
|
|
158
|
+
enableDebugLogs: true,
|
|
159
|
+
},
|
|
160
|
+
}}
|
|
161
|
+
/>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Setup Requirements:**
|
|
165
|
+
|
|
166
|
+
Add PostHog script to your HTML head or load it programmatically:
|
|
167
|
+
|
|
168
|
+
```html
|
|
169
|
+
<!-- Add to your HTML head -->
|
|
170
|
+
<script>
|
|
171
|
+
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]);t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
|
|
172
|
+
posthog.init('YOUR_POSTHOG_KEY', {api_host: 'https://app.posthog.com'})
|
|
173
|
+
</script>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Or load it programmatically:
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
// In your app initialization
|
|
180
|
+
import posthog from 'posthog-js'
|
|
181
|
+
|
|
182
|
+
posthog.init('YOUR_POSTHOG_KEY', {
|
|
183
|
+
api_host: 'https://app.posthog.com'
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**Environment Variables:**
|
|
188
|
+
```bash
|
|
189
|
+
NEXT_PUBLIC_POSTHOG_KEY="phc_xxxxxxxxxx"
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
**Supported Events:**
|
|
193
|
+
- Product analytics events
|
|
194
|
+
- User behavior tracking
|
|
195
|
+
- Feature usage analytics
|
|
196
|
+
|
|
197
|
+
### 5. Shopify Analytics
|
|
198
|
+
|
|
199
|
+
Native Shopify Admin Analytics integration.
|
|
200
|
+
|
|
201
|
+
```tsx
|
|
202
|
+
<ShopkitAnalytics
|
|
203
|
+
config={{
|
|
204
|
+
shopify: {
|
|
205
|
+
shopId: "your-shop-id",
|
|
206
|
+
domain: "your-shop.myshopify.com",
|
|
207
|
+
currency: "USD",
|
|
208
|
+
},
|
|
209
|
+
}}
|
|
210
|
+
/>
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
**Environment Variables:**
|
|
214
|
+
```bash
|
|
215
|
+
NEXT_PUBLIC_SHOPIFY_SHOP_ID="your-shop-id"
|
|
216
|
+
NEXT_PUBLIC_SHOPIFY_DOMAIN="your-shop.myshopify.com"
|
|
217
|
+
NEXT_PUBLIC_SHOPIFY_CURRENCY="USD"
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
**Supported Events:**
|
|
221
|
+
- Page views, product views
|
|
222
|
+
- Add to cart, checkout events
|
|
223
|
+
- Session tracking with Shopify cookies
|
|
224
|
+
|
|
225
|
+
### 6. KwikPass Analytics
|
|
226
|
+
|
|
227
|
+
Custom analytics adapter for KwikPass platform integration.
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
<ShopkitAnalytics
|
|
231
|
+
config={{
|
|
232
|
+
kwikpass: {
|
|
233
|
+
enableDebugLogs: true,
|
|
234
|
+
enableKwikPassEvents: true,
|
|
235
|
+
},
|
|
236
|
+
}}
|
|
237
|
+
/>
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
**Supported Events:**
|
|
241
|
+
- Product views, collection views
|
|
242
|
+
- Page views with custom event dispatching
|
|
243
|
+
- Enhanced affiliate parameter tracking
|
|
244
|
+
|
|
245
|
+
### 7. KwikCheckout Analytics (GoKwik)
|
|
246
|
+
|
|
247
|
+
Comprehensive checkout analytics adapter for GoKwik platform integration.
|
|
248
|
+
|
|
249
|
+
```tsx
|
|
250
|
+
<ShopkitAnalytics
|
|
251
|
+
config={{
|
|
252
|
+
kwikcheckout: {
|
|
253
|
+
mid: "your-gokwik-mid",
|
|
254
|
+
environment: "production", // or "staging"
|
|
255
|
+
storeId: "your-store-id",
|
|
256
|
+
enableDebugLogs: true,
|
|
257
|
+
},
|
|
258
|
+
}}
|
|
259
|
+
/>
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Environment Variables:**
|
|
263
|
+
```bash
|
|
264
|
+
NEXT_PUBLIC_GOKWIK_MID="your-gokwik-mid"
|
|
265
|
+
NEXT_PUBLIC_GOKWIK_ENVIRONMENT="production"
|
|
266
|
+
NEXT_PUBLIC_STORE_ID="your-store-id"
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**Supported Events:**
|
|
270
|
+
- Checkout flow events (started, completed)
|
|
271
|
+
- Address and payment method selection
|
|
272
|
+
- Mobile number and PIN code addition
|
|
273
|
+
- Order success and completion tracking
|
|
274
|
+
- Cart viewing and payment info events
|
|
275
|
+
|
|
276
|
+
**GoKwik-Specific Events:**
|
|
277
|
+
- `STARTED_CHECKOUT_GK` - Checkout initiation
|
|
278
|
+
- `MOBILE_ADDED_GK` - Mobile number added
|
|
279
|
+
- `ADDRESS_SELECTED_GK` - Address selection
|
|
280
|
+
- `ADDRESS_COMPLETED_GK` - Address completion
|
|
281
|
+
- `ADDRESS_ADDED_GK` - New address added
|
|
282
|
+
- `PIN_CODE_ADDED_GK` - PIN code verification
|
|
283
|
+
- `PAYMENT_METHOD_SELECTED_GK` - Payment method choice
|
|
284
|
+
- `PAYMENT_COMPLETED_GK` - Payment completion
|
|
285
|
+
- `ORDER_SUCCESS` - Order success confirmation
|
|
286
|
+
- `ORDER_COMPLETED` - Order fulfillment
|
|
287
|
+
|
|
288
|
+
## 📝 Professional Logging System
|
|
289
|
+
|
|
290
|
+
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
|
+
|
|
292
|
+
### Logger Configuration
|
|
293
|
+
|
|
294
|
+
Configure logging behavior for all adapters:
|
|
295
|
+
|
|
296
|
+
```tsx
|
|
297
|
+
<ShopkitAnalytics
|
|
298
|
+
config={{
|
|
299
|
+
// ... other config
|
|
300
|
+
logger: {
|
|
301
|
+
enabled: true,
|
|
302
|
+
level: "info", // "trace" | "debug" | "info" | "warn" | "error" | "fatal"
|
|
303
|
+
prettyPrint: process.env.NODE_ENV === "development",
|
|
304
|
+
redact: ["password", "token", "apiKey"], // Redact sensitive data
|
|
305
|
+
},
|
|
306
|
+
// Legacy support (deprecated but still works)
|
|
307
|
+
enableDebugLogs: true, // Maps to logger.enabled
|
|
308
|
+
}}
|
|
309
|
+
/>
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Log Levels
|
|
313
|
+
|
|
314
|
+
The logger supports standard log levels with appropriate use cases:
|
|
315
|
+
|
|
316
|
+
```tsx
|
|
317
|
+
// TRACE: Very detailed debugging information
|
|
318
|
+
logger.trace("Detailed execution flow");
|
|
319
|
+
|
|
320
|
+
// DEBUG: General debugging information
|
|
321
|
+
logger.debug("Processing event", { eventType: "add_to_cart" });
|
|
322
|
+
|
|
323
|
+
// INFO: General information about application flow
|
|
324
|
+
logger.info("Analytics adapter initialized", { adapterName: "GoogleAnalytics" });
|
|
325
|
+
|
|
326
|
+
// WARN: Warning messages for potential issues
|
|
327
|
+
logger.warn("Missing optional parameter", { parameter: "currency" });
|
|
328
|
+
|
|
329
|
+
// ERROR: Error conditions that don't stop the application
|
|
330
|
+
logger.error("Failed to send event", new Error("Network timeout"));
|
|
331
|
+
|
|
332
|
+
// FATAL: Critical errors that may cause application termination
|
|
333
|
+
logger.fatal("Critical system failure", new Error("Database connection lost"));
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Structured Logging
|
|
337
|
+
|
|
338
|
+
All logs are structured with contextual information:
|
|
339
|
+
|
|
340
|
+
```json
|
|
341
|
+
{
|
|
342
|
+
"level": 30,
|
|
343
|
+
"time": 1635724800000,
|
|
344
|
+
"name": "@shopkit/analytics",
|
|
345
|
+
"adapter": "GoogleAnalytics",
|
|
346
|
+
"msg": "Event tracked successfully",
|
|
347
|
+
"eventType": "add_to_cart",
|
|
348
|
+
"productId": "123",
|
|
349
|
+
"price": 29.99
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Browser-Friendly Output
|
|
354
|
+
|
|
355
|
+
In development mode with `prettyPrint: true`:
|
|
356
|
+
|
|
357
|
+
```
|
|
358
|
+
[14:20:00.123] INFO (@shopkit/analytics/GoogleAnalytics): Event tracked successfully
|
|
359
|
+
eventType: "add_to_cart"
|
|
360
|
+
productId: "123"
|
|
361
|
+
price: 29.99
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### Sensitive Data Redaction
|
|
365
|
+
|
|
366
|
+
Automatically redact sensitive information:
|
|
367
|
+
|
|
368
|
+
```tsx
|
|
369
|
+
<ShopkitAnalytics
|
|
370
|
+
config={{
|
|
371
|
+
logger: {
|
|
372
|
+
enabled: true,
|
|
373
|
+
redact: [
|
|
374
|
+
"password",
|
|
375
|
+
"token",
|
|
376
|
+
"apiKey",
|
|
377
|
+
"creditCard",
|
|
378
|
+
"ssn",
|
|
379
|
+
"email", // Optional: redact PII
|
|
380
|
+
],
|
|
381
|
+
},
|
|
382
|
+
}}
|
|
383
|
+
/>
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
### Adapter-Specific Logging
|
|
387
|
+
|
|
388
|
+
Each adapter gets its own logger instance with proper naming:
|
|
389
|
+
|
|
390
|
+
```tsx
|
|
391
|
+
// Google Analytics logs
|
|
392
|
+
[GoogleAnalytics] Event tracked successfully
|
|
393
|
+
|
|
394
|
+
// Facebook Pixel logs
|
|
395
|
+
[FacebookPixel] Pixel event sent
|
|
396
|
+
|
|
397
|
+
// MoEngage logs
|
|
398
|
+
[MoEngage] User attribute updated
|
|
399
|
+
|
|
400
|
+
// PostHog logs
|
|
401
|
+
[PostHog] Feature flag evaluated
|
|
402
|
+
|
|
403
|
+
// Shopify Analytics logs
|
|
404
|
+
[ShopifyAnalytics] Session tracking enabled
|
|
405
|
+
|
|
406
|
+
// KwikPass logs
|
|
407
|
+
[KwikPass] Custom event dispatched
|
|
408
|
+
|
|
409
|
+
// KwikCheckout logs
|
|
410
|
+
[KwikCheckout] Checkout event processed
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Production Logging
|
|
414
|
+
|
|
415
|
+
For production environments, use structured JSON logging:
|
|
416
|
+
|
|
417
|
+
```tsx
|
|
418
|
+
const analyticsConfig = {
|
|
419
|
+
logger: {
|
|
420
|
+
enabled: true,
|
|
421
|
+
level: process.env.NODE_ENV === "production" ? "warn" : "debug",
|
|
422
|
+
prettyPrint: false, // JSON output for log aggregation
|
|
423
|
+
redact: ["password", "token", "apiKey", "email"],
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
## 🎯 Event Types
|
|
429
|
+
|
|
430
|
+
### E-commerce Events
|
|
431
|
+
|
|
432
|
+
```tsx
|
|
433
|
+
// Product View
|
|
434
|
+
publishEvent({
|
|
435
|
+
type: EventType.PRODUCT_VIEW,
|
|
436
|
+
productId: "123",
|
|
437
|
+
productName: "Example Product",
|
|
438
|
+
price: 29.99,
|
|
439
|
+
currency: "USD",
|
|
440
|
+
category: "Electronics",
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// Add to Cart
|
|
444
|
+
publishEvent({
|
|
445
|
+
type: EventType.ADD_TO_CART,
|
|
446
|
+
productId: "123",
|
|
447
|
+
productName: "Example Product",
|
|
448
|
+
price: 29.99,
|
|
449
|
+
quantity: 2,
|
|
450
|
+
variant: "Red",
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
// Checkout Started
|
|
454
|
+
publishEvent({
|
|
455
|
+
type: EventType.CHECKOUT_STARTED,
|
|
456
|
+
cartValue: 59.98,
|
|
457
|
+
currency: "USD",
|
|
458
|
+
itemCount: 2,
|
|
459
|
+
items: [
|
|
460
|
+
{
|
|
461
|
+
productId: "123",
|
|
462
|
+
productName: "Example Product",
|
|
463
|
+
price: 29.99,
|
|
464
|
+
quantity: 2,
|
|
465
|
+
},
|
|
466
|
+
],
|
|
467
|
+
});
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### User Interaction Events
|
|
471
|
+
|
|
472
|
+
```tsx
|
|
473
|
+
// Page View
|
|
474
|
+
publishEvent({
|
|
475
|
+
type: EventType.PAGE_VIEW,
|
|
476
|
+
path: "/products/example",
|
|
477
|
+
title: "Product Page",
|
|
478
|
+
referrer: document.referrer,
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
// Button Click
|
|
482
|
+
publishEvent({
|
|
483
|
+
type: EventType.BUTTON_CLICK,
|
|
484
|
+
buttonId: "cta-button",
|
|
485
|
+
buttonText: "Buy Now",
|
|
486
|
+
location: "product-page",
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// Search
|
|
490
|
+
publishEvent({
|
|
491
|
+
type: EventType.SEARCH,
|
|
492
|
+
searchTerm: "wireless headphones",
|
|
493
|
+
resultsCount: 25,
|
|
494
|
+
});
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
### GoKwik Checkout Events
|
|
498
|
+
|
|
499
|
+
```tsx
|
|
500
|
+
// Started Checkout
|
|
501
|
+
publishEvent({
|
|
502
|
+
type: EventType.STARTED_CHECKOUT_GK,
|
|
503
|
+
cartValue: 99.99,
|
|
504
|
+
currency: "USD",
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
// Mobile Added
|
|
508
|
+
publishEvent({
|
|
509
|
+
type: EventType.MOBILE_ADDED_GK,
|
|
510
|
+
mobile: "+1234567890",
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
// Address Selected
|
|
514
|
+
publishEvent({
|
|
515
|
+
type: EventType.ADDRESS_SELECTED_GK,
|
|
516
|
+
addressId: "addr_123",
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
// Payment Method Selected
|
|
520
|
+
publishEvent({
|
|
521
|
+
type: EventType.PAYMENT_METHOD_SELECTED_GK,
|
|
522
|
+
paymentMethod: "credit_card",
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
// Payment Completed
|
|
526
|
+
publishEvent({
|
|
527
|
+
type: EventType.PAYMENT_COMPLETED_GK,
|
|
528
|
+
amount: 99.99,
|
|
529
|
+
currency: "USD",
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
// Order Success
|
|
533
|
+
publishEvent({
|
|
534
|
+
type: EventType.ORDER_SUCCESS,
|
|
535
|
+
orderId: "order_123",
|
|
536
|
+
amount: 99.99,
|
|
537
|
+
currency: "USD",
|
|
538
|
+
});
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
## 🔀 Selective Adapter Routing
|
|
542
|
+
|
|
543
|
+
Route events to specific adapters for optimized performance:
|
|
544
|
+
|
|
545
|
+
```tsx
|
|
546
|
+
// Send to specific adapters only
|
|
547
|
+
publishEvent(
|
|
548
|
+
{
|
|
549
|
+
type: EventType.ADD_TO_CART,
|
|
550
|
+
productId: "123",
|
|
551
|
+
productName: "Example Product",
|
|
552
|
+
price: 29.99,
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
adapters: ["GoogleAnalytics", "FacebookPixel"], // Only these adapters
|
|
556
|
+
}
|
|
557
|
+
);
|
|
558
|
+
|
|
559
|
+
// Send GoKwik events only to KwikCheckout adapter
|
|
560
|
+
publishEvent(
|
|
561
|
+
{
|
|
562
|
+
type: EventType.STARTED_CHECKOUT_GK,
|
|
563
|
+
cartValue: 99.99,
|
|
564
|
+
currency: "USD",
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
adapters: ["KwikCheckout"], // Only GoKwik adapter
|
|
568
|
+
}
|
|
569
|
+
);
|
|
570
|
+
|
|
571
|
+
// Exclude specific adapters
|
|
572
|
+
publishEvent(
|
|
573
|
+
{
|
|
574
|
+
type: EventType.PAGE_VIEW,
|
|
575
|
+
path: "/internal-page",
|
|
576
|
+
title: "Internal Page",
|
|
577
|
+
},
|
|
578
|
+
{
|
|
579
|
+
excludeAdapters: ["FacebookPixel"], // All except Facebook Pixel
|
|
580
|
+
}
|
|
581
|
+
);
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
## 🏷️ Affiliate Tracking
|
|
585
|
+
|
|
586
|
+
Built-in affiliate parameter tracking and attribution:
|
|
587
|
+
|
|
588
|
+
```tsx
|
|
589
|
+
<ShopkitAnalytics
|
|
590
|
+
config={{
|
|
591
|
+
// ... other config
|
|
592
|
+
affiliate: {
|
|
593
|
+
enabled: true,
|
|
594
|
+
autoCapture: true,
|
|
595
|
+
config: {
|
|
596
|
+
utmParams: ["utm_source", "utm_medium", "utm_campaign"],
|
|
597
|
+
customParams: ["ref", "affiliate_id"],
|
|
598
|
+
storageKey: "affiliate_data",
|
|
599
|
+
expirationDays: 30,
|
|
600
|
+
},
|
|
601
|
+
},
|
|
602
|
+
}}
|
|
603
|
+
/>
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
### Affiliate Hooks
|
|
607
|
+
|
|
608
|
+
```tsx
|
|
609
|
+
import { useAffiliateTracker, useHasAffiliateData } from "@shopkit/analytics";
|
|
610
|
+
|
|
611
|
+
function MyComponent() {
|
|
612
|
+
const hasAffiliateData = useHasAffiliateData();
|
|
613
|
+
const affiliateSource = useAffiliateSource();
|
|
614
|
+
|
|
615
|
+
if (hasAffiliateData) {
|
|
616
|
+
console.log("Affiliate source:", affiliateSource);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
```
|
|
620
|
+
|
|
621
|
+
## 🛠️ Advanced Configuration
|
|
622
|
+
|
|
623
|
+
### Custom Adapters
|
|
624
|
+
|
|
625
|
+
Create your own analytics adapter:
|
|
626
|
+
|
|
627
|
+
```tsx
|
|
628
|
+
import { BaseAdapter } from "@shopkit/analytics";
|
|
629
|
+
|
|
630
|
+
class CustomAdapter extends BaseAdapter {
|
|
631
|
+
public readonly name = "CustomAnalytics";
|
|
632
|
+
|
|
633
|
+
async initialize() {
|
|
634
|
+
this.initializeLogger();
|
|
635
|
+
// Initialize your analytics service
|
|
636
|
+
this.initialized = true;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
async trackEvent(event) {
|
|
640
|
+
// Send event to your analytics service
|
|
641
|
+
this.logger.info("Custom tracking", { event });
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Use custom adapter
|
|
646
|
+
<ShopkitAnalytics
|
|
647
|
+
config={{
|
|
648
|
+
customAdapters: [new CustomAdapter()],
|
|
649
|
+
}}
|
|
650
|
+
/>
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
### Environment-Based Configuration
|
|
654
|
+
|
|
655
|
+
```tsx
|
|
656
|
+
const analyticsConfig = {
|
|
657
|
+
googleAnalytics: process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID
|
|
658
|
+
? {
|
|
659
|
+
measurementId: process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID,
|
|
660
|
+
}
|
|
661
|
+
: undefined,
|
|
662
|
+
facebookPixel: process.env.NEXT_PUBLIC_FB_PIXEL_ID
|
|
663
|
+
? {
|
|
664
|
+
pixelId: process.env.NEXT_PUBLIC_FB_PIXEL_ID,
|
|
665
|
+
}
|
|
666
|
+
: undefined,
|
|
667
|
+
logger: {
|
|
668
|
+
enabled: true,
|
|
669
|
+
level: process.env.NODE_ENV === "development" ? "debug" : "info",
|
|
670
|
+
prettyPrint: process.env.NODE_ENV === "development",
|
|
671
|
+
},
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
<ShopkitAnalytics config={analyticsConfig} />
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
## 🔧 API Reference
|
|
678
|
+
|
|
679
|
+
### ShopkitAnalytics Props
|
|
680
|
+
|
|
681
|
+
```tsx
|
|
682
|
+
interface ShopkitAnalyticsProps {
|
|
683
|
+
config: ShopkitAnalyticsConfig;
|
|
684
|
+
children?: React.ReactNode;
|
|
685
|
+
onInitialized?: () => void;
|
|
686
|
+
onError?: (error: Error) => void;
|
|
687
|
+
}
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
### Configuration Interface
|
|
691
|
+
|
|
692
|
+
```tsx
|
|
693
|
+
interface ShopkitAnalyticsConfig {
|
|
694
|
+
// Analytics Adapters
|
|
695
|
+
googleAnalytics?: GoogleAdapterConfig;
|
|
696
|
+
facebookPixel?: PixelAdapterConfig;
|
|
697
|
+
moengage?: MoengageAdapterConfig;
|
|
698
|
+
posthog?: PostHogAdapterConfig;
|
|
699
|
+
shopify?: ShopifyAdapterConfig;
|
|
700
|
+
kwikpass?: KwikPassConfig;
|
|
701
|
+
kwikcheckout?: KwikCheckoutConfig;
|
|
702
|
+
customAdapters?: TrackingAdapter[];
|
|
703
|
+
|
|
704
|
+
// Logging Configuration
|
|
705
|
+
logger?: {
|
|
706
|
+
enabled?: boolean;
|
|
707
|
+
level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal";
|
|
708
|
+
prettyPrint?: boolean;
|
|
709
|
+
redact?: string[];
|
|
710
|
+
name?: string;
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
// Affiliate Tracking
|
|
714
|
+
affiliate?: {
|
|
715
|
+
enabled?: boolean;
|
|
716
|
+
config?: Partial<AffiliateConfig>;
|
|
717
|
+
autoCapture?: boolean;
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
// General Settings
|
|
721
|
+
enableDebugLogs?: boolean; // Deprecated: use logger.enabled
|
|
722
|
+
autoInitialize?: boolean;
|
|
723
|
+
}
|
|
724
|
+
```
|
|
725
|
+
|
|
726
|
+
## 🚨 Important Notes
|
|
727
|
+
|
|
728
|
+
### Requirements
|
|
729
|
+
|
|
730
|
+
- React 16.8+ (for hooks support)
|
|
731
|
+
- Modern browser with ES2020 support
|
|
732
|
+
- Valid analytics account credentials
|
|
733
|
+
|
|
734
|
+
### Best Practices
|
|
735
|
+
|
|
736
|
+
1. **Environment Variables**: Store sensitive keys in environment variables
|
|
737
|
+
2. **Logger Configuration**: Use appropriate log levels for production
|
|
738
|
+
3. **Event Validation**: Ensure required event properties are provided
|
|
739
|
+
4. **Performance**: Use selective adapter routing for high-traffic events
|
|
740
|
+
5. **Privacy**: Implement proper consent management
|
|
741
|
+
|
|
742
|
+
### Limitations
|
|
743
|
+
|
|
744
|
+
- Some adapters may have rate limits
|
|
745
|
+
- Event data size limits vary by platform
|
|
746
|
+
- Real-time reporting availability depends on the platform
|
|
747
|
+
|
|
748
|
+
## 📄 License
|
|
749
|
+
|
|
750
|
+
MIT License - see LICENSE file for details.
|
|
751
|
+
|
|
752
|
+
## 🤝 Contributing
|
|
753
|
+
|
|
754
|
+
1. Fork the repository
|
|
755
|
+
2. Create a feature branch
|
|
756
|
+
3. Make your changes
|
|
757
|
+
4. Add tests if applicable
|
|
758
|
+
5. Submit a pull request
|
|
759
|
+
|
|
760
|
+
## 📞 Support
|
|
761
|
+
|
|
762
|
+
For issues and questions:
|
|
763
|
+
- Create an issue on GitHub
|
|
764
|
+
- Check the documentation
|
|
765
|
+
- Review the debug logs
|
|
766
|
+
|
|
767
|
+
---
|
|
768
|
+
|
|
769
|
+
**@shopkit/analytics** - Comprehensive analytics solution for modern e-commerce applications.
|