@aacigroup/aaci_shared 2.6.0 → 2.6.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.
- package/README.md +181 -443
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,98 +1,35 @@
|
|
|
1
1
|
# AACI Shared Library
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React Context-based tracking library for AACI Group projects with lead capture and analytics.
|
|
4
4
|
|
|
5
5
|
[](https://badge.fury.io/js/@aacigroup%2Faaci_shared)
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
npm install @aacigroup/aaci_shared
|
|
10
|
+
npm install @aacigroup/aaci_shared react
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## React Context Setup
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
import { LeadTracker } from '@aacigroup/aaci_shared';
|
|
17
|
-
|
|
18
|
-
const tracker = new LeadTracker({
|
|
19
|
-
apiUrl: 'https://your-api.com/leads',
|
|
20
|
-
apiKey: 'your-api-key',
|
|
21
|
-
projectName: 'MyProject',
|
|
22
|
-
productionDomains: ['myproject.com']
|
|
23
|
-
});
|
|
15
|
+
### Environment Variables
|
|
24
16
|
|
|
25
|
-
|
|
26
|
-
await tracker.trackLeadAndAddress({
|
|
27
|
-
lead_type: 'contact',
|
|
28
|
-
email: 'user@example.com'
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
// Track address only
|
|
32
|
-
await tracker.trackLeadAndAddress(undefined, {
|
|
33
|
-
full_address: '123 Main St, New York, NY 10001',
|
|
34
|
-
source: 'address_check'
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Track both together
|
|
38
|
-
await tracker.trackLeadAndAddress({
|
|
39
|
-
lead_type: 'policy_review',
|
|
40
|
-
email: 'user@example.com'
|
|
41
|
-
}, {
|
|
42
|
-
full_address: '123 Main St, New York, NY 10001',
|
|
43
|
-
source: 'policy_review'
|
|
44
|
-
});
|
|
45
|
-
```
|
|
17
|
+
Add these to your `.env` file:
|
|
46
18
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- `'signup'` - User registration
|
|
54
|
-
- `'consultation'` - Service consultation
|
|
55
|
-
- `'quote'` - Price quote requests
|
|
56
|
-
|
|
57
|
-
## Validation (Optional)
|
|
58
|
-
|
|
59
|
-
Install Zod for input validation:
|
|
60
|
-
```bash
|
|
61
|
-
npm install zod
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
```javascript
|
|
65
|
-
import { validateTrackLeadParams, validateAddress } from '@aacigroup/aaci_shared';
|
|
66
|
-
|
|
67
|
-
// Validate before tracking
|
|
68
|
-
const validLead = validateTrackLeadParams({
|
|
69
|
-
lead_type: 'contact',
|
|
70
|
-
email: 'user@example.com'
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
const validAddress = validateAddress({
|
|
74
|
-
full_address: '123 Main St',
|
|
75
|
-
source: 'contact'
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
await tracker.trackLeadAndAddress(validLead, validAddress);
|
|
19
|
+
```env
|
|
20
|
+
VITE_LEAD_CAPTURE_API_URL=https://your-api.com/leads
|
|
21
|
+
VITE_LEAD_CAPTURE_API_KEY=your-api-key
|
|
22
|
+
VITE_PROJECT_NAME=MyProject
|
|
23
|
+
VITE_POSTHOG_KEY=phc_your_posthog_key
|
|
24
|
+
VITE_PRODUCTION_DOMAINS=myproject.com,www.myproject.com
|
|
79
25
|
```
|
|
80
26
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
### React Context (Optional)
|
|
84
|
-
|
|
85
|
-
For React projects, you can use the provided context for easier integration:
|
|
86
|
-
|
|
87
|
-
```bash
|
|
88
|
-
npm install react # Peer dependency
|
|
89
|
-
```
|
|
27
|
+
### App Setup
|
|
90
28
|
|
|
91
29
|
```javascript
|
|
92
30
|
// App.tsx
|
|
93
31
|
import { TrackingProvider, createTrackingConfigFromEnv } from '@aacigroup/aaci_shared/react';
|
|
94
32
|
|
|
95
|
-
// Option 1: Use environment variables (in Vite projects)
|
|
96
33
|
function App() {
|
|
97
34
|
return (
|
|
98
35
|
<TrackingProvider config={createTrackingConfigFromEnv()}>
|
|
@@ -100,25 +37,12 @@ function App() {
|
|
|
100
37
|
</TrackingProvider>
|
|
101
38
|
);
|
|
102
39
|
}
|
|
40
|
+
```
|
|
103
41
|
|
|
104
|
-
|
|
105
|
-
function App() {
|
|
106
|
-
const trackingConfig = {
|
|
107
|
-
apiUrl: 'https://your-api.com/leads',
|
|
108
|
-
apiKey: 'your-api-key',
|
|
109
|
-
projectName: 'MyProject',
|
|
110
|
-
posthogKey: 'phc_your_key',
|
|
111
|
-
productionDomains: ['myproject.com']
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
return (
|
|
115
|
-
<TrackingProvider config={trackingConfig}>
|
|
116
|
-
<YourApp />
|
|
117
|
-
</TrackingProvider>
|
|
118
|
-
);
|
|
119
|
-
}
|
|
42
|
+
### Component Usage
|
|
120
43
|
|
|
121
|
-
|
|
44
|
+
```javascript
|
|
45
|
+
// ContactForm.tsx
|
|
122
46
|
import { useLeadTracker, usePostHog } from '@aacigroup/aaci_shared/react';
|
|
123
47
|
|
|
124
48
|
function ContactForm() {
|
|
@@ -129,7 +53,8 @@ function ContactForm() {
|
|
|
129
53
|
// Track lead submission
|
|
130
54
|
await tracker.trackLeadAndAddress({
|
|
131
55
|
lead_type: 'contact',
|
|
132
|
-
email: formData.email
|
|
56
|
+
email: formData.email,
|
|
57
|
+
first_name: formData.firstName
|
|
133
58
|
});
|
|
134
59
|
|
|
135
60
|
// Track analytics event
|
|
@@ -140,71 +65,9 @@ function ContactForm() {
|
|
|
140
65
|
}
|
|
141
66
|
```
|
|
142
67
|
|
|
143
|
-
|
|
144
|
-
```env
|
|
145
|
-
VITE_LEAD_CAPTURE_API_URL=https://your-api.com/leads
|
|
146
|
-
VITE_LEAD_CAPTURE_API_KEY=your-api-key
|
|
147
|
-
VITE_PROJECT_NAME=MyProject
|
|
148
|
-
VITE_POSTHOG_KEY=phc_your_key
|
|
149
|
-
VITE_PRODUCTION_DOMAINS=myproject.com,www.myproject.com
|
|
150
|
-
```
|
|
68
|
+
## Data Types
|
|
151
69
|
|
|
152
|
-
###
|
|
153
|
-
```javascript
|
|
154
|
-
import { PostHog } from '@aacigroup/aaci_shared';
|
|
155
|
-
|
|
156
|
-
const analytics = new PostHog({
|
|
157
|
-
apiKey: 'phc_your_key',
|
|
158
|
-
projectName: 'MyProject',
|
|
159
|
-
productionDomains: ['myproject.com']
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
await analytics.init();
|
|
163
|
-
analytics.trackEvent('user_signup', { plan: 'premium' });
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### Google Tag Manager
|
|
167
|
-
```javascript
|
|
168
|
-
import { GTM } from '@aacigroup/aaci_shared';
|
|
169
|
-
|
|
170
|
-
const gtm = new GTM({
|
|
171
|
-
productionDomains: ['myproject.com']
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
gtm.trackEvent('purchase', { value: 100, currency: 'USD' });
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
## TypeScript Types
|
|
178
|
-
|
|
179
|
-
```javascript
|
|
180
|
-
import type { Lead, AddressCapture, TrackLeadParams, Address } from '@aacigroup/aaci_shared';
|
|
181
|
-
|
|
182
|
-
// Use types in your application
|
|
183
|
-
function processLead(lead: Lead): void {
|
|
184
|
-
console.log(`Processing ${lead.email}`);
|
|
185
|
-
}
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
## License
|
|
189
|
-
|
|
190
|
-
ISC
|
|
191
|
-
|
|
192
|
-
## Core API Reference
|
|
193
|
-
|
|
194
|
-
### LeadTracker
|
|
195
|
-
|
|
196
|
-
#### `trackLeadAndAddress(lead?, address?)`
|
|
197
|
-
|
|
198
|
-
**Primary tracking method that accepts lead data, address data, or both.**
|
|
199
|
-
|
|
200
|
-
**Parameters:**
|
|
201
|
-
- `lead` (optional): Lead information object
|
|
202
|
-
- `address` (optional): Address information object
|
|
203
|
-
- **At least one parameter is required**
|
|
204
|
-
|
|
205
|
-
**Returns:** `Promise<LeadTrackingResponse>`
|
|
206
|
-
|
|
207
|
-
#### Lead Capture Types
|
|
70
|
+
### Lead Data
|
|
208
71
|
|
|
209
72
|
```typescript
|
|
210
73
|
interface TrackLeadParams {
|
|
@@ -214,11 +77,10 @@ interface TrackLeadParams {
|
|
|
214
77
|
email?: string; // Optional: Email address
|
|
215
78
|
phone?: string; // Optional: Phone number
|
|
216
79
|
extra_data?: Record<string, any>; // Optional: Additional custom data
|
|
217
|
-
session_data?: SessionData; // Optional: Session tracking data (auto-populated)
|
|
218
80
|
}
|
|
219
81
|
```
|
|
220
82
|
|
|
221
|
-
**Common `lead_type` examples:**
|
|
83
|
+
**Common `lead_type` examples (free string):**
|
|
222
84
|
- `'policy_review'` - Insurance policy review requests
|
|
223
85
|
- `'address_check'` - Property address verification
|
|
224
86
|
- `'contact'` - General contact form submissions
|
|
@@ -226,11 +88,13 @@ interface TrackLeadParams {
|
|
|
226
88
|
- `'consultation'` - Service consultation requests
|
|
227
89
|
- `'quote'` - Price quote requests
|
|
228
90
|
|
|
229
|
-
|
|
230
|
-
|
|
91
|
+
Note: `lead_type` is a free string - you can use any value that makes sense for your application.
|
|
92
|
+
|
|
93
|
+
**Validation Rules:**
|
|
231
94
|
- `lead_type` is always required
|
|
95
|
+
- Either `email` OR `phone` is required when tracking a lead
|
|
232
96
|
|
|
233
|
-
|
|
97
|
+
### Address Data
|
|
234
98
|
|
|
235
99
|
```typescript
|
|
236
100
|
interface Address {
|
|
@@ -246,337 +110,211 @@ interface Address {
|
|
|
246
110
|
}
|
|
247
111
|
```
|
|
248
112
|
|
|
249
|
-
|
|
250
|
-
- `full_address` is always required
|
|
251
|
-
- `source` is always required (uses same values as `lead_type`)
|
|
113
|
+
### Session Data
|
|
252
114
|
|
|
253
|
-
|
|
115
|
+
```typescript
|
|
116
|
+
interface SessionData {
|
|
117
|
+
ip?: string; // Optional: User's IP address
|
|
118
|
+
user_agent?: string; // Optional: Browser user agent
|
|
119
|
+
browser?: string; // Optional: Browser name
|
|
120
|
+
browser_version?: string; // Optional: Browser version
|
|
121
|
+
os?: string; // Optional: Operating system
|
|
122
|
+
device?: string; // Optional: Device type
|
|
123
|
+
referrer?: string; // Optional: Referring page URL
|
|
124
|
+
utm_source?: string; // Optional: UTM source parameter
|
|
125
|
+
utm_medium?: string; // Optional: UTM medium parameter
|
|
126
|
+
utm_campaign?: string; // Optional: UTM campaign parameter
|
|
127
|
+
landing_page?: string; // Optional: First page visited
|
|
128
|
+
current_page?: string; // Optional: Current page URL
|
|
129
|
+
session_id?: string; // Optional: Session identifier
|
|
130
|
+
timestamp?: string; // Optional: Timestamp of action
|
|
131
|
+
distinct_id?: string; // Optional: User identifier
|
|
132
|
+
gclid?: string; // Optional: Google Ads click ID
|
|
133
|
+
fbclid?: string; // Optional: Facebook click ID
|
|
134
|
+
fbc?: string; // Optional: Facebook browser cookie
|
|
135
|
+
fbp?: string; // Optional: Facebook pixel cookie
|
|
136
|
+
}
|
|
137
|
+
```
|
|
254
138
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
139
|
+
**Common `source` examples (free string):**
|
|
140
|
+
- `'policy_review'` - When tracking address from policy review forms
|
|
141
|
+
- `'address_check'` - For standalone address verification
|
|
142
|
+
- `'contact'` - From general contact forms
|
|
143
|
+
- `'signup'` - During user registration
|
|
144
|
+
- `'consultation'` - From consultation request forms
|
|
145
|
+
- `'quote'` - From quote request forms
|
|
262
146
|
|
|
263
|
-
|
|
264
|
-
await tracker.trackLeadAndAddress({
|
|
265
|
-
lead_type: 'policy_review',
|
|
266
|
-
phone: '+1234567890',
|
|
267
|
-
extra_data: { policy_number: 'POL123456' }
|
|
268
|
-
});
|
|
147
|
+
Note: `source` is a free string - you can use any value to identify where the address came from. It may match `lead_type` by design but doesn't have to.
|
|
269
148
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
source: 'address_check',
|
|
274
|
-
city: 'New York',
|
|
275
|
-
state: 'NY',
|
|
276
|
-
zip_code: '10001'
|
|
277
|
-
});
|
|
149
|
+
**Validation Rules:**
|
|
150
|
+
- `full_address` is always required
|
|
151
|
+
- `source` is always required
|
|
278
152
|
|
|
279
|
-
|
|
280
|
-
await tracker.trackLeadAndAddress({
|
|
281
|
-
lead_type: 'address_check',
|
|
282
|
-
email: 'homeowner@example.com',
|
|
283
|
-
first_name: 'Jane'
|
|
284
|
-
}, {
|
|
285
|
-
full_address: '456 Oak Ave, Boston, MA 02101',
|
|
286
|
-
source: 'address_check',
|
|
287
|
-
city: 'Boston',
|
|
288
|
-
state: 'MA',
|
|
289
|
-
zip_code: '02101'
|
|
290
|
-
});
|
|
153
|
+
## Usage Examples
|
|
291
154
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
lead_type: 'quote',
|
|
295
|
-
email: 'customer@example.com'
|
|
296
|
-
});
|
|
297
|
-
// Step 2: Address info (automatically merges with saved lead)
|
|
298
|
-
await tracker.trackLeadAndAddress(undefined, {
|
|
299
|
-
full_address: '789 Pine St, Chicago, IL 60601',
|
|
300
|
-
source: 'quote'
|
|
301
|
-
});
|
|
302
|
-
```
|
|
155
|
+
```javascript
|
|
156
|
+
import { useLeadTracker, usePostHog } from '@aacigroup/aaci_shared/react';
|
|
303
157
|
|
|
304
|
-
|
|
158
|
+
function MyComponent() {
|
|
159
|
+
const tracker = useLeadTracker();
|
|
160
|
+
const analytics = usePostHog();
|
|
305
161
|
|
|
306
|
-
|
|
162
|
+
// 1. Track lead only - Basic contact form
|
|
163
|
+
const handleContactForm = async (formData) => {
|
|
164
|
+
await tracker.trackLeadAndAddress({
|
|
165
|
+
lead_type: 'contact',
|
|
166
|
+
email: formData.email,
|
|
167
|
+
first_name: formData.firstName
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
analytics.trackEvent('contact_form_submitted');
|
|
171
|
+
};
|
|
307
172
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
173
|
+
// 2. Track address only - Address verification
|
|
174
|
+
const handleAddressCheck = async (address) => {
|
|
175
|
+
await tracker.trackLeadAndAddress(undefined, {
|
|
176
|
+
full_address: address,
|
|
177
|
+
source: 'address_check',
|
|
178
|
+
city: 'New York',
|
|
179
|
+
state: 'NY'
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
analytics.trackEvent('address_checked');
|
|
183
|
+
};
|
|
312
184
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
185
|
+
// 3. Track both together - Policy review with address
|
|
186
|
+
const handlePolicyReview = async (leadData, addressData) => {
|
|
187
|
+
await tracker.trackLeadAndAddress({
|
|
188
|
+
lead_type: 'policy_review',
|
|
189
|
+
email: leadData.email,
|
|
190
|
+
phone: leadData.phone,
|
|
191
|
+
extra_data: { policy_number: leadData.policyNumber }
|
|
192
|
+
}, {
|
|
193
|
+
full_address: addressData.fullAddress,
|
|
194
|
+
source: 'policy_review',
|
|
195
|
+
zip_code: addressData.zipCode
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
analytics.trackEvent('policy_review_started', {
|
|
199
|
+
has_phone: !!leadData.phone
|
|
200
|
+
});
|
|
201
|
+
};
|
|
319
202
|
|
|
320
|
-
//
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
console.error('Invalid lead data:', error.errors);
|
|
329
|
-
}
|
|
203
|
+
// 4. Multi-step forms
|
|
204
|
+
const handleStepOne = async (leadData) => {
|
|
205
|
+
// Step 1: Collect lead info
|
|
206
|
+
await tracker.trackLeadAndAddress({
|
|
207
|
+
lead_type: 'quote',
|
|
208
|
+
email: leadData.email
|
|
209
|
+
});
|
|
210
|
+
};
|
|
330
211
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
212
|
+
const handleStepTwo = async (addressData) => {
|
|
213
|
+
// Step 2: Add address (automatically merges with saved lead)
|
|
214
|
+
await tracker.trackLeadAndAddress(undefined, {
|
|
215
|
+
full_address: addressData.address,
|
|
216
|
+
source: 'quote'
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
analytics.trackEvent('quote_completed');
|
|
220
|
+
};
|
|
341
221
|
|
|
342
|
-
|
|
343
|
-
const result = safeValidateTrackLeadAndAddress(leadData, addressData);
|
|
344
|
-
if (result.success) {
|
|
345
|
-
await tracker.trackLeadAndAddress(result.data.lead, result.data.address);
|
|
346
|
-
} else {
|
|
347
|
-
console.error('Validation errors:', result.error.errors);
|
|
222
|
+
return <div>...</div>;
|
|
348
223
|
}
|
|
349
|
-
|
|
350
|
-
// Available validation functions:
|
|
351
|
-
// validateTrackLeadParams(data) - throws on error
|
|
352
|
-
// validateAddress(data) - throws on error
|
|
353
|
-
// validateTrackLeadAndAddress(lead, address) - throws on error
|
|
354
|
-
// safeValidateTrackLeadParams(data) - returns {success, data, error}
|
|
355
|
-
// safeValidateAddress(data) - returns {success, data, error}
|
|
356
|
-
// safeValidateTrackLeadAndAddress(lead, address) - returns {success, data, error}
|
|
357
224
|
```
|
|
358
225
|
|
|
359
|
-
|
|
226
|
+
## Other Functions (Non-React)
|
|
360
227
|
|
|
361
|
-
|
|
362
|
-
interface LeadTrackingResponse {
|
|
363
|
-
status: 'ok' | 'error'; // Success or error status
|
|
364
|
-
lead_id?: string; // Generated lead ID (on success)
|
|
365
|
-
address_id?: string; // Generated address ID (if address provided)
|
|
366
|
-
is_first_submission?: boolean; // Whether this is user's first submission
|
|
367
|
-
message?: string; // Success/error message
|
|
368
|
-
errors?: Array<{ // Validation errors (if any)
|
|
369
|
-
field: string;
|
|
370
|
-
message: string;
|
|
371
|
-
}>;
|
|
372
|
-
}
|
|
373
|
-
```
|
|
228
|
+
### Direct Class Usage
|
|
374
229
|
|
|
375
|
-
|
|
230
|
+
For non-React projects or when you need more control:
|
|
376
231
|
|
|
377
232
|
```javascript
|
|
378
|
-
|
|
379
|
-
const sessionData = tracker.getSessionData();
|
|
233
|
+
import { LeadTracker, PostHog, GTM } from '@aacigroup/aaci_shared';
|
|
380
234
|
|
|
381
|
-
//
|
|
382
|
-
const
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
import { PostHog } from '@aacigroup/aaci_shared';
|
|
235
|
+
// Initialize LeadTracker
|
|
236
|
+
const tracker = new LeadTracker({
|
|
237
|
+
apiUrl: 'https://your-api.com/leads',
|
|
238
|
+
apiKey: 'your-api-key',
|
|
239
|
+
projectName: 'MyProject',
|
|
240
|
+
productionDomains: ['myproject.com']
|
|
241
|
+
});
|
|
389
242
|
|
|
243
|
+
// Initialize PostHog Analytics
|
|
390
244
|
const analytics = new PostHog({
|
|
391
|
-
apiKey: '
|
|
245
|
+
apiKey: 'phc_your_key',
|
|
392
246
|
projectName: 'MyProject',
|
|
393
|
-
productionDomains: ['myproject.com']
|
|
394
|
-
apiHost: 'https://us.i.posthog.com', // Optional: Custom PostHog host
|
|
395
|
-
autocapture: true, // Optional: Enable autocapture (default: true)
|
|
396
|
-
capturePageview: true // Optional: Enable pageview tracking (default: true)
|
|
247
|
+
productionDomains: ['myproject.com']
|
|
397
248
|
});
|
|
398
|
-
|
|
399
|
-
// Initialize PostHog
|
|
400
249
|
await analytics.init();
|
|
401
250
|
|
|
402
|
-
//
|
|
403
|
-
analytics.trackEvent('user_signup', { plan: 'premium', source: 'landing_page' });
|
|
404
|
-
|
|
405
|
-
// Identify users
|
|
406
|
-
analytics.identify('user_123', { email: 'user@example.com', plan: 'premium' });
|
|
407
|
-
|
|
408
|
-
// Track errors
|
|
409
|
-
analytics.trackError(new Error('Something went wrong'), 'checkout_process');
|
|
410
|
-
analytics.trackAPIError('https://api.example.com/users', 500, 'Internal Server Error');
|
|
411
|
-
|
|
412
|
-
// Reset user identity
|
|
413
|
-
analytics.reset();
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
### Google Tag Manager
|
|
417
|
-
|
|
418
|
-
```javascript
|
|
419
|
-
import { GTM } from '@aacigroup/aaci_shared';
|
|
420
|
-
|
|
251
|
+
// Initialize Google Tag Manager
|
|
421
252
|
const gtm = new GTM({
|
|
422
253
|
productionDomains: ['myproject.com']
|
|
423
254
|
});
|
|
424
255
|
|
|
425
|
-
//
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
item_id: 'product_123'
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
// Check if GTM is available
|
|
433
|
-
if (gtm.isAvailable()) {
|
|
434
|
-
gtm.trackEvent('page_view', { page: '/checkout' });
|
|
435
|
-
}
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
### Environment Detection
|
|
439
|
-
|
|
440
|
-
```javascript
|
|
441
|
-
import { Environment } from '@aacigroup/aaci_shared';
|
|
442
|
-
|
|
443
|
-
const env = new Environment({
|
|
444
|
-
productionDomains: ['myproject.com', 'www.myproject.com']
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
console.log('Is production:', env.isProduction());
|
|
448
|
-
console.log('Current domain:', env.getCurrentDomain());
|
|
256
|
+
// Use the same trackLeadAndAddress method
|
|
257
|
+
await tracker.trackLeadAndAddress(leadData, addressData);
|
|
258
|
+
analytics.trackEvent('custom_event', { data: 'value' });
|
|
259
|
+
gtm.trackEvent('purchase', { value: 100, currency: 'USD' });
|
|
449
260
|
```
|
|
450
261
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
### Input Types (for tracking methods)
|
|
262
|
+
### Input Validation (Optional)
|
|
454
263
|
|
|
455
|
-
|
|
456
|
-
// Lead data for tracking
|
|
457
|
-
interface TrackLeadParams {
|
|
458
|
-
lead_type: string; // Required: Type of lead
|
|
459
|
-
first_name?: string; // Optional: First name
|
|
460
|
-
last_name?: string; // Optional: Last name
|
|
461
|
-
email?: string; // Optional: Email address
|
|
462
|
-
phone?: string; // Optional: Phone number
|
|
463
|
-
extra_data?: Record<string, any>; // Optional: Additional custom data
|
|
464
|
-
session_data?: SessionData; // Optional: Session tracking data (auto-populated)
|
|
465
|
-
}
|
|
264
|
+
Validate data before tracking (requires `zod` package):
|
|
466
265
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
full_address: string; // Required: Complete address string
|
|
470
|
-
source: string; // Required: Lead type that generated this address
|
|
471
|
-
street_address?: string; // Optional: Street address component
|
|
472
|
-
city?: string; // Optional: City
|
|
473
|
-
state?: string; // Optional: State/Province
|
|
474
|
-
zip_code?: string; // Optional: ZIP/Postal code
|
|
475
|
-
county?: string; // Optional: County
|
|
476
|
-
country?: string; // Optional: Country
|
|
477
|
-
extra_data?: Record<string, any>; // Optional: Additional address data
|
|
478
|
-
}
|
|
266
|
+
```bash
|
|
267
|
+
npm install zod
|
|
479
268
|
```
|
|
480
269
|
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
project_name: string;
|
|
488
|
-
user_id: string | null;
|
|
489
|
-
first_name: string | null;
|
|
490
|
-
last_name: string | null;
|
|
491
|
-
email: string | null;
|
|
492
|
-
phone: string | null;
|
|
493
|
-
lead_type: string | null;
|
|
494
|
-
first_submission: boolean | null;
|
|
495
|
-
extra_data: Record<string, any>;
|
|
496
|
-
session_data: SessionData;
|
|
497
|
-
created_at: string;
|
|
498
|
-
updated_at: string;
|
|
499
|
-
}
|
|
270
|
+
```javascript
|
|
271
|
+
import {
|
|
272
|
+
validateTrackLeadParams,
|
|
273
|
+
validateAddress,
|
|
274
|
+
safeValidateTrackLeadAndAddress
|
|
275
|
+
} from '@aacigroup/aaci_shared';
|
|
500
276
|
|
|
501
|
-
//
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
zip: string | null;
|
|
511
|
-
source: string | null; // Lead type source (same values as lead_type)
|
|
512
|
-
status: string | null; // Processing status (e.g., 'verified', 'pending', 'invalid')
|
|
513
|
-
extra_data: Record<string, any>;
|
|
514
|
-
session_data: SessionData;
|
|
515
|
-
created_at: string;
|
|
516
|
-
updated_at: string;
|
|
277
|
+
// Validate lead data
|
|
278
|
+
try {
|
|
279
|
+
const validLead = validateTrackLeadParams({
|
|
280
|
+
lead_type: 'contact',
|
|
281
|
+
email: 'user@example.com'
|
|
282
|
+
});
|
|
283
|
+
console.log('Valid lead:', validLead);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
console.error('Invalid lead data:', error.message);
|
|
517
286
|
}
|
|
518
|
-
```
|
|
519
|
-
|
|
520
|
-
**Common `lead_type` and `source` values:**
|
|
521
|
-
- `'policy_review'` - Insurance policy review requests
|
|
522
|
-
- `'address_check'` - Property address verification
|
|
523
|
-
- `'contact'` - General contact form submissions
|
|
524
|
-
- `'signup'` - User registration/signup
|
|
525
|
-
- `'consultation'` - Service consultation requests
|
|
526
|
-
- `'quote'` - Price quote requests
|
|
527
287
|
|
|
528
|
-
|
|
288
|
+
// Safe validation (returns success/error object)
|
|
289
|
+
const result = safeValidateTrackLeadAndAddress({
|
|
290
|
+
lead: { lead_type: 'contact', email: 'user@example.com' },
|
|
291
|
+
address: { full_address: '123 Main St', source: 'contact' }
|
|
292
|
+
});
|
|
529
293
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
user_agent?: string;
|
|
535
|
-
browser?: string;
|
|
536
|
-
browser_version?: string;
|
|
537
|
-
os?: string;
|
|
538
|
-
device?: string;
|
|
539
|
-
referrer?: string;
|
|
540
|
-
utm_source?: string;
|
|
541
|
-
utm_medium?: string;
|
|
542
|
-
utm_campaign?: string;
|
|
543
|
-
landing_page?: string;
|
|
544
|
-
current_page?: string;
|
|
545
|
-
session_id?: string;
|
|
546
|
-
timestamp?: string;
|
|
547
|
-
distinct_id?: string;
|
|
548
|
-
gclid?: string; // Google Ads click ID
|
|
549
|
-
fbclid?: string; // Facebook click ID
|
|
550
|
-
fbc?: string; // Facebook browser cookie
|
|
551
|
-
fbp?: string; // Facebook pixel cookie
|
|
294
|
+
if (result.success) {
|
|
295
|
+
await tracker.trackLeadAndAddress(result.data.lead, result.data.address);
|
|
296
|
+
} else {
|
|
297
|
+
console.error('Validation failed:', result.error);
|
|
552
298
|
}
|
|
553
299
|
```
|
|
554
300
|
|
|
555
|
-
###
|
|
301
|
+
### TypeScript Types
|
|
556
302
|
|
|
557
303
|
```javascript
|
|
558
|
-
import type {
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
304
|
+
import type {
|
|
305
|
+
Lead,
|
|
306
|
+
AddressCapture,
|
|
307
|
+
TrackLeadParams,
|
|
308
|
+
Address,
|
|
309
|
+
LeadTrackingResponse
|
|
310
|
+
} from '@aacigroup/aaci_shared';
|
|
564
311
|
|
|
565
|
-
|
|
566
|
-
|
|
312
|
+
// Use types in your application
|
|
313
|
+
function processLead(lead: TrackLeadParams): Promise<LeadTrackingResponse> {
|
|
314
|
+
return tracker.trackLeadAndAddress(lead);
|
|
567
315
|
}
|
|
568
316
|
```
|
|
569
317
|
|
|
570
|
-
## Features
|
|
571
|
-
|
|
572
|
-
- **Flexible Tracking** - Track leads, addresses, or both in any combination
|
|
573
|
-
- **Smart Data Persistence** - Automatically saves and merges data across form steps
|
|
574
|
-
- **Attribution Tracking** - Captures and persists marketing attribution (gclid, fbclid, UTM)
|
|
575
|
-
- **Session Tracking** - Comprehensive browser and user session data
|
|
576
|
-
- **Production Detection** - Domain-based environment detection
|
|
577
|
-
- **TypeScript Support** - Full type definitions for all data structures
|
|
578
|
-
- **Framework Agnostic** - Works with React, Vue, vanilla JS, or any framework
|
|
579
|
-
|
|
580
318
|
## License
|
|
581
319
|
|
|
582
320
|
ISC
|