user-analytics-tracker 1.7.0 → 2.1.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/CHANGELOG.md +65 -0
- package/README.md +236 -2
- package/dist/index.cjs.js +1198 -268
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.cts +262 -23
- package/dist/index.d.ts +262 -23
- package/dist/index.esm.js +1192 -269
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
+
# [2.1.0](https://github.com/switch-org/analytics-tracker/compare/v2.0.0...v2.1.0) (2025-12-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* migrate IP geolocation to ipwho.is API with dynamic key storage ([0605f72](https://github.com/switch-org/analytics-tracker/commit/0605f7207baa590b776a3b52cbc055e98a8a22e4))
|
|
7
|
+
|
|
8
|
+
# [2.1.0](https://github.com/switch-org/analytics-tracker/compare/v2.0.0...v2.1.0) (TBD)
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* **IP Geolocation API Migration**: Migrated from ip-api.com to ipwho.is API for IP-based location tracking
|
|
13
|
+
- More comprehensive location data including continent, flag, connection details, and timezone information
|
|
14
|
+
- Dynamic key storage: All API response keys are automatically stored, including nested objects
|
|
15
|
+
- Future-proof: New fields added by the API are automatically captured without code changes
|
|
16
|
+
- No API key required: Free tier with no authentication needed
|
|
17
|
+
* **Enhanced IP Location Data**: The `IPLocation` interface now includes all fields from the ipwho.is API response
|
|
18
|
+
- New fields: `continent`, `continent_code`, `flag`, `connection`, `timezone` (with full details), `is_eu`, `postal`, `calling_code`, `capital`, `borders`
|
|
19
|
+
- Full backward compatibility: All existing fields (`lat`, `lon`, `countryCode`, etc.) remain available
|
|
20
|
+
- Access to full IP location data in analytics events via `ipLocationData` field
|
|
21
|
+
|
|
22
|
+
### Improvements
|
|
23
|
+
|
|
24
|
+
* Better IP location data coverage with additional metadata
|
|
25
|
+
* Automatic storage of all API response keys, including future additions
|
|
26
|
+
* Improved type definitions for IP location data
|
|
27
|
+
|
|
28
|
+
### Migration Notes
|
|
29
|
+
|
|
30
|
+
This upgrade is **fully backward compatible**. No code changes are required. See the [Upgrade Guide](./docs/upgrade-guide.md) for details on accessing new fields.
|
|
31
|
+
|
|
32
|
+
# [2.0.0](https://github.com/switch-org/analytics-tracker/compare/v1.7.0...v2.0.0) (2025-12-01)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Features
|
|
36
|
+
|
|
37
|
+
* major improvements - event batching, retry logic, plugins, and more ([f47b0ea](https://github.com/switch-org/analytics-tracker/commit/f47b0ea0f7bde575d4d91a2ae807fb074065c55c))
|
|
38
|
+
* major improvements - event batching, retry logic, plugins, and more ([46d0c1c](https://github.com/switch-org/analytics-tracker/commit/46d0c1c3e8435dec70fb04e99ad0902198c1486c))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### BREAKING CHANGES
|
|
42
|
+
|
|
43
|
+
* None - all changes are backward compatible
|
|
44
|
+
* None - all changes are backward compatible
|
|
45
|
+
|
|
46
|
+
# [1.8.0](https://github.com/switch-org/analytics-tracker/compare/v1.7.0...v1.8.0) (2025-01-XX)
|
|
47
|
+
|
|
48
|
+
### Features
|
|
49
|
+
|
|
50
|
+
* **Event Batching & Queue System**: Automatic event batching reduces API calls by 50-90%. Events are queued and sent in configurable batches (default: 10 events per batch, 5 second intervals)
|
|
51
|
+
* **Offline Support**: Events are persisted to localStorage and automatically sent when connection is restored
|
|
52
|
+
* **Retry Logic with Exponential Backoff**: Automatic retry mechanism for failed requests with exponential backoff (1s, 2s, 4s, 8s). Configurable max retries (default: 3)
|
|
53
|
+
* **Enhanced Logging System**: Configurable log levels (`silent`, `error`, `warn`, `info`, `debug`) with automatic dev/prod level selection
|
|
54
|
+
* **Plugin/Middleware System**: Extensible plugin architecture for event transformation, filtering, and enrichment. Supports `beforeSend`, `afterSend`, and `onError` hooks
|
|
55
|
+
* **Session Management Improvements**: Enhanced session tracking with timeout detection (default: 30 min), automatic session renewal, and session utilities
|
|
56
|
+
* **Developer Debugging Tools**: Built-in debug utilities accessible via `window.__analyticsDebug` in development mode. Includes queue inspection, manual flush, and stats
|
|
57
|
+
* **Performance Monitoring & Metrics**: Optional metrics collection tracking events sent/queued/failed, average send time, retry counts, and error history
|
|
58
|
+
* **Enhanced Configuration Options**: New config options for batching (`batchSize`, `batchInterval`, `maxQueueSize`), retry (`maxRetries`, `retryDelay`), session (`sessionTimeout`), logging (`logLevel`), and metrics (`enableMetrics`)
|
|
59
|
+
|
|
60
|
+
### Improvements
|
|
61
|
+
|
|
62
|
+
* Better error handling with graceful degradation
|
|
63
|
+
* Improved test coverage for new features
|
|
64
|
+
* Enhanced TypeScript types for all new features
|
|
65
|
+
|
|
1
66
|
# [1.7.0](https://github.com/switch-org/analytics-tracker/compare/v1.6.0...v1.7.0) (2025-11-24)
|
|
2
67
|
|
|
3
68
|
|
package/README.md
CHANGED
|
@@ -15,13 +15,21 @@ A comprehensive, lightweight analytics tracking library for React applications.
|
|
|
15
15
|
- 📍 **Location Tracking**:
|
|
16
16
|
- **IP-based location** - Requires user consent (privacy-compliant)
|
|
17
17
|
- **GPS location** - Requires explicit user consent and browser permission
|
|
18
|
-
- Includes public IP address, country, city, region, timezone
|
|
18
|
+
- Includes public IP address, country, city, region, timezone, continent, flag, connection details
|
|
19
|
+
- Dynamic key storage: All IP location API fields are automatically captured
|
|
19
20
|
- Automatic fallback from GPS to IP when GPS unavailable
|
|
20
21
|
- Consent management utilities included
|
|
21
22
|
- 🎯 **Attribution Tracking**: UTM parameters, referrer tracking, first/last touch attribution
|
|
22
23
|
- 📊 **IP Geolocation**: Client-side and server-side IP-based location detection utilities
|
|
23
24
|
- 🔒 **Privacy-First**: User consent required for location tracking (GPS & IP), consent management utilities
|
|
24
25
|
- 🎯 **Custom Event Tracking**: Firebase/Google Analytics-style event tracking with automatic context collection
|
|
26
|
+
- ⚡ **Event Batching & Queue System**: Automatic event batching reduces API calls by 50-90%. Events are queued and sent in configurable batches with offline persistence
|
|
27
|
+
- 🔄 **Retry Logic**: Automatic retry with exponential backoff for failed requests. Configurable retry attempts and delays
|
|
28
|
+
- 📝 **Enhanced Logging**: Configurable log levels (silent, error, warn, info, debug) with automatic dev/prod level selection
|
|
29
|
+
- 🔌 **Plugin System**: Extensible plugin architecture for event transformation, filtering, and enrichment
|
|
30
|
+
- 📈 **Session Management**: Enhanced session tracking with timeout detection and automatic renewal
|
|
31
|
+
- 🐛 **Debug Tools**: Built-in debugging utilities for development (queue inspection, manual flush, stats)
|
|
32
|
+
- 📊 **Performance Metrics**: Optional metrics collection for monitoring events, retries, and performance
|
|
25
33
|
- ⚡ **Lightweight**: Zero runtime dependencies (except React)
|
|
26
34
|
- 📦 **TypeScript**: Fully typed with comprehensive type definitions
|
|
27
35
|
- 🎨 **Framework Agnostic Core**: Core detectors work without React
|
|
@@ -59,6 +67,36 @@ function App() {
|
|
|
59
67
|
}
|
|
60
68
|
```
|
|
61
69
|
|
|
70
|
+
### Advanced Configuration Options
|
|
71
|
+
|
|
72
|
+
The package now supports extensive configuration options for batching, retry logic, logging, and more:
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
const analytics = useAnalytics({
|
|
76
|
+
config: {
|
|
77
|
+
apiEndpoint: 'https://api.yourcompany.com/analytics',
|
|
78
|
+
|
|
79
|
+
// Event batching configuration
|
|
80
|
+
batchSize: 10, // Events per batch (default: 10)
|
|
81
|
+
batchInterval: 5000, // Flush interval in ms (default: 5000)
|
|
82
|
+
maxQueueSize: 100, // Max queued events (default: 100)
|
|
83
|
+
|
|
84
|
+
// Retry configuration
|
|
85
|
+
maxRetries: 3, // Max retry attempts (default: 3)
|
|
86
|
+
retryDelay: 1000, // Initial retry delay in ms (default: 1000)
|
|
87
|
+
|
|
88
|
+
// Session configuration
|
|
89
|
+
sessionTimeout: 1800000, // Session timeout in ms (default: 30 min)
|
|
90
|
+
|
|
91
|
+
// Logging configuration
|
|
92
|
+
logLevel: 'warn', // 'silent' | 'error' | 'warn' | 'info' | 'debug' (default: 'warn')
|
|
93
|
+
|
|
94
|
+
// Metrics configuration
|
|
95
|
+
enableMetrics: false, // Enable metrics collection (default: false)
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
62
100
|
### Configuration Options
|
|
63
101
|
|
|
64
102
|
You can configure your backend URL in three ways:
|
|
@@ -272,6 +310,32 @@ interface UseAnalyticsOptions {
|
|
|
272
310
|
attribution: AttributionInfo;
|
|
273
311
|
}) => void; // Callback when data is ready
|
|
274
312
|
}
|
|
313
|
+
|
|
314
|
+
interface AnalyticsConfig {
|
|
315
|
+
apiEndpoint: string;
|
|
316
|
+
// Batching options
|
|
317
|
+
batchSize?: number; // Events per batch (default: 10)
|
|
318
|
+
batchInterval?: number; // Flush interval in ms (default: 5000)
|
|
319
|
+
maxQueueSize?: number; // Max queued events (default: 100)
|
|
320
|
+
// Retry options
|
|
321
|
+
maxRetries?: number; // Max retry attempts (default: 3)
|
|
322
|
+
retryDelay?: number; // Initial retry delay in ms (default: 1000)
|
|
323
|
+
// Session options
|
|
324
|
+
sessionTimeout?: number; // Session timeout in ms (default: 1800000 = 30 min)
|
|
325
|
+
// Logging options
|
|
326
|
+
logLevel?: LogLevel; // 'silent' | 'error' | 'warn' | 'info' | 'debug' (default: 'warn')
|
|
327
|
+
// Metrics options
|
|
328
|
+
enableMetrics?: boolean; // Enable metrics collection (default: false)
|
|
329
|
+
// Existing options
|
|
330
|
+
autoSend?: boolean;
|
|
331
|
+
enableLocation?: boolean;
|
|
332
|
+
enableIPGeolocation?: boolean;
|
|
333
|
+
enableNetworkDetection?: boolean;
|
|
334
|
+
enableDeviceDetection?: boolean;
|
|
335
|
+
enableAttribution?: boolean;
|
|
336
|
+
sessionStoragePrefix?: string;
|
|
337
|
+
localStoragePrefix?: string;
|
|
338
|
+
}
|
|
275
339
|
```
|
|
276
340
|
|
|
277
341
|
#### Returns
|
|
@@ -416,6 +480,26 @@ const attribution = AttributionDetector.detect();
|
|
|
416
480
|
|
|
417
481
|
### Services
|
|
418
482
|
|
|
483
|
+
#### `AnalyticsService.configure()`
|
|
484
|
+
|
|
485
|
+
Configure the analytics service with advanced options.
|
|
486
|
+
|
|
487
|
+
```typescript
|
|
488
|
+
import { AnalyticsService } from 'user-analytics-tracker';
|
|
489
|
+
|
|
490
|
+
AnalyticsService.configure({
|
|
491
|
+
apiEndpoint: 'https://api.yourcompany.com/analytics',
|
|
492
|
+
batchSize: 20, // Events per batch (default: 10)
|
|
493
|
+
batchInterval: 10000, // Flush interval in ms (default: 5000)
|
|
494
|
+
maxQueueSize: 100, // Max queued events (default: 100)
|
|
495
|
+
maxRetries: 5, // Max retry attempts (default: 3)
|
|
496
|
+
retryDelay: 2000, // Initial retry delay in ms (default: 1000)
|
|
497
|
+
sessionTimeout: 1800000, // Session timeout in ms (default: 30 min)
|
|
498
|
+
logLevel: 'info', // Logging verbosity (default: 'warn')
|
|
499
|
+
enableMetrics: true, // Enable metrics collection (default: false)
|
|
500
|
+
});
|
|
501
|
+
```
|
|
502
|
+
|
|
419
503
|
#### `AnalyticsService.trackUserJourney()`
|
|
420
504
|
|
|
421
505
|
Send analytics data to your backend.
|
|
@@ -443,8 +527,111 @@ await AnalyticsService.trackUserJourney({
|
|
|
443
527
|
});
|
|
444
528
|
```
|
|
445
529
|
|
|
530
|
+
#### `AnalyticsService.flushQueue()`
|
|
531
|
+
|
|
532
|
+
Manually flush the event queue (useful before page unload).
|
|
533
|
+
|
|
534
|
+
```typescript
|
|
535
|
+
// Flush all queued events immediately
|
|
536
|
+
await AnalyticsService.flushQueue();
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
#### `AnalyticsService.getQueueSize()`
|
|
540
|
+
|
|
541
|
+
Get the current number of events in the queue.
|
|
542
|
+
|
|
543
|
+
```typescript
|
|
544
|
+
const size = AnalyticsService.getQueueSize();
|
|
545
|
+
console.log(`Queue has ${size} events`);
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
#### `AnalyticsService.getMetrics()`
|
|
549
|
+
|
|
550
|
+
Get performance metrics (if enabled).
|
|
551
|
+
|
|
552
|
+
```typescript
|
|
553
|
+
const metrics = AnalyticsService.getMetrics();
|
|
554
|
+
if (metrics) {
|
|
555
|
+
console.log(`Sent: ${metrics.eventsSent}, Failed: ${metrics.eventsFailed}`);
|
|
556
|
+
}
|
|
557
|
+
```
|
|
558
|
+
|
|
446
559
|
### Utilities
|
|
447
560
|
|
|
561
|
+
#### Logger
|
|
562
|
+
|
|
563
|
+
Configure logging levels for better debugging and production use.
|
|
564
|
+
|
|
565
|
+
```typescript
|
|
566
|
+
import { logger } from 'user-analytics-tracker';
|
|
567
|
+
|
|
568
|
+
// Set log level
|
|
569
|
+
logger.setLevel('debug'); // 'silent' | 'error' | 'warn' | 'info' | 'debug'
|
|
570
|
+
|
|
571
|
+
// Use logger
|
|
572
|
+
logger.debug('Debug message');
|
|
573
|
+
logger.info('Info message');
|
|
574
|
+
logger.warn('Warning message');
|
|
575
|
+
logger.error('Error message');
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
#### Plugin Manager
|
|
579
|
+
|
|
580
|
+
Register and manage plugins for event transformation.
|
|
581
|
+
|
|
582
|
+
```typescript
|
|
583
|
+
import { pluginManager } from 'user-analytics-tracker';
|
|
584
|
+
|
|
585
|
+
// Register a plugin
|
|
586
|
+
pluginManager.register({
|
|
587
|
+
name: 'my-plugin',
|
|
588
|
+
beforeSend: (event) => {
|
|
589
|
+
// Transform event
|
|
590
|
+
return event;
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
// Unregister a plugin
|
|
595
|
+
pluginManager.unregister('my-plugin');
|
|
596
|
+
|
|
597
|
+
// Get all plugins
|
|
598
|
+
const plugins = pluginManager.getPlugins();
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
#### Queue Manager
|
|
602
|
+
|
|
603
|
+
Advanced queue management (for power users).
|
|
604
|
+
|
|
605
|
+
```typescript
|
|
606
|
+
import { QueueManager } from 'user-analytics-tracker';
|
|
607
|
+
|
|
608
|
+
const queue = new QueueManager({
|
|
609
|
+
batchSize: 20,
|
|
610
|
+
batchInterval: 10000,
|
|
611
|
+
maxQueueSize: 200,
|
|
612
|
+
storageKey: 'my-queue',
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
queue.setFlushCallback(async (events) => {
|
|
616
|
+
// Custom flush logic
|
|
617
|
+
});
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
#### Metrics Collector
|
|
621
|
+
|
|
622
|
+
Collect and monitor analytics performance metrics.
|
|
623
|
+
|
|
624
|
+
```typescript
|
|
625
|
+
import { metricsCollector } from 'user-analytics-tracker';
|
|
626
|
+
|
|
627
|
+
// Metrics are automatically collected when enableMetrics is true
|
|
628
|
+
// Access metrics
|
|
629
|
+
const metrics = metricsCollector.getMetrics();
|
|
630
|
+
|
|
631
|
+
// Reset metrics
|
|
632
|
+
metricsCollector.reset();
|
|
633
|
+
```
|
|
634
|
+
|
|
448
635
|
#### Location Consent Management
|
|
449
636
|
|
|
450
637
|
```typescript
|
|
@@ -468,6 +655,45 @@ setLocationConsentGranted();
|
|
|
468
655
|
clearLocationConsent();
|
|
469
656
|
```
|
|
470
657
|
|
|
658
|
+
#### Session Management
|
|
659
|
+
|
|
660
|
+
Enhanced session tracking utilities.
|
|
661
|
+
|
|
662
|
+
```typescript
|
|
663
|
+
import {
|
|
664
|
+
getOrCreateSession,
|
|
665
|
+
updateSessionActivity,
|
|
666
|
+
getSession,
|
|
667
|
+
clearSession,
|
|
668
|
+
} from 'user-analytics-tracker';
|
|
669
|
+
|
|
670
|
+
// Get or create session with custom timeout (30 minutes)
|
|
671
|
+
const session = getOrCreateSession(30 * 60 * 1000);
|
|
672
|
+
// Returns: { sessionId, startTime, lastActivity, pageViews }
|
|
673
|
+
|
|
674
|
+
// Update session activity
|
|
675
|
+
updateSessionActivity();
|
|
676
|
+
|
|
677
|
+
// Get current session
|
|
678
|
+
const currentSession = getSession();
|
|
679
|
+
|
|
680
|
+
// Clear session
|
|
681
|
+
clearSession();
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
#### Debug Utilities
|
|
685
|
+
|
|
686
|
+
Development debugging tools.
|
|
687
|
+
|
|
688
|
+
```typescript
|
|
689
|
+
import { initDebug } from 'user-analytics-tracker';
|
|
690
|
+
|
|
691
|
+
// Initialize debug tools (automatically called in development)
|
|
692
|
+
initDebug();
|
|
693
|
+
|
|
694
|
+
// Then access via window.__analyticsDebug in browser console
|
|
695
|
+
```
|
|
696
|
+
|
|
471
697
|
#### IP Geolocation Utilities
|
|
472
698
|
|
|
473
699
|
**Client-Side: Get Public IP**
|
|
@@ -630,6 +856,14 @@ export async function POST(req: NextRequest) {
|
|
|
630
856
|
|
|
631
857
|
Comprehensive documentation is available in the [`docs/`](./docs) directory:
|
|
632
858
|
|
|
859
|
+
- **[Upgrade Guide](./docs/upgrade-guide.md)** - Step-by-step migration instructions for upgrading between versions
|
|
860
|
+
|
|
861
|
+
- **[Upgrade Guide](./docs/upgrade-guide.md)** - Step-by-step migration instructions for upgrading between versions
|
|
862
|
+
- Breaking changes and compatibility notes
|
|
863
|
+
- New features and improvements
|
|
864
|
+
- Migration examples
|
|
865
|
+
- Troubleshooting upgrade issues
|
|
866
|
+
|
|
633
867
|
- **[Usage Guide](./docs/usage-guide.md)** - Complete guide on how to use the package in your applications
|
|
634
868
|
- Installation instructions
|
|
635
869
|
- Basic and advanced usage examples
|
|
@@ -739,7 +973,7 @@ MIT © [Switch Org](https://github.com/switch-org)
|
|
|
739
973
|
|
|
740
974
|
## 🙏 Acknowledgments
|
|
741
975
|
|
|
742
|
-
- Uses [
|
|
976
|
+
- Uses [ipwho.is](https://ipwho.is/) for free IP geolocation
|
|
743
977
|
- Built with modern web APIs (User-Agent Client Hints, Network Information API, Geolocation API)
|
|
744
978
|
|
|
745
979
|
<!-- ## 📞 Support
|