@aacigroup/aaci_shared 2.4.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 CHANGED
@@ -1,65 +1,73 @@
1
1
  # AACI Shared Library
2
2
 
3
- Simple TypeScript/JavaScript library for tracking and analytics in AACI Group projects.
3
+ React Context-based tracking library for AACI Group projects with lead capture and analytics.
4
4
 
5
5
  [![npm version](https://badge.fury.io/js/@aacigroup%2Faaci_shared.svg)](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
- ## Quick Start
13
+ ## React Context Setup
14
14
 
15
- ```javascript
16
- import { LeadTracker } from '@aacigroup/aaci_shared';
17
-
18
- const leadTracker = 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
- // Track lead only
26
- await leadTracker.trackLeadAndAddress({
27
- lead_type: 'signup',
28
- email: 'user@example.com'
29
- });
17
+ Add these to your `.env` file:
30
18
 
31
- // Track address only
32
- await leadTracker.trackLeadAndAddress(undefined, {
33
- full_address: '123 Main St, New York, NY 10001',
34
- source: 'address_check'
35
- });
36
-
37
- // Track both together
38
- await leadTracker.trackLeadAndAddress({
39
- lead_type: 'contact',
40
- email: 'user@example.com'
41
- }, {
42
- full_address: '123 Main St, New York, NY 10001',
43
- source: 'contact'
44
- });
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
45
25
  ```
46
26
 
47
- ## Core API Reference
48
-
49
- ### LeadTracker
27
+ ### App Setup
50
28
 
51
- #### `trackLeadAndAddress(lead?, address?)`
29
+ ```javascript
30
+ // App.tsx
31
+ import { TrackingProvider, createTrackingConfigFromEnv } from '@aacigroup/aaci_shared/react';
32
+
33
+ function App() {
34
+ return (
35
+ <TrackingProvider config={createTrackingConfigFromEnv()}>
36
+ <YourApp />
37
+ </TrackingProvider>
38
+ );
39
+ }
40
+ ```
52
41
 
53
- **Primary tracking method that accepts lead data, address data, or both.**
42
+ ### Component Usage
54
43
 
55
- **Parameters:**
56
- - `lead` (optional): Lead information object
57
- - `address` (optional): Address information object
58
- - **At least one parameter is required**
44
+ ```javascript
45
+ // ContactForm.tsx
46
+ import { useLeadTracker, usePostHog } from '@aacigroup/aaci_shared/react';
47
+
48
+ function ContactForm() {
49
+ const tracker = useLeadTracker();
50
+ const analytics = usePostHog();
51
+
52
+ const handleSubmit = async (formData) => {
53
+ // Track lead submission
54
+ await tracker.trackLeadAndAddress({
55
+ lead_type: 'contact',
56
+ email: formData.email,
57
+ first_name: formData.firstName
58
+ });
59
+
60
+ // Track analytics event
61
+ analytics.trackEvent('form_submitted', { form_type: 'contact' });
62
+ };
63
+
64
+ return <form onSubmit={handleSubmit}>...</form>;
65
+ }
66
+ ```
59
67
 
60
- **Returns:** `Promise<LeadTrackingResponse>`
68
+ ## Data Types
61
69
 
62
- #### Lead Capture Types
70
+ ### Lead Data
63
71
 
64
72
  ```typescript
65
73
  interface TrackLeadParams {
@@ -69,11 +77,10 @@ interface TrackLeadParams {
69
77
  email?: string; // Optional: Email address
70
78
  phone?: string; // Optional: Phone number
71
79
  extra_data?: Record<string, any>; // Optional: Additional custom data
72
- session_data?: SessionData; // Optional: Session tracking data (auto-populated)
73
80
  }
74
81
  ```
75
82
 
76
- **Common `lead_type` examples:**
83
+ **Common `lead_type` examples (free string):**
77
84
  - `'policy_review'` - Insurance policy review requests
78
85
  - `'address_check'` - Property address verification
79
86
  - `'contact'` - General contact form submissions
@@ -81,11 +88,13 @@ interface TrackLeadParams {
81
88
  - `'consultation'` - Service consultation requests
82
89
  - `'quote'` - Price quote requests
83
90
 
84
- **Lead Validation:**
85
- - When tracking a lead, either `email` OR `phone` is required
91
+ Note: `lead_type` is a free string - you can use any value that makes sense for your application.
92
+
93
+ **Validation Rules:**
86
94
  - `lead_type` is always required
95
+ - Either `email` OR `phone` is required when tracking a lead
87
96
 
88
- #### Address Capture Types
97
+ ### Address Data
89
98
 
90
99
  ```typescript
91
100
  interface Address {
@@ -101,66 +110,159 @@ interface Address {
101
110
  }
102
111
  ```
103
112
 
104
- **Address Validation:**
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:**
105
150
  - `full_address` is always required
106
- - `source` is always required (uses same values as `lead_type`)
151
+ - `source` is always required
107
152
 
108
- #### Usage Examples
153
+ ## Usage Examples
109
154
 
110
155
  ```javascript
111
- // 1. Lead only - Basic contact form
112
- await tracker.trackLeadAndAddress({
113
- lead_type: 'contact',
114
- email: 'user@example.com',
115
- first_name: 'John'
116
- });
156
+ import { useLeadTracker, usePostHog } from '@aacigroup/aaci_shared/react';
157
+
158
+ function MyComponent() {
159
+ const tracker = useLeadTracker();
160
+ const analytics = usePostHog();
161
+
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
+ };
172
+
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
+ ```
117
225
 
118
- // 2. Lead only - Policy review request
119
- await tracker.trackLeadAndAddress({
120
- lead_type: 'policy_review',
121
- phone: '+1234567890',
122
- extra_data: { policy_number: 'POL123456' }
123
- });
226
+ ## Other Functions (Non-React)
124
227
 
125
- // 3. Address only - Address verification
126
- await tracker.trackLeadAndAddress(undefined, {
127
- full_address: '123 Main St, New York, NY 10001',
128
- source: 'address_check',
129
- city: 'New York',
130
- state: 'NY',
131
- zip_code: '10001'
132
- });
228
+ ### Direct Class Usage
229
+
230
+ For non-React projects or when you need more control:
133
231
 
134
- // 4. Both together - Address check with contact
135
- await tracker.trackLeadAndAddress({
136
- lead_type: 'address_check',
137
- email: 'homeowner@example.com',
138
- first_name: 'Jane'
139
- }, {
140
- full_address: '456 Oak Ave, Boston, MA 02101',
141
- source: 'address_check',
142
- city: 'Boston',
143
- state: 'MA',
144
- zip_code: '02101'
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']
145
241
  });
146
242
 
147
- // 5. Multi-step forms - Step 1: Lead info
148
- await tracker.trackLeadAndAddress({
149
- lead_type: 'quote',
150
- email: 'customer@example.com'
243
+ // Initialize PostHog Analytics
244
+ const analytics = new PostHog({
245
+ apiKey: 'phc_your_key',
246
+ projectName: 'MyProject',
247
+ productionDomains: ['myproject.com']
151
248
  });
152
- // Step 2: Address info (automatically merges with saved lead)
153
- await tracker.trackLeadAndAddress(undefined, {
154
- full_address: '789 Pine St, Chicago, IL 60601',
155
- source: 'quote'
249
+ await analytics.init();
250
+
251
+ // Initialize Google Tag Manager
252
+ const gtm = new GTM({
253
+ productionDomains: ['myproject.com']
156
254
  });
255
+
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' });
157
260
  ```
158
261
 
159
- ## Input Validation
262
+ ### Input Validation (Optional)
160
263
 
161
- The library provides Zod validation schemas to validate customer input before tracking.
264
+ Validate data before tracking (requires `zod` package):
162
265
 
163
- **Note:** Validation functions require the `zod` package to be installed in your project:
164
266
  ```bash
165
267
  npm install zod
166
268
  ```
@@ -172,234 +274,47 @@ import {
172
274
  safeValidateTrackLeadAndAddress
173
275
  } from '@aacigroup/aaci_shared';
174
276
 
175
- // Validate lead data (throws on error)
277
+ // Validate lead data
176
278
  try {
177
279
  const validLead = validateTrackLeadParams({
178
280
  lead_type: 'contact',
179
281
  email: 'user@example.com'
180
282
  });
181
- await tracker.trackLeadAndAddress(validLead);
182
- } catch (error) {
183
- console.error('Invalid lead data:', error.errors);
184
- }
185
-
186
- // Validate address data (throws on error)
187
- try {
188
- const validAddress = validateAddress({
189
- full_address: '123 Main St, New York, NY 10001',
190
- source: 'address_check'
191
- });
192
- await tracker.trackLeadAndAddress(undefined, validAddress);
283
+ console.log('Valid lead:', validLead);
193
284
  } catch (error) {
194
- console.error('Invalid address data:', error.errors);
285
+ console.error('Invalid lead data:', error.message);
195
286
  }
196
287
 
197
288
  // Safe validation (returns success/error object)
198
- const result = safeValidateTrackLeadAndAddress(leadData, addressData);
289
+ const result = safeValidateTrackLeadAndAddress({
290
+ lead: { lead_type: 'contact', email: 'user@example.com' },
291
+ address: { full_address: '123 Main St', source: 'contact' }
292
+ });
293
+
199
294
  if (result.success) {
200
295
  await tracker.trackLeadAndAddress(result.data.lead, result.data.address);
201
296
  } else {
202
- console.error('Validation errors:', result.error.errors);
297
+ console.error('Validation failed:', result.error);
203
298
  }
204
-
205
- // Available validation functions:
206
- // validateTrackLeadParams(data) - throws on error
207
- // validateAddress(data) - throws on error
208
- // validateTrackLeadAndAddress(lead, address) - throws on error
209
- // safeValidateTrackLeadParams(data) - returns {success, data, error}
210
- // safeValidateAddress(data) - returns {success, data, error}
211
- // safeValidateTrackLeadAndAddress(lead, address) - returns {success, data, error}
212
299
  ```
213
300
 
214
- #### Response Type
215
-
216
- ```typescript
217
- interface LeadTrackingResponse {
218
- status: 'ok' | 'error'; // Success or error status
219
- lead_id?: string; // Generated lead ID (on success)
220
- address_id?: string; // Generated address ID (if address provided)
221
- is_first_submission?: boolean; // Whether this is user's first submission
222
- message?: string; // Success/error message
223
- errors?: Array<{ // Validation errors (if any)
224
- field: string;
225
- message: string;
226
- }>;
227
- }
228
- ```
229
-
230
- #### Other LeadTracker Methods
231
-
232
- ```javascript
233
- // Get current session data
234
- const sessionData = tracker.getSessionData();
235
-
236
- // Check if running in production
237
- const isProduction = tracker.isProduction();
238
- ```
239
-
240
- ### PostHog Analytics
301
+ ### TypeScript Types
241
302
 
242
303
  ```javascript
243
- import { PostHog } from '@aacigroup/aaci_shared';
244
-
245
- const analytics = new PostHog({
246
- apiKey: 'phc_your_posthog_key',
247
- projectName: 'MyProject',
248
- productionDomains: ['myproject.com'],
249
- apiHost: 'https://us.i.posthog.com', // Optional: Custom PostHog host
250
- autocapture: true, // Optional: Enable autocapture (default: true)
251
- capturePageview: true // Optional: Enable pageview tracking (default: true)
252
- });
253
-
254
- // Initialize PostHog
255
- await analytics.init();
256
-
257
- // Track events
258
- analytics.trackEvent('user_signup', { plan: 'premium', source: 'landing_page' });
259
-
260
- // Identify users
261
- analytics.identify('user_123', { email: 'user@example.com', plan: 'premium' });
262
-
263
- // Track errors
264
- analytics.trackError(new Error('Something went wrong'), 'checkout_process');
265
- analytics.trackAPIError('https://api.example.com/users', 500, 'Internal Server Error');
266
-
267
- // Reset user identity
268
- analytics.reset();
269
- ```
270
-
271
- ### Google Tag Manager
272
-
273
- ```javascript
274
- import { GTM } from '@aacigroup/aaci_shared';
275
-
276
- const gtm = new GTM({
277
- productionDomains: ['myproject.com']
278
- });
279
-
280
- // Track events
281
- gtm.trackEvent('purchase', {
282
- value: 99.99,
283
- currency: 'USD',
284
- item_id: 'product_123'
285
- });
286
-
287
- // Check if GTM is available
288
- if (gtm.isAvailable()) {
289
- gtm.trackEvent('page_view', { page: '/checkout' });
290
- }
291
- ```
292
-
293
- ### Environment Detection
294
-
295
- ```javascript
296
- import { Environment } from '@aacigroup/aaci_shared';
297
-
298
- const env = new Environment({
299
- productionDomains: ['myproject.com', 'www.myproject.com']
300
- });
301
-
302
- console.log('Is production:', env.isProduction());
303
- console.log('Current domain:', env.getCurrentDomain());
304
- ```
305
-
306
- ## Data Types Reference
307
-
308
- ### Core Data Types
309
-
310
- ```typescript
311
- // Complete lead record (from database)
312
- interface Lead {
313
- id: string;
314
- project_name: string;
315
- user_id: string | null;
316
- first_name: string | null;
317
- last_name: string | null;
318
- email: string | null;
319
- phone: string | null;
320
- lead_type: string | null;
321
- first_submission: boolean | null;
322
- extra_data: Record<string, any>;
323
- session_data: SessionData;
324
- created_at: string;
325
- updated_at: string;
326
- }
327
-
328
- // Complete address record (from database)
329
- interface AddressCapture {
330
- id: string;
331
- project_name: string;
332
- user_id: string | null;
333
- lead_id: string | null;
334
- address: string | null;
335
- city: string | null;
336
- state: string | null;
337
- zip: string | null;
338
- source: string | null; // Lead type source (same values as lead_type)
339
- status: string | null; // Processing status (e.g., 'verified', 'pending', 'invalid')
340
- extra_data: Record<string, any>;
341
- session_data: SessionData;
342
- created_at: string;
343
- updated_at: string;
344
- }
345
-
346
- **Common `source` values (identical to `lead_type`):**
347
- - `'policy_review'` - Address from policy review requests
348
- - `'address_check'` - Address from address verification requests
349
- - `'contact'` - Address from general contact form submissions
350
- - `'signup'` - Address from user registration/signup
351
- - `'consultation'` - Address from service consultation requests
352
- - `'quote'` - Address from price quote requests
353
-
354
- // Session tracking data
355
- interface SessionData {
356
- ip?: string;
357
- user_agent?: string;
358
- browser?: string;
359
- browser_version?: string;
360
- os?: string;
361
- device?: string;
362
- referrer?: string;
363
- utm_source?: string;
364
- utm_medium?: string;
365
- utm_campaign?: string;
366
- landing_page?: string;
367
- current_page?: string;
368
- session_id?: string;
369
- timestamp?: string;
370
- distinct_id?: string;
371
- gclid?: string; // Google Ads click ID
372
- fbclid?: string; // Facebook click ID
373
- fbc?: string; // Facebook browser cookie
374
- fbp?: string; // Facebook pixel cookie
375
- }
376
- ```
377
-
378
- ### Import Types Only
379
-
380
- ```javascript
381
- import type { Lead, AddressCapture, SessionData, TrackLeadParams, Address } from '@aacigroup/aaci_shared';
382
-
383
- // Use in your application
384
- function saveLead(lead: Lead): Promise<void> {
385
- return database.leads.create(lead);
386
- }
304
+ import type {
305
+ Lead,
306
+ AddressCapture,
307
+ TrackLeadParams,
308
+ Address,
309
+ LeadTrackingResponse
310
+ } from '@aacigroup/aaci_shared';
387
311
 
388
- function saveAddress(address: AddressCapture): Promise<void> {
389
- return database.addresses.create(address);
312
+ // Use types in your application
313
+ function processLead(lead: TrackLeadParams): Promise<LeadTrackingResponse> {
314
+ return tracker.trackLeadAndAddress(lead);
390
315
  }
391
316
  ```
392
317
 
393
- ## Features
394
-
395
- - **Flexible Tracking** - Track leads, addresses, or both in any combination
396
- - **Smart Data Persistence** - Automatically saves and merges data across form steps
397
- - **Attribution Tracking** - Captures and persists marketing attribution (gclid, fbclid, UTM)
398
- - **Session Tracking** - Comprehensive browser and user session data
399
- - **Production Detection** - Domain-based environment detection
400
- - **TypeScript Support** - Full type definitions for all data structures
401
- - **Framework Agnostic** - Works with React, Vue, vanilla JS, or any framework
402
-
403
318
  ## License
404
319
 
405
320
  ISC
@@ -0,0 +1,19 @@
1
+ import { ReactNode } from 'react';
2
+ import { LeadTracker, PostHog } from './index';
3
+ interface TrackingConfig {
4
+ apiUrl: string;
5
+ apiKey: string;
6
+ projectName: string;
7
+ posthogKey: string;
8
+ productionDomains: string[];
9
+ }
10
+ export type { TrackingConfig };
11
+ interface TrackingProviderProps {
12
+ children: ReactNode;
13
+ config: TrackingConfig;
14
+ }
15
+ export declare const TrackingProvider: ({ children, config }: TrackingProviderProps) => import("react/jsx-runtime").JSX.Element;
16
+ export declare const useLeadTracker: () => LeadTracker;
17
+ export declare const usePostHog: () => PostHog;
18
+ export declare const createTrackingConfigFromEnv: () => TrackingConfig;
19
+ //# sourceMappingURL=TrackingContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TrackingContext.d.ts","sourceRoot":"","sources":["../src/TrackingContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAA6B,SAAS,EAAW,MAAM,OAAO,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAS/C,UAAU,cAAc;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B;AAED,YAAY,EAAE,cAAc,EAAE,CAAC;AAE/B,UAAU,qBAAqB;IAC7B,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,eAAO,MAAM,gBAAgB,GAAI,sBAAsB,qBAAqB,4CA4B3E,CAAC;AAEF,eAAO,MAAM,cAAc,QAAO,WAMjC,CAAC;AAEF,eAAO,MAAM,UAAU,QAAO,OAM7B,CAAC;AAGF,eAAO,MAAM,2BAA2B,QAAO,cAc9C,CAAC"}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTrackingConfigFromEnv = exports.usePostHog = exports.useLeadTracker = exports.TrackingProvider = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const index_1 = require("./index");
7
+ const TrackingContext = (0, react_1.createContext)(undefined);
8
+ const TrackingProvider = ({ children, config }) => {
9
+ const trackers = (0, react_1.useMemo)(() => {
10
+ const leadTracker = new index_1.LeadTracker({
11
+ apiUrl: config.apiUrl,
12
+ apiKey: config.apiKey,
13
+ projectName: config.projectName,
14
+ productionDomains: config.productionDomains,
15
+ });
16
+ const posthog = new index_1.PostHog({
17
+ apiKey: config.posthogKey,
18
+ projectName: config.projectName,
19
+ productionDomains: config.productionDomains,
20
+ });
21
+ posthog.init();
22
+ return { leadTracker, posthog };
23
+ }, [config]);
24
+ return ((0, jsx_runtime_1.jsx)(TrackingContext.Provider, { value: trackers, children: children }));
25
+ };
26
+ exports.TrackingProvider = TrackingProvider;
27
+ const useLeadTracker = () => {
28
+ const context = (0, react_1.useContext)(TrackingContext);
29
+ if (!context) {
30
+ throw new Error('useLeadTracker must be used within TrackingProvider');
31
+ }
32
+ return context.leadTracker;
33
+ };
34
+ exports.useLeadTracker = useLeadTracker;
35
+ const usePostHog = () => {
36
+ const context = (0, react_1.useContext)(TrackingContext);
37
+ if (!context) {
38
+ throw new Error('usePostHog must be used within TrackingProvider');
39
+ }
40
+ return context.posthog;
41
+ };
42
+ exports.usePostHog = usePostHog;
43
+ const createTrackingConfigFromEnv = () => {
44
+ var _a;
45
+ if (typeof window !== 'undefined' && 'import' in window && 'meta' in window.import) {
46
+ const env = window.import.meta.env;
47
+ return {
48
+ apiUrl: env.VITE_LEAD_CAPTURE_API_URL,
49
+ apiKey: env.VITE_LEAD_CAPTURE_API_KEY,
50
+ projectName: env.VITE_PROJECT_NAME,
51
+ posthogKey: env.VITE_POSTHOG_KEY,
52
+ productionDomains: ((_a = env.VITE_PRODUCTION_DOMAINS) === null || _a === void 0 ? void 0 : _a.split(',').map((d) => d.trim()).filter(Boolean)) || [],
53
+ };
54
+ }
55
+ throw new Error('Environment variables not available. Please provide config manually.');
56
+ };
57
+ exports.createTrackingConfigFromEnv = createTrackingConfigFromEnv;
58
+ //# sourceMappingURL=TrackingContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TrackingContext.js","sourceRoot":"","sources":["../src/TrackingContext.tsx"],"names":[],"mappings":";;;;AAAA,iCAAsE;AACtE,mCAA+C;AAO/C,MAAM,eAAe,GAAG,IAAA,qBAAa,EAAkC,SAAS,CAAC,CAAC;AAiB3E,MAAM,gBAAgB,GAAG,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAyB,EAAE,EAAE;IAC9E,MAAM,QAAQ,GAAG,IAAA,eAAO,EAAC,GAAG,EAAE;QAE5B,MAAM,WAAW,GAAG,IAAI,mBAAW,CAAC;YAClC,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,iBAAiB,EAAE,MAAM,CAAC,iBAAiB;SAC5C,CAAC,CAAC;QAGH,MAAM,OAAO,GAAG,IAAI,eAAO,CAAC;YAC1B,MAAM,EAAE,MAAM,CAAC,UAAU;YACzB,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,iBAAiB,EAAE,MAAM,CAAC,iBAAiB;SAC5C,CAAC,CAAC;QAGH,OAAO,CAAC,IAAI,EAAE,CAAC;QAEf,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC;IAClC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEb,OAAO,CACL,uBAAC,eAAe,CAAC,QAAQ,IAAC,KAAK,EAAE,QAAQ,YACtC,QAAQ,GACgB,CAC5B,CAAC;AACJ,CAAC,CAAC;AA5BW,QAAA,gBAAgB,oBA4B3B;AAEK,MAAM,cAAc,GAAG,GAAgB,EAAE;IAC9C,MAAM,OAAO,GAAG,IAAA,kBAAU,EAAC,eAAe,CAAC,CAAC;IAC5C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IACzE,CAAC;IACD,OAAO,OAAO,CAAC,WAAW,CAAC;AAC7B,CAAC,CAAC;AANW,QAAA,cAAc,kBAMzB;AAEK,MAAM,UAAU,GAAG,GAAY,EAAE;IACtC,MAAM,OAAO,GAAG,IAAA,kBAAU,EAAC,eAAe,CAAC,CAAC;IAC5C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,OAAO,CAAC,OAAO,CAAC;AACzB,CAAC,CAAC;AANW,QAAA,UAAU,cAMrB;AAGK,MAAM,2BAA2B,GAAG,GAAmB,EAAE;;IAE9D,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,QAAQ,IAAI,MAAM,IAAI,MAAM,IAAK,MAAc,CAAC,MAAM,EAAE,CAAC;QAC5F,MAAM,GAAG,GAAI,MAAc,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;QAC5C,OAAO;YACL,MAAM,EAAE,GAAG,CAAC,yBAAyB;YACrC,MAAM,EAAE,GAAG,CAAC,yBAAyB;YACrC,WAAW,EAAE,GAAG,CAAC,iBAAiB;YAClC,UAAU,EAAE,GAAG,CAAC,gBAAgB;YAChC,iBAAiB,EAAE,CAAA,MAAA,GAAG,CAAC,uBAAuB,0CAAE,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,MAAM,CAAC,OAAO,CAAC,KAAI,EAAE;SAC9G,CAAC;IACJ,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;AAC1F,CAAC,CAAC;AAdW,QAAA,2BAA2B,+BActC"}
@@ -0,0 +1,3 @@
1
+ export { TrackingProvider, useLeadTracker, usePostHog, createTrackingConfigFromEnv } from './TrackingContext';
2
+ export type { TrackingConfig } from './TrackingContext';
3
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,UAAU,EACV,2BAA2B,EAC5B,MAAM,mBAAmB,CAAC;AAE3B,YAAY,EACV,cAAc,EACf,MAAM,mBAAmB,CAAC"}
package/dist/react.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTrackingConfigFromEnv = exports.usePostHog = exports.useLeadTracker = exports.TrackingProvider = void 0;
4
+ var TrackingContext_1 = require("./TrackingContext");
5
+ Object.defineProperty(exports, "TrackingProvider", { enumerable: true, get: function () { return TrackingContext_1.TrackingProvider; } });
6
+ Object.defineProperty(exports, "useLeadTracker", { enumerable: true, get: function () { return TrackingContext_1.useLeadTracker; } });
7
+ Object.defineProperty(exports, "usePostHog", { enumerable: true, get: function () { return TrackingContext_1.usePostHog; } });
8
+ Object.defineProperty(exports, "createTrackingConfigFromEnv", { enumerable: true, get: function () { return TrackingContext_1.createTrackingConfigFromEnv; } });
9
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":";;;AAGA,qDAK2B;AAJzB,mHAAA,gBAAgB,OAAA;AAChB,iHAAA,cAAc,OAAA;AACd,6GAAA,UAAU,OAAA;AACV,8HAAA,2BAA2B,OAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aacigroup/aaci_shared",
3
- "version": "2.4.0",
4
- "description": "Shared tracking utilities for AACI Group projects",
3
+ "version": "2.6.1",
4
+ "description": "Shared tracking utilities for AACI Group projects with React Context support",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
@@ -22,7 +22,10 @@
22
22
  "posthog",
23
23
  "gtm",
24
24
  "lead-capture",
25
- "error-tracking"
25
+ "error-tracking",
26
+ "react",
27
+ "context",
28
+ "hooks"
26
29
  ],
27
30
  "author": "AACI Group",
28
31
  "license": "ISC",
@@ -32,8 +35,18 @@
32
35
  },
33
36
  "homepage": "https://github.com/AACI-Group/aaci_shared#readme",
34
37
  "devDependencies": {
38
+ "@types/react": "^19.2.0",
39
+ "react": "^19.2.0",
35
40
  "typescript": "^5.9.3"
36
41
  },
42
+ "peerDependencies": {
43
+ "react": ">=16.8.0"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "react": {
47
+ "optional": true
48
+ }
49
+ },
37
50
  "publishConfig": {
38
51
  "access": "public",
39
52
  "registry": "https://registry.npmjs.org/"
@@ -48,6 +61,11 @@
48
61
  "require": "./dist/index.js",
49
62
  "types": "./dist/index.d.ts"
50
63
  },
64
+ "./react": {
65
+ "import": "./dist/react.js",
66
+ "require": "./dist/react.js",
67
+ "types": "./dist/react.d.ts"
68
+ },
51
69
  "./track/*": {
52
70
  "import": "./dist/track/*.js",
53
71
  "require": "./dist/track/*.js",