@kitbase/events 0.1.2 → 0.1.4

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/dist/index.d.cts CHANGED
@@ -77,6 +77,140 @@ interface QueueStats {
77
77
  isFlushing: boolean;
78
78
  }
79
79
 
80
+ /**
81
+ * Bot detection module for filtering automated traffic
82
+ *
83
+ * Detects common automation tools, headless browsers, and bot user agents.
84
+ * Inspired by DataFast's bot detection approach.
85
+ */
86
+ /**
87
+ * Bot detection result
88
+ */
89
+ interface BotDetectionResult {
90
+ isBot: boolean;
91
+ reason?: string;
92
+ checks: {
93
+ webdriver: boolean;
94
+ phantomjs: boolean;
95
+ nightmare: boolean;
96
+ automationGlobals: boolean;
97
+ documentAttributes: boolean;
98
+ userAgentHeadless: boolean;
99
+ userAgentHttpClient: boolean;
100
+ missingUserAgent: boolean;
101
+ invalidEnvironment: boolean;
102
+ };
103
+ }
104
+ /**
105
+ * Configuration for bot detection
106
+ */
107
+ interface BotDetectionConfig {
108
+ /**
109
+ * Enable bot detection.
110
+ * When enabled and a bot is detected, all tracking events are blocked.
111
+ * Set to false to disable bot detection.
112
+ * @default true
113
+ */
114
+ enabled?: boolean;
115
+ /**
116
+ * Check for webdriver flag (Chrome, Firefox)
117
+ * @default true
118
+ */
119
+ checkWebdriver?: boolean;
120
+ /**
121
+ * Check for PhantomJS
122
+ * @default true
123
+ */
124
+ checkPhantomJS?: boolean;
125
+ /**
126
+ * Check for Nightmare.js
127
+ * @default true
128
+ */
129
+ checkNightmare?: boolean;
130
+ /**
131
+ * Check for automation tool globals
132
+ * @default true
133
+ */
134
+ checkAutomationGlobals?: boolean;
135
+ /**
136
+ * Check document element attributes
137
+ * @default true
138
+ */
139
+ checkDocumentAttributes?: boolean;
140
+ /**
141
+ * Check user agent for headless browsers
142
+ * @default true
143
+ */
144
+ checkUserAgentHeadless?: boolean;
145
+ /**
146
+ * Check user agent for HTTP clients/bots
147
+ * @default true
148
+ */
149
+ checkUserAgentHttpClient?: boolean;
150
+ /**
151
+ * Additional user agent patterns to detect as bots
152
+ */
153
+ additionalBotPatterns?: string[];
154
+ /**
155
+ * Callback when a bot is detected
156
+ */
157
+ onBotDetected?: (result: BotDetectionResult) => void;
158
+ }
159
+ /**
160
+ * Detect if the current visitor is a bot
161
+ *
162
+ * @param config - Bot detection configuration
163
+ * @returns Bot detection result
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * const result = detectBot();
168
+ * if (result.isBot) {
169
+ * console.log('Bot detected:', result.reason);
170
+ * }
171
+ * ```
172
+ */
173
+ declare function detectBot(config?: BotDetectionConfig): BotDetectionResult;
174
+ /**
175
+ * Quick check if current visitor is a bot
176
+ *
177
+ * @param config - Bot detection configuration
178
+ * @returns true if bot detected, false otherwise
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * if (isBot()) {
183
+ * console.log('Bot detected, skipping tracking');
184
+ * return;
185
+ * }
186
+ * ```
187
+ */
188
+ declare function isBot(config?: BotDetectionConfig): boolean;
189
+ /**
190
+ * Check a custom user agent string for bot patterns
191
+ * Useful for server-side bot detection
192
+ *
193
+ * @param userAgent - The user agent string to check
194
+ * @param additionalPatterns - Additional patterns to check
195
+ * @returns true if bot user agent detected
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * // Server-side usage
200
+ * const ua = request.headers['user-agent'];
201
+ * if (isUserAgentBot(ua)) {
202
+ * console.log('Bot request detected');
203
+ * }
204
+ * ```
205
+ */
206
+ declare function isUserAgentBot(userAgent: string, additionalPatterns?: string[]): boolean;
207
+ /**
208
+ * Get the current user agent (browser only)
209
+ *
210
+ * @returns User agent string or null if not in browser
211
+ */
212
+ declare function getUserAgent(): string | null;
213
+
80
214
  /**
81
215
  * Storage interface for anonymous ID persistence
82
216
  */
@@ -126,6 +260,11 @@ interface KitbaseConfig {
126
260
  * Enables session tracking, pageview tracking, and revenue tracking.
127
261
  */
128
262
  analytics?: AnalyticsConfig;
263
+ /**
264
+ * Bot detection configuration.
265
+ * When enabled, detects automated traffic and can block tracking for bots.
266
+ */
267
+ botDetection?: BotDetectionConfig;
129
268
  }
130
269
  /**
131
270
  * Tag values can be strings, numbers, or booleans
@@ -237,6 +376,12 @@ interface AnalyticsConfig {
237
376
  * @default true
238
377
  */
239
378
  autoTrackSessions?: boolean;
379
+ /**
380
+ * Enable automatic outbound link click tracking
381
+ * Tracks when users click links to external domains
382
+ * @default true
383
+ */
384
+ autoTrackOutboundLinks?: boolean;
240
385
  /**
241
386
  * Session timeout in milliseconds (default: 30 minutes)
242
387
  * @default 1800000
@@ -366,8 +511,12 @@ declare class Kitbase {
366
511
  private sessionStorageKey;
367
512
  private analyticsEnabled;
368
513
  private autoTrackPageViews;
514
+ private autoTrackOutboundLinks;
369
515
  private userId;
370
516
  private unloadListenerAdded;
517
+ private clickListenerAdded;
518
+ private botDetectionConfig;
519
+ private botDetectionResult;
371
520
  constructor(config: KitbaseConfig);
372
521
  /**
373
522
  * Initialize the anonymous ID from storage or generate a new one
@@ -510,6 +659,54 @@ declare class Kitbase {
510
659
  * @returns Duration in seconds, or null if not being timed
511
660
  */
512
661
  getEventDuration(eventName: string): number | null;
662
+ /**
663
+ * Check if the current visitor is detected as a bot
664
+ *
665
+ * @returns true if bot detected, false otherwise
666
+ *
667
+ * @example
668
+ * ```typescript
669
+ * if (kitbase.isBot()) {
670
+ * console.log('Bot detected, tracking disabled');
671
+ * }
672
+ * ```
673
+ */
674
+ isBot(): boolean;
675
+ /**
676
+ * Get detailed bot detection result
677
+ *
678
+ * @returns Bot detection result with detailed checks, or null if detection not enabled
679
+ *
680
+ * @example
681
+ * ```typescript
682
+ * const result = kitbase.getBotDetectionResult();
683
+ * if (result?.isBot) {
684
+ * console.log('Bot detected:', result.reason);
685
+ * console.log('Checks:', result.checks);
686
+ * }
687
+ * ```
688
+ */
689
+ getBotDetectionResult(): BotDetectionResult | null;
690
+ /**
691
+ * Force re-run bot detection
692
+ * Useful if you want to check again after page state changes
693
+ *
694
+ * @returns Updated bot detection result
695
+ *
696
+ * @example
697
+ * ```typescript
698
+ * const result = kitbase.redetectBot();
699
+ * console.log('Is bot:', result.isBot);
700
+ * ```
701
+ */
702
+ redetectBot(): BotDetectionResult;
703
+ /**
704
+ * Check if bot blocking is currently active
705
+ * When bot detection is enabled and a bot is detected, all events are blocked.
706
+ *
707
+ * @returns true if bots are being blocked from tracking
708
+ */
709
+ isBotBlockingActive(): boolean;
513
710
  /**
514
711
  * Get offline queue statistics
515
712
  *
@@ -600,6 +797,40 @@ declare class Kitbase {
600
797
  * Setup listeners for session lifecycle management
601
798
  */
602
799
  private setupUnloadListener;
800
+ /**
801
+ * Setup outbound link click tracking
802
+ */
803
+ private setupOutboundLinkTracking;
804
+ /**
805
+ * Handle link click for outbound tracking
806
+ */
807
+ private handleLinkClick;
808
+ /**
809
+ * Get root domain from hostname (e.g., blog.example.com -> example.com)
810
+ */
811
+ private getRootDomain;
812
+ /**
813
+ * Check if two hostnames share the same root domain
814
+ */
815
+ private isSameRootDomain;
816
+ /**
817
+ * Track an outbound link click
818
+ *
819
+ * @param options - Outbound link options
820
+ * @returns Promise resolving to the track response
821
+ *
822
+ * @example
823
+ * ```typescript
824
+ * await kitbase.trackOutboundLink({
825
+ * url: 'https://example.com',
826
+ * text: 'Visit Example',
827
+ * });
828
+ * ```
829
+ */
830
+ trackOutboundLink(options: {
831
+ url: string;
832
+ text?: string;
833
+ }): Promise<TrackResponse>;
603
834
  /**
604
835
  * Get UTM parameters from URL
605
836
  */
@@ -723,4 +954,4 @@ declare class TimeoutError extends KitbaseError {
723
954
  constructor(message?: string);
724
955
  }
725
956
 
726
- export { type AnalyticsConfig, ApiError, AuthenticationError, type IdentifyOptions, Kitbase, type KitbaseConfig, KitbaseError, type OfflineConfig, type PageViewOptions, type QueueStats, type QueuedEvent, type RevenueOptions, type Session, type Storage, type TagValue, type Tags, TimeoutError, type TrackOptions, type TrackResponse, ValidationError };
957
+ export { type AnalyticsConfig, ApiError, AuthenticationError, type BotDetectionConfig, type BotDetectionResult, type IdentifyOptions, Kitbase, type KitbaseConfig, KitbaseError, type OfflineConfig, type PageViewOptions, type QueueStats, type QueuedEvent, type RevenueOptions, type Session, type Storage, type TagValue, type Tags, TimeoutError, type TrackOptions, type TrackResponse, ValidationError, detectBot, getUserAgent, isBot, isUserAgentBot };
package/dist/index.d.ts CHANGED
@@ -77,6 +77,140 @@ interface QueueStats {
77
77
  isFlushing: boolean;
78
78
  }
79
79
 
80
+ /**
81
+ * Bot detection module for filtering automated traffic
82
+ *
83
+ * Detects common automation tools, headless browsers, and bot user agents.
84
+ * Inspired by DataFast's bot detection approach.
85
+ */
86
+ /**
87
+ * Bot detection result
88
+ */
89
+ interface BotDetectionResult {
90
+ isBot: boolean;
91
+ reason?: string;
92
+ checks: {
93
+ webdriver: boolean;
94
+ phantomjs: boolean;
95
+ nightmare: boolean;
96
+ automationGlobals: boolean;
97
+ documentAttributes: boolean;
98
+ userAgentHeadless: boolean;
99
+ userAgentHttpClient: boolean;
100
+ missingUserAgent: boolean;
101
+ invalidEnvironment: boolean;
102
+ };
103
+ }
104
+ /**
105
+ * Configuration for bot detection
106
+ */
107
+ interface BotDetectionConfig {
108
+ /**
109
+ * Enable bot detection.
110
+ * When enabled and a bot is detected, all tracking events are blocked.
111
+ * Set to false to disable bot detection.
112
+ * @default true
113
+ */
114
+ enabled?: boolean;
115
+ /**
116
+ * Check for webdriver flag (Chrome, Firefox)
117
+ * @default true
118
+ */
119
+ checkWebdriver?: boolean;
120
+ /**
121
+ * Check for PhantomJS
122
+ * @default true
123
+ */
124
+ checkPhantomJS?: boolean;
125
+ /**
126
+ * Check for Nightmare.js
127
+ * @default true
128
+ */
129
+ checkNightmare?: boolean;
130
+ /**
131
+ * Check for automation tool globals
132
+ * @default true
133
+ */
134
+ checkAutomationGlobals?: boolean;
135
+ /**
136
+ * Check document element attributes
137
+ * @default true
138
+ */
139
+ checkDocumentAttributes?: boolean;
140
+ /**
141
+ * Check user agent for headless browsers
142
+ * @default true
143
+ */
144
+ checkUserAgentHeadless?: boolean;
145
+ /**
146
+ * Check user agent for HTTP clients/bots
147
+ * @default true
148
+ */
149
+ checkUserAgentHttpClient?: boolean;
150
+ /**
151
+ * Additional user agent patterns to detect as bots
152
+ */
153
+ additionalBotPatterns?: string[];
154
+ /**
155
+ * Callback when a bot is detected
156
+ */
157
+ onBotDetected?: (result: BotDetectionResult) => void;
158
+ }
159
+ /**
160
+ * Detect if the current visitor is a bot
161
+ *
162
+ * @param config - Bot detection configuration
163
+ * @returns Bot detection result
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * const result = detectBot();
168
+ * if (result.isBot) {
169
+ * console.log('Bot detected:', result.reason);
170
+ * }
171
+ * ```
172
+ */
173
+ declare function detectBot(config?: BotDetectionConfig): BotDetectionResult;
174
+ /**
175
+ * Quick check if current visitor is a bot
176
+ *
177
+ * @param config - Bot detection configuration
178
+ * @returns true if bot detected, false otherwise
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * if (isBot()) {
183
+ * console.log('Bot detected, skipping tracking');
184
+ * return;
185
+ * }
186
+ * ```
187
+ */
188
+ declare function isBot(config?: BotDetectionConfig): boolean;
189
+ /**
190
+ * Check a custom user agent string for bot patterns
191
+ * Useful for server-side bot detection
192
+ *
193
+ * @param userAgent - The user agent string to check
194
+ * @param additionalPatterns - Additional patterns to check
195
+ * @returns true if bot user agent detected
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * // Server-side usage
200
+ * const ua = request.headers['user-agent'];
201
+ * if (isUserAgentBot(ua)) {
202
+ * console.log('Bot request detected');
203
+ * }
204
+ * ```
205
+ */
206
+ declare function isUserAgentBot(userAgent: string, additionalPatterns?: string[]): boolean;
207
+ /**
208
+ * Get the current user agent (browser only)
209
+ *
210
+ * @returns User agent string or null if not in browser
211
+ */
212
+ declare function getUserAgent(): string | null;
213
+
80
214
  /**
81
215
  * Storage interface for anonymous ID persistence
82
216
  */
@@ -126,6 +260,11 @@ interface KitbaseConfig {
126
260
  * Enables session tracking, pageview tracking, and revenue tracking.
127
261
  */
128
262
  analytics?: AnalyticsConfig;
263
+ /**
264
+ * Bot detection configuration.
265
+ * When enabled, detects automated traffic and can block tracking for bots.
266
+ */
267
+ botDetection?: BotDetectionConfig;
129
268
  }
130
269
  /**
131
270
  * Tag values can be strings, numbers, or booleans
@@ -237,6 +376,12 @@ interface AnalyticsConfig {
237
376
  * @default true
238
377
  */
239
378
  autoTrackSessions?: boolean;
379
+ /**
380
+ * Enable automatic outbound link click tracking
381
+ * Tracks when users click links to external domains
382
+ * @default true
383
+ */
384
+ autoTrackOutboundLinks?: boolean;
240
385
  /**
241
386
  * Session timeout in milliseconds (default: 30 minutes)
242
387
  * @default 1800000
@@ -366,8 +511,12 @@ declare class Kitbase {
366
511
  private sessionStorageKey;
367
512
  private analyticsEnabled;
368
513
  private autoTrackPageViews;
514
+ private autoTrackOutboundLinks;
369
515
  private userId;
370
516
  private unloadListenerAdded;
517
+ private clickListenerAdded;
518
+ private botDetectionConfig;
519
+ private botDetectionResult;
371
520
  constructor(config: KitbaseConfig);
372
521
  /**
373
522
  * Initialize the anonymous ID from storage or generate a new one
@@ -510,6 +659,54 @@ declare class Kitbase {
510
659
  * @returns Duration in seconds, or null if not being timed
511
660
  */
512
661
  getEventDuration(eventName: string): number | null;
662
+ /**
663
+ * Check if the current visitor is detected as a bot
664
+ *
665
+ * @returns true if bot detected, false otherwise
666
+ *
667
+ * @example
668
+ * ```typescript
669
+ * if (kitbase.isBot()) {
670
+ * console.log('Bot detected, tracking disabled');
671
+ * }
672
+ * ```
673
+ */
674
+ isBot(): boolean;
675
+ /**
676
+ * Get detailed bot detection result
677
+ *
678
+ * @returns Bot detection result with detailed checks, or null if detection not enabled
679
+ *
680
+ * @example
681
+ * ```typescript
682
+ * const result = kitbase.getBotDetectionResult();
683
+ * if (result?.isBot) {
684
+ * console.log('Bot detected:', result.reason);
685
+ * console.log('Checks:', result.checks);
686
+ * }
687
+ * ```
688
+ */
689
+ getBotDetectionResult(): BotDetectionResult | null;
690
+ /**
691
+ * Force re-run bot detection
692
+ * Useful if you want to check again after page state changes
693
+ *
694
+ * @returns Updated bot detection result
695
+ *
696
+ * @example
697
+ * ```typescript
698
+ * const result = kitbase.redetectBot();
699
+ * console.log('Is bot:', result.isBot);
700
+ * ```
701
+ */
702
+ redetectBot(): BotDetectionResult;
703
+ /**
704
+ * Check if bot blocking is currently active
705
+ * When bot detection is enabled and a bot is detected, all events are blocked.
706
+ *
707
+ * @returns true if bots are being blocked from tracking
708
+ */
709
+ isBotBlockingActive(): boolean;
513
710
  /**
514
711
  * Get offline queue statistics
515
712
  *
@@ -600,6 +797,40 @@ declare class Kitbase {
600
797
  * Setup listeners for session lifecycle management
601
798
  */
602
799
  private setupUnloadListener;
800
+ /**
801
+ * Setup outbound link click tracking
802
+ */
803
+ private setupOutboundLinkTracking;
804
+ /**
805
+ * Handle link click for outbound tracking
806
+ */
807
+ private handleLinkClick;
808
+ /**
809
+ * Get root domain from hostname (e.g., blog.example.com -> example.com)
810
+ */
811
+ private getRootDomain;
812
+ /**
813
+ * Check if two hostnames share the same root domain
814
+ */
815
+ private isSameRootDomain;
816
+ /**
817
+ * Track an outbound link click
818
+ *
819
+ * @param options - Outbound link options
820
+ * @returns Promise resolving to the track response
821
+ *
822
+ * @example
823
+ * ```typescript
824
+ * await kitbase.trackOutboundLink({
825
+ * url: 'https://example.com',
826
+ * text: 'Visit Example',
827
+ * });
828
+ * ```
829
+ */
830
+ trackOutboundLink(options: {
831
+ url: string;
832
+ text?: string;
833
+ }): Promise<TrackResponse>;
603
834
  /**
604
835
  * Get UTM parameters from URL
605
836
  */
@@ -723,4 +954,4 @@ declare class TimeoutError extends KitbaseError {
723
954
  constructor(message?: string);
724
955
  }
725
956
 
726
- export { type AnalyticsConfig, ApiError, AuthenticationError, type IdentifyOptions, Kitbase, type KitbaseConfig, KitbaseError, type OfflineConfig, type PageViewOptions, type QueueStats, type QueuedEvent, type RevenueOptions, type Session, type Storage, type TagValue, type Tags, TimeoutError, type TrackOptions, type TrackResponse, ValidationError };
957
+ export { type AnalyticsConfig, ApiError, AuthenticationError, type BotDetectionConfig, type BotDetectionResult, type IdentifyOptions, Kitbase, type KitbaseConfig, KitbaseError, type OfflineConfig, type PageViewOptions, type QueueStats, type QueuedEvent, type RevenueOptions, type Session, type Storage, type TagValue, type Tags, TimeoutError, type TrackOptions, type TrackResponse, ValidationError, detectBot, getUserAgent, isBot, isUserAgentBot };