@aacigroup/aaci_shared 2.6.0 → 2.6.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.
- package/README.md +191 -429
- 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
|
-
});
|
|
24
|
-
|
|
25
|
-
// Track lead only
|
|
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
|
-
```
|
|
46
|
-
|
|
47
|
-
## Lead Types
|
|
15
|
+
### Environment Variables
|
|
48
16
|
|
|
49
|
-
|
|
50
|
-
- `'policy_review'` - Insurance policy review
|
|
51
|
-
- `'address_check'` - Property address verification
|
|
52
|
-
- `'contact'` - General contact forms
|
|
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';
|
|
17
|
+
Add these to your `.env` file:
|
|
66
18
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
```
|
|
151
|
-
|
|
152
|
-
### PostHog Analytics
|
|
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
|
|
68
|
+
## Data Types
|
|
191
69
|
|
|
192
|
-
|
|
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,66 +110,155 @@ interface Address {
|
|
|
246
110
|
}
|
|
247
111
|
```
|
|
248
112
|
|
|
249
|
-
|
|
113
|
+
### Session Data
|
|
114
|
+
|
|
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
|
+
```
|
|
138
|
+
|
|
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
|
|
146
|
+
|
|
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.
|
|
148
|
+
|
|
149
|
+
**Validation Rules:**
|
|
250
150
|
- `full_address` is always required
|
|
251
|
-
- `source` is always required
|
|
151
|
+
- `source` is always required
|
|
252
152
|
|
|
253
|
-
|
|
153
|
+
## Usage Examples
|
|
254
154
|
|
|
255
155
|
```javascript
|
|
256
|
-
|
|
257
|
-
await tracker.trackLeadAndAddress({
|
|
258
|
-
lead_type: 'contact',
|
|
259
|
-
email: 'user@example.com',
|
|
260
|
-
first_name: 'John'
|
|
261
|
-
});
|
|
156
|
+
import { useLeadTracker, usePostHog } from '@aacigroup/aaci_shared/react';
|
|
262
157
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
phone: '+1234567890',
|
|
267
|
-
extra_data: { policy_number: 'POL123456' }
|
|
268
|
-
});
|
|
158
|
+
function MyComponent() {
|
|
159
|
+
const tracker = useLeadTracker();
|
|
160
|
+
const analytics = usePostHog();
|
|
269
161
|
|
|
270
|
-
//
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
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
|
+
};
|
|
278
172
|
|
|
279
|
-
//
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
+
};
|
|
184
|
+
|
|
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
|
+
};
|
|
202
|
+
|
|
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
|
+
};
|
|
211
|
+
|
|
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
|
+
};
|
|
221
|
+
|
|
222
|
+
return <div>...</div>;
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Other Functions (Non-React)
|
|
291
227
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
228
|
+
### Direct Class Usage
|
|
229
|
+
|
|
230
|
+
For non-React projects or when you need more control:
|
|
231
|
+
|
|
232
|
+
```javascript
|
|
233
|
+
import { LeadTracker, PostHog, GTM } from '@aacigroup/aaci_shared';
|
|
234
|
+
|
|
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', 'www.myproject.com']
|
|
241
|
+
// productionDomains: import.meta.env.VITE_PRODUCTION_DOMAINS.split(',')
|
|
296
242
|
});
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
243
|
+
|
|
244
|
+
// Initialize PostHog Analytics
|
|
245
|
+
const analytics = new PostHog({
|
|
246
|
+
apiKey: 'phc_your_key',
|
|
247
|
+
projectName: 'MyProject',
|
|
248
|
+
productionDomains: ['myproject.com', 'www.myproject.com']
|
|
249
|
+
// productionDomains: import.meta.env.VITE_PRODUCTION_DOMAINS.split(',')
|
|
301
250
|
});
|
|
251
|
+
await analytics.init();
|
|
252
|
+
|
|
253
|
+
// Use tracking methods
|
|
254
|
+
await tracker.trackLeadAndAddress(leadData, addressData);
|
|
255
|
+
analytics.trackEvent('custom_event', { data: 'value' });
|
|
302
256
|
```
|
|
303
257
|
|
|
304
|
-
|
|
258
|
+
### Input Validation (Optional)
|
|
305
259
|
|
|
306
|
-
|
|
260
|
+
Validate data before tracking (requires `zod` package):
|
|
307
261
|
|
|
308
|
-
**Note:** Validation functions require the `zod` package to be installed in your project:
|
|
309
262
|
```bash
|
|
310
263
|
npm install zod
|
|
311
264
|
```
|
|
@@ -317,100 +270,43 @@ import {
|
|
|
317
270
|
safeValidateTrackLeadAndAddress
|
|
318
271
|
} from '@aacigroup/aaci_shared';
|
|
319
272
|
|
|
320
|
-
// Validate lead data
|
|
273
|
+
// Validate lead data
|
|
321
274
|
try {
|
|
322
275
|
const validLead = validateTrackLeadParams({
|
|
323
276
|
lead_type: 'contact',
|
|
324
277
|
email: 'user@example.com'
|
|
325
278
|
});
|
|
326
|
-
|
|
279
|
+
console.log('Valid lead:', validLead);
|
|
327
280
|
} catch (error) {
|
|
328
|
-
console.error('Invalid lead data:', error.
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// Validate address data (throws on error)
|
|
332
|
-
try {
|
|
333
|
-
const validAddress = validateAddress({
|
|
334
|
-
full_address: '123 Main St, New York, NY 10001',
|
|
335
|
-
source: 'address_check'
|
|
336
|
-
});
|
|
337
|
-
await tracker.trackLeadAndAddress(undefined, validAddress);
|
|
338
|
-
} catch (error) {
|
|
339
|
-
console.error('Invalid address data:', error.errors);
|
|
281
|
+
console.error('Invalid lead data:', error.message);
|
|
340
282
|
}
|
|
341
283
|
|
|
342
284
|
// Safe validation (returns success/error object)
|
|
343
|
-
const result = safeValidateTrackLeadAndAddress(
|
|
285
|
+
const result = safeValidateTrackLeadAndAddress({
|
|
286
|
+
lead: { lead_type: 'contact', email: 'user@example.com' },
|
|
287
|
+
address: { full_address: '123 Main St', source: 'contact' }
|
|
288
|
+
});
|
|
289
|
+
|
|
344
290
|
if (result.success) {
|
|
345
291
|
await tracker.trackLeadAndAddress(result.data.lead, result.data.address);
|
|
346
292
|
} else {
|
|
347
|
-
console.error('Validation
|
|
348
|
-
}
|
|
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
|
-
```
|
|
358
|
-
|
|
359
|
-
#### Response Type
|
|
360
|
-
|
|
361
|
-
```typescript
|
|
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
|
-
}>;
|
|
293
|
+
console.error('Validation failed:', result.error);
|
|
372
294
|
}
|
|
373
295
|
```
|
|
374
296
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
```javascript
|
|
378
|
-
// Get current session data
|
|
379
|
-
const sessionData = tracker.getSessionData();
|
|
380
|
-
|
|
381
|
-
// Check if running in production
|
|
382
|
-
const isProduction = tracker.isProduction();
|
|
383
|
-
```
|
|
384
|
-
|
|
385
|
-
### PostHog Analytics
|
|
297
|
+
### Environment Detection
|
|
386
298
|
|
|
387
299
|
```javascript
|
|
388
|
-
import {
|
|
300
|
+
import { Environment } from '@aacigroup/aaci_shared';
|
|
389
301
|
|
|
390
|
-
const
|
|
391
|
-
|
|
392
|
-
|
|
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)
|
|
302
|
+
const env = new Environment({
|
|
303
|
+
productionDomains: ['myproject.com', 'www.myproject.com']
|
|
304
|
+
// productionDomains: import.meta.env.VITE_PRODUCTION_DOMAINS.split(',')
|
|
397
305
|
});
|
|
398
306
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
// Track events
|
|
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();
|
|
307
|
+
if (env.isProduction()) {
|
|
308
|
+
// Production-specific code
|
|
309
|
+
}
|
|
414
310
|
```
|
|
415
311
|
|
|
416
312
|
### Google Tag Manager
|
|
@@ -419,164 +315,30 @@ analytics.reset();
|
|
|
419
315
|
import { GTM } from '@aacigroup/aaci_shared';
|
|
420
316
|
|
|
421
317
|
const gtm = new GTM({
|
|
422
|
-
productionDomains: ['myproject.com']
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
// Track events
|
|
426
|
-
gtm.trackEvent('purchase', {
|
|
427
|
-
value: 99.99,
|
|
428
|
-
currency: 'USD',
|
|
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
318
|
productionDomains: ['myproject.com', 'www.myproject.com']
|
|
319
|
+
// productionDomains: import.meta.env.VITE_PRODUCTION_DOMAINS.split(',')
|
|
445
320
|
});
|
|
446
321
|
|
|
447
|
-
|
|
448
|
-
console.log('Current domain:', env.getCurrentDomain());
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
## Data Types Reference
|
|
452
|
-
|
|
453
|
-
### Input Types (for tracking methods)
|
|
454
|
-
|
|
455
|
-
```typescript
|
|
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
|
-
}
|
|
466
|
-
|
|
467
|
-
// Address data for tracking
|
|
468
|
-
interface Address {
|
|
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
|
-
}
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
### Database Types (complete records)
|
|
482
|
-
|
|
483
|
-
```typescript
|
|
484
|
-
// Complete lead record (from database)
|
|
485
|
-
interface Lead {
|
|
486
|
-
id: string;
|
|
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
|
-
}
|
|
500
|
-
|
|
501
|
-
// Complete address record (from database)
|
|
502
|
-
interface AddressCapture {
|
|
503
|
-
id: string;
|
|
504
|
-
project_name: string;
|
|
505
|
-
user_id: string | null;
|
|
506
|
-
lead_id: string | null;
|
|
507
|
-
address: string | null;
|
|
508
|
-
city: string | null;
|
|
509
|
-
state: string | null;
|
|
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;
|
|
517
|
-
}
|
|
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
|
-
|
|
528
|
-
### Session Data
|
|
529
|
-
|
|
530
|
-
```typescript
|
|
531
|
-
// Session tracking data
|
|
532
|
-
interface SessionData {
|
|
533
|
-
ip?: string;
|
|
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
|
|
552
|
-
}
|
|
322
|
+
gtm.trackEvent('purchase', { value: 100, currency: 'USD' });
|
|
553
323
|
```
|
|
554
324
|
|
|
555
|
-
###
|
|
325
|
+
### TypeScript Types
|
|
556
326
|
|
|
557
327
|
```javascript
|
|
558
|
-
import type {
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
328
|
+
import type {
|
|
329
|
+
Lead,
|
|
330
|
+
AddressCapture,
|
|
331
|
+
TrackLeadParams,
|
|
332
|
+
Address,
|
|
333
|
+
LeadTrackingResponse
|
|
334
|
+
} from '@aacigroup/aaci_shared';
|
|
564
335
|
|
|
565
|
-
|
|
566
|
-
|
|
336
|
+
// Use types in your application
|
|
337
|
+
function processLead(lead: TrackLeadParams): Promise<LeadTrackingResponse> {
|
|
338
|
+
return tracker.trackLeadAndAddress(lead);
|
|
567
339
|
}
|
|
568
340
|
```
|
|
569
341
|
|
|
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
342
|
## License
|
|
581
343
|
|
|
582
344
|
ISC
|