@akson/cortex-analytics-react 2.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/README.md +330 -0
- package/package.json +63 -0
package/README.md
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
# @akson/cortex-analytics-react
|
|
2
|
+
|
|
3
|
+
Reusable React analytics components and hooks for the @akson ecosystem. Provides a unified, configuration-driven approach to analytics tracking across multiple platforms.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **Unified API**: Single interface for GTM, PostHog, GA4, Google Ads, and Meta tracking
|
|
8
|
+
- ⚡ **Type-Safe**: Full TypeScript support with standardized event types
|
|
9
|
+
- 🎣 **React Hooks**: Purpose-built hooks for different tracking scenarios
|
|
10
|
+
- 🔧 **Configuration-Driven**: Easy setup with JSON/TypeScript configuration
|
|
11
|
+
- 📊 **Funnel Tracking**: Advanced funnel analysis with abandonment tracking
|
|
12
|
+
- 🎪 **Session Management**: Built-in session handling and persistence
|
|
13
|
+
- 🛡️ **Error-Safe**: Graceful handling of blocked trackers and storage errors
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @akson/cortex-analytics-react
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### 1. Setup Provider
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import React from 'react';
|
|
27
|
+
import { AnalyticsProvider, type AnalyticsConfig } from '@akson/cortex-analytics-react';
|
|
28
|
+
import { LEAD_GENERATION_EVENTS } from '@akson/cortex-api-shared/events';
|
|
29
|
+
|
|
30
|
+
const analyticsConfig: AnalyticsConfig = {
|
|
31
|
+
platforms: {
|
|
32
|
+
gtm: {
|
|
33
|
+
containerId: 'GTM-XXXXXXX',
|
|
34
|
+
workspaceId: 40,
|
|
35
|
+
},
|
|
36
|
+
posthog: {
|
|
37
|
+
apiKey: 'phc_your_key',
|
|
38
|
+
host: 'https://app.posthog.com',
|
|
39
|
+
},
|
|
40
|
+
ga4: {
|
|
41
|
+
measurementId: 'G-XXXXXXXXXX',
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
conversions: {
|
|
45
|
+
[LEAD_GENERATION_EVENTS.LEAD_FORM_SUBMITTED]: 'CONVERSION_LABEL_1',
|
|
46
|
+
[LEAD_GENERATION_EVENTS.LEAD_WHATSAPP_CONTACT]: 'CONVERSION_LABEL_2',
|
|
47
|
+
},
|
|
48
|
+
debug: process.env.NODE_ENV === 'development',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export default function App({ children }) {
|
|
52
|
+
return (
|
|
53
|
+
<AnalyticsProvider config={analyticsConfig}>
|
|
54
|
+
{children}
|
|
55
|
+
</AnalyticsProvider>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 2. Use Analytics Hooks
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import React from 'react';
|
|
64
|
+
import { useAnalytics, LEAD_GENERATION_EVENTS } from '@akson/cortex-analytics-react';
|
|
65
|
+
|
|
66
|
+
export function ContactForm() {
|
|
67
|
+
const analytics = useAnalytics();
|
|
68
|
+
|
|
69
|
+
const handleSubmit = (formData: any) => {
|
|
70
|
+
// Track standardized lead event
|
|
71
|
+
analytics.track(LEAD_GENERATION_EVENTS.LEAD_FORM_SUBMITTED, {
|
|
72
|
+
form_type: 'contact',
|
|
73
|
+
source: 'header',
|
|
74
|
+
lead_score: 100,
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<form onSubmit={handleSubmit}>
|
|
80
|
+
{/* Your form */}
|
|
81
|
+
</form>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Available Hooks
|
|
87
|
+
|
|
88
|
+
### `useAnalytics()`
|
|
89
|
+
Core analytics hook with full tracking capabilities.
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import { useAnalytics } from '@akson/cortex-analytics-react';
|
|
93
|
+
|
|
94
|
+
function Component() {
|
|
95
|
+
const analytics = useAnalytics();
|
|
96
|
+
|
|
97
|
+
// Basic tracking
|
|
98
|
+
analytics.track('custom_event', { property: 'value' });
|
|
99
|
+
analytics.trackPageView('/page', 'Page Title');
|
|
100
|
+
analytics.trackInteraction('button', 'click', 'header_cta');
|
|
101
|
+
|
|
102
|
+
// User identification
|
|
103
|
+
analytics.identify('user123', { email: 'user@example.com' });
|
|
104
|
+
analytics.sendUserData({ email: 'user@example.com', phone: '+1234567890' });
|
|
105
|
+
|
|
106
|
+
// Session utilities
|
|
107
|
+
const sessionId = analytics.getSessionId();
|
|
108
|
+
const funnelPath = analytics.getFunnelPath();
|
|
109
|
+
const timeInFunnel = analytics.getTimeInFunnel();
|
|
110
|
+
|
|
111
|
+
return <div>...</div>;
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### `useFunnelTracking()`
|
|
116
|
+
Specialized hook for funnel analysis.
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
import { useFunnelTracking } from '@akson/cortex-analytics-react';
|
|
120
|
+
|
|
121
|
+
function OrderForm() {
|
|
122
|
+
const funnel = useFunnelTracking();
|
|
123
|
+
|
|
124
|
+
const handleStepComplete = (step: string) => {
|
|
125
|
+
funnel.trackStage(step, {
|
|
126
|
+
completion_time: 30,
|
|
127
|
+
source: 'organic',
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const handleAbandonment = () => {
|
|
132
|
+
funnel.trackAbandonment('step_2', 'form_error');
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const handleConversion = () => {
|
|
136
|
+
funnel.trackConversion('order', 149.90, {
|
|
137
|
+
product_type: 'custom_badge',
|
|
138
|
+
currency: 'CHF',
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
return <div>...</div>;
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### `useLeadTracking()`
|
|
147
|
+
Hook for lead generation scenarios.
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
import { useLeadTracking, LEAD_GENERATION_EVENTS } from '@akson/cortex-analytics-react';
|
|
151
|
+
|
|
152
|
+
function LeadCapture() {
|
|
153
|
+
const lead = useLeadTracking();
|
|
154
|
+
|
|
155
|
+
const handleFormSubmit = (userData: any) => {
|
|
156
|
+
// Track the lead event
|
|
157
|
+
lead.trackLead(LEAD_GENERATION_EVENTS.LEAD_FORM_SUBMITTED, {
|
|
158
|
+
lead_score: 100,
|
|
159
|
+
source: 'landing_page',
|
|
160
|
+
form_type: 'quote_request',
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Send user data for enhanced conversions
|
|
164
|
+
lead.sendUserData({
|
|
165
|
+
email: userData.email,
|
|
166
|
+
phone: userData.phone,
|
|
167
|
+
firstName: userData.firstName,
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
return <div>...</div>;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### `useEcommerceTracking()`
|
|
176
|
+
Hook for e-commerce events.
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
import { useEcommerceTracking, ECOMMERCE_EVENTS } from '@akson/cortex-analytics-react';
|
|
180
|
+
|
|
181
|
+
function ProductPage() {
|
|
182
|
+
const ecommerce = useEcommerceTracking();
|
|
183
|
+
|
|
184
|
+
const handlePurchase = (orderData: any) => {
|
|
185
|
+
ecommerce.trackEcommerce(ECOMMERCE_EVENTS.PURCHASE, {
|
|
186
|
+
value: orderData.total,
|
|
187
|
+
currency: 'CHF',
|
|
188
|
+
transaction_id: orderData.id,
|
|
189
|
+
items: orderData.items.map(item => ({
|
|
190
|
+
item_name: item.name,
|
|
191
|
+
price: item.price,
|
|
192
|
+
quantity: item.quantity,
|
|
193
|
+
})),
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
return <div>...</div>;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Configuration
|
|
202
|
+
|
|
203
|
+
### Platform Configuration
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
interface AnalyticsConfig {
|
|
207
|
+
platforms: {
|
|
208
|
+
gtm?: {
|
|
209
|
+
containerId: string;
|
|
210
|
+
workspaceId?: number;
|
|
211
|
+
};
|
|
212
|
+
posthog?: {
|
|
213
|
+
apiKey: string;
|
|
214
|
+
host?: string;
|
|
215
|
+
};
|
|
216
|
+
ga4?: {
|
|
217
|
+
measurementId: string;
|
|
218
|
+
};
|
|
219
|
+
googleAds?: {
|
|
220
|
+
accountId: string;
|
|
221
|
+
};
|
|
222
|
+
meta?: {
|
|
223
|
+
pixelId: string;
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
conversions?: Record<string, string>; // Event -> Conversion label mapping
|
|
227
|
+
funnel?: FunnelConfig;
|
|
228
|
+
debug?: boolean;
|
|
229
|
+
enableEnhancedConversions?: boolean;
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Funnel Configuration
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
interface FunnelConfig {
|
|
237
|
+
stages?: Record<string, string[]>; // Stage name -> Event names
|
|
238
|
+
scoring?: (stage: string) => number;
|
|
239
|
+
abandonmentTracking?: boolean;
|
|
240
|
+
sessionTimeout?: number; // milliseconds
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Standardized Events
|
|
245
|
+
|
|
246
|
+
The package uses standardized events from `@akson/cortex-api-shared`:
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
import { LEAD_GENERATION_EVENTS, ECOMMERCE_EVENTS } from '@akson/cortex-analytics-react';
|
|
250
|
+
|
|
251
|
+
// Lead generation events (scored 1-100)
|
|
252
|
+
LEAD_GENERATION_EVENTS.LEAD_PAGE_VIEW // Score: 5
|
|
253
|
+
LEAD_GENERATION_EVENTS.LEAD_CONTENT_VIEW // Score: 15
|
|
254
|
+
LEAD_GENERATION_EVENTS.LEAD_INQUIRY_STARTED // Score: 40
|
|
255
|
+
LEAD_GENERATION_EVENTS.LEAD_CONTACT_INFO // Score: 60
|
|
256
|
+
LEAD_GENERATION_EVENTS.LEAD_WHATSAPP_CONTACT // Score: 85
|
|
257
|
+
LEAD_GENERATION_EVENTS.LEAD_FORM_SUBMITTED // Score: 100
|
|
258
|
+
|
|
259
|
+
// E-commerce events (with monetary values)
|
|
260
|
+
ECOMMERCE_EVENTS.VIEW_ITEM // Product views
|
|
261
|
+
ECOMMERCE_EVENTS.ADD_TO_CART // Cart additions
|
|
262
|
+
ECOMMERCE_EVENTS.BEGIN_CHECKOUT // Checkout started
|
|
263
|
+
ECOMMERCE_EVENTS.PURCHASE // Completed transactions
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Migration from Landing Analytics
|
|
267
|
+
|
|
268
|
+
If migrating from the landing page analytics system:
|
|
269
|
+
|
|
270
|
+
```tsx
|
|
271
|
+
// OLD: Landing-specific analytics
|
|
272
|
+
import { Analytics, FUNNEL_STAGES } from '@/app/lib/analytics';
|
|
273
|
+
|
|
274
|
+
// NEW: @akson/cortex-analytics-react
|
|
275
|
+
import { useAnalytics, LEAD_GENERATION_EVENTS } from '@akson/cortex-analytics-react';
|
|
276
|
+
|
|
277
|
+
function Component() {
|
|
278
|
+
const analytics = useAnalytics();
|
|
279
|
+
|
|
280
|
+
// OLD: Analytics.trackStage(FUNNEL_STAGES.FORM_SUBMITTED)
|
|
281
|
+
// NEW:
|
|
282
|
+
analytics.track(LEAD_GENERATION_EVENTS.LEAD_FORM_SUBMITTED, {
|
|
283
|
+
lead_score: 100,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Development
|
|
289
|
+
|
|
290
|
+
```bash
|
|
291
|
+
# Install dependencies
|
|
292
|
+
npm install
|
|
293
|
+
|
|
294
|
+
# Build package
|
|
295
|
+
npm run build
|
|
296
|
+
|
|
297
|
+
# Watch mode for development
|
|
298
|
+
npm run dev
|
|
299
|
+
|
|
300
|
+
# Type checking
|
|
301
|
+
npm run type-check
|
|
302
|
+
|
|
303
|
+
# Run tests (when implemented)
|
|
304
|
+
npm test
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## Architecture
|
|
308
|
+
|
|
309
|
+
The package follows a layered architecture:
|
|
310
|
+
|
|
311
|
+
- **Adapters**: Platform-specific implementations (GTM, PostHog)
|
|
312
|
+
- **Utilities**: Session management, storage, device detection
|
|
313
|
+
- **Hooks**: React integration layer
|
|
314
|
+
- **Providers**: Context and configuration management
|
|
315
|
+
- **Types**: Comprehensive TypeScript definitions
|
|
316
|
+
|
|
317
|
+
## Dependencies
|
|
318
|
+
|
|
319
|
+
### Peer Dependencies
|
|
320
|
+
- `react ^18.0.0`
|
|
321
|
+
- `react-dom ^18.0.0`
|
|
322
|
+
|
|
323
|
+
### Dependencies
|
|
324
|
+
- `@akson/cortex-api-shared` - Standardized event definitions
|
|
325
|
+
- `@akson/cortex-api-gtm` - GTM API client
|
|
326
|
+
- `@akson/cortex-api-posthog` - PostHog API client
|
|
327
|
+
|
|
328
|
+
## License
|
|
329
|
+
|
|
330
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@akson/cortex-analytics-react",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Reusable React analytics components and hooks for @cortex ecosystem",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsup",
|
|
15
|
+
"dev": "tsup --watch",
|
|
16
|
+
"type-check": "tsc --noEmit",
|
|
17
|
+
"clean": "rm -rf dist",
|
|
18
|
+
"test": "jest"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
22
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@akson/cortex-api-gtm": "^2.0.0",
|
|
26
|
+
"@akson/cortex-api-posthog": "^0.1.0",
|
|
27
|
+
"@akson/cortex-api-shared": "^0.3.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/react": "^19.0.0",
|
|
31
|
+
"@types/react-dom": "^19.0.0",
|
|
32
|
+
"react": "^19.1.0",
|
|
33
|
+
"react-dom": "^19.1.0",
|
|
34
|
+
"tsup": "^8.0.2",
|
|
35
|
+
"typescript": "^5.3.3"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"analytics",
|
|
39
|
+
"react",
|
|
40
|
+
"hooks",
|
|
41
|
+
"gtm",
|
|
42
|
+
"posthog",
|
|
43
|
+
"tracking"
|
|
44
|
+
],
|
|
45
|
+
"author": "MyArmy <contact@myarmy.ch>",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"homepage": "https://github.com/antoineschaller/myarmy/tree/main/packages/@akson/cortex-analytics-react",
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "https://github.com/antoineschaller/myarmy.git",
|
|
51
|
+
"directory": "packages/@akson/cortex-analytics-react"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=18.0.0"
|
|
55
|
+
},
|
|
56
|
+
"exports": {
|
|
57
|
+
".": {
|
|
58
|
+
"types": "./dist/index.d.ts",
|
|
59
|
+
"import": "./dist/index.js"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"gitHead": "84153813d2b6cc183f00f23d04cf0e887c512fbe"
|
|
63
|
+
}
|