@databuddy/sdk 1.2.0 → 1.4.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/README.md CHANGED
@@ -61,7 +61,7 @@ All options are type-safe and documented in `DatabuddyConfig`:
61
61
  | `clientId` | string | — | **Required.** Your Databuddy project client ID. |
62
62
  | `clientSecret` | string | — | (Advanced) For server-side use only. |
63
63
  | `apiUrl` | string | `https://api.databuddy.cc` | Custom API endpoint. |
64
- | `scriptUrl` | string | `https://app.databuddy.cc/databuddy.js` | Custom script URL. |
64
+ | `scriptUrl` | string | `https://cdn.databuddy.cc/databuddy.js` | Custom script URL. |
65
65
  | `sdk` | string | `web` | SDK name. Only override for custom builds. |
66
66
  | `sdkVersion` | string | *auto* | SDK version. Defaults to package version. |
67
67
  | `disabled` | boolean | `false` | Disable all tracking. |
@@ -70,7 +70,7 @@ All options are type-safe and documented in `DatabuddyConfig`:
70
70
  | `trackHashChanges` | boolean | `false` | Track hash changes in URL. |
71
71
  | `trackAttributes` | boolean | `false` | Track data-* attributes. |
72
72
  | `trackOutgoingLinks` | boolean | `false` | Track outgoing link clicks. |
73
- | `trackSessions` | boolean | `false` | Track user sessions. |
73
+ | `trackSessions` | boolean | `true` | Track user sessions. |
74
74
  | `trackPerformance` | boolean | `true` | Track page performance. |
75
75
  | `trackWebVitals` | boolean | `true` | Track Web Vitals. |
76
76
  | `trackEngagement` | boolean | `false` | Track engagement metrics. |
package/dist/Databuddy.js CHANGED
@@ -15,8 +15,9 @@ export function Databuddy(props) {
15
15
  if (document.querySelector('script[data-databuddy-injected]'))
16
16
  return;
17
17
  const script = document.createElement('script');
18
- script.src = props.scriptUrl || 'https://app.databuddy.cc/databuddy.js';
19
- script.defer = true;
18
+ script.src = props.scriptUrl || 'https://cdn.databuddy.cc/databuddy.js';
19
+ script.async = true;
20
+ script.crossOrigin = 'anonymous';
20
21
  script.setAttribute('data-databuddy-injected', 'true');
21
22
  // Always set sdkVersion from package.json unless explicitly overridden
22
23
  const sdkVersion = props.sdkVersion || pkg.version;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './Databuddy';
2
2
  export * from './types';
3
+ export * from './tracker';
package/dist/index.js CHANGED
@@ -1,2 +1,4 @@
1
+ // Main exports
1
2
  export * from './Databuddy';
2
3
  export * from './types';
4
+ export * from './tracker';
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Databuddy SDK Client-side Tracker
3
+ * Provides type-safe tracking functions
4
+ */
5
+ import type { DatabuddyTracker, TrackFunction } from './types';
6
+ /**
7
+ * Check if the Databuddy tracker is available
8
+ */
9
+ export declare function isTrackerAvailable(): boolean;
10
+ /**
11
+ * Get the Databuddy tracker instance
12
+ */
13
+ export declare function getTracker(): DatabuddyTracker | null;
14
+ /**
15
+ * Type-safe track function
16
+ */
17
+ export declare const track: TrackFunction;
18
+ /**
19
+ * Clear the current session
20
+ */
21
+ export declare function clear(): void;
22
+ /**
23
+ * Flush any queued events
24
+ */
25
+ export declare function flush(): void;
26
+ /**
27
+ * Track an error event
28
+ */
29
+ export declare function trackError(message: string, properties?: {
30
+ filename?: string;
31
+ lineno?: number;
32
+ colno?: number;
33
+ stack?: string;
34
+ error_type?: string;
35
+ [key: string]: any;
36
+ }): Promise<void>;
37
+ declare const _default: {
38
+ track: TrackFunction;
39
+ clear: typeof clear;
40
+ flush: typeof flush;
41
+ trackError: typeof trackError;
42
+ };
43
+ export default _default;
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Databuddy SDK Client-side Tracker
3
+ * Provides type-safe tracking functions
4
+ */
5
+ /**
6
+ * Check if the Databuddy tracker is available
7
+ */
8
+ export function isTrackerAvailable() {
9
+ return typeof window !== 'undefined' &&
10
+ (!!window.databuddy || !!window.db);
11
+ }
12
+ /**
13
+ * Get the Databuddy tracker instance
14
+ */
15
+ export function getTracker() {
16
+ if (typeof window === 'undefined')
17
+ return null;
18
+ return window.databuddy || null;
19
+ }
20
+ /**
21
+ * Type-safe track function
22
+ */
23
+ export const track = async (eventName, properties) => {
24
+ var _a, _b;
25
+ if (typeof window === 'undefined') {
26
+ return;
27
+ }
28
+ // Try window.db first (shorthand), then window.databuddy
29
+ const tracker = ((_a = window.db) === null || _a === void 0 ? void 0 : _a.track) || ((_b = window.databuddy) === null || _b === void 0 ? void 0 : _b.track);
30
+ if (!tracker) {
31
+ return;
32
+ }
33
+ try {
34
+ await tracker(eventName, properties);
35
+ }
36
+ catch (error) {
37
+ }
38
+ };
39
+ /**
40
+ * Clear the current session
41
+ */
42
+ export function clear() {
43
+ var _a, _b;
44
+ if (typeof window === 'undefined') {
45
+ return;
46
+ }
47
+ const tracker = ((_a = window.db) === null || _a === void 0 ? void 0 : _a.clear) || ((_b = window.databuddy) === null || _b === void 0 ? void 0 : _b.clear);
48
+ if (!tracker) {
49
+ return;
50
+ }
51
+ try {
52
+ tracker();
53
+ }
54
+ catch (error) {
55
+ }
56
+ }
57
+ /**
58
+ * Flush any queued events
59
+ */
60
+ export function flush() {
61
+ var _a, _b;
62
+ if (typeof window === 'undefined') {
63
+ return;
64
+ }
65
+ const tracker = ((_a = window.db) === null || _a === void 0 ? void 0 : _a.flush) || ((_b = window.databuddy) === null || _b === void 0 ? void 0 : _b.flush);
66
+ if (!tracker) {
67
+ return;
68
+ }
69
+ try {
70
+ tracker();
71
+ }
72
+ catch (error) {
73
+ }
74
+ }
75
+ /**
76
+ * Track an error event
77
+ */
78
+ export function trackError(message, properties) {
79
+ return track('error', { message, ...properties });
80
+ }
81
+ export default {
82
+ track,
83
+ clear,
84
+ flush,
85
+ trackError,
86
+ };
package/dist/types.d.ts CHANGED
@@ -16,12 +16,12 @@ export interface DatabuddyConfig {
16
16
  clientSecret?: string;
17
17
  /**
18
18
  * Custom API endpoint for event ingestion.
19
- * Default: 'https://api.databuddy.cc'
19
+ * Default: 'https://basket.databuddy.cc'
20
20
  */
21
21
  apiUrl?: string;
22
22
  /**
23
23
  * Custom script URL for the Databuddy browser bundle.
24
- * Default: 'https://app.databuddy.cc/databuddy.js'
24
+ * Default: 'https://cdn.databuddy.cc/databuddy.js'
25
25
  */
26
26
  scriptUrl?: string;
27
27
  /**
@@ -51,6 +51,10 @@ export interface DatabuddyConfig {
51
51
  * Track hash changes in the URL (default: false).
52
52
  */
53
53
  trackHashChanges?: boolean;
54
+ /**
55
+ * Track user sessions (default: true).
56
+ */
57
+ trackSessions?: boolean;
54
58
  /**
55
59
  * Track data-* attributes on elements (default: false).
56
60
  */
@@ -60,17 +64,9 @@ export interface DatabuddyConfig {
60
64
  */
61
65
  trackOutgoingLinks?: boolean;
62
66
  /**
63
- * Track user sessions (default: false).
64
- */
65
- trackSessions?: boolean;
66
- /**
67
- * Track page performance metrics (default: true).
68
- */
69
- trackPerformance?: boolean;
70
- /**
71
- * Track Web Vitals metrics (default: true).
67
+ * Track user interactions (default: false).
72
68
  */
73
- trackWebVitals?: boolean;
69
+ trackInteractions?: boolean;
74
70
  /**
75
71
  * Track user engagement metrics (default: false).
76
72
  */
@@ -84,17 +80,21 @@ export interface DatabuddyConfig {
84
80
  */
85
81
  trackExitIntent?: boolean;
86
82
  /**
87
- * Track user interactions (default: false).
83
+ * Track bounce rate (default: false).
88
84
  */
89
- trackInteractions?: boolean;
85
+ trackBounceRate?: boolean;
90
86
  /**
91
- * Track JavaScript errors (default: true).
87
+ * Track page performance metrics (default: true).
92
88
  */
93
- trackErrors?: boolean;
89
+ trackPerformance?: boolean;
94
90
  /**
95
- * Track bounce rate (default: false).
91
+ * Track Web Vitals metrics (default: false).
96
92
  */
97
- trackBounceRate?: boolean;
93
+ trackWebVitals?: boolean;
94
+ /**
95
+ * Track JavaScript errors (default: false).
96
+ */
97
+ trackErrors?: boolean;
98
98
  /**
99
99
  * Sampling rate for events (0.0 to 1.0, default: 1.0).
100
100
  * Example: 0.5 = 50% of events sent.
@@ -115,26 +115,184 @@ export interface DatabuddyConfig {
115
115
  */
116
116
  initialRetryDelay?: number;
117
117
  /**
118
- * Enable event batching (default: true).
118
+ * Enable event batching (default: false).
119
119
  */
120
120
  enableBatching?: boolean;
121
121
  /**
122
- * Number of events to batch before sending (default: 20).
122
+ * Number of events to batch before sending (default: 10).
123
123
  * Only used if enableBatching is true.
124
124
  * Min: 1, Max: 50
125
125
  */
126
126
  batchSize?: number;
127
127
  /**
128
- * Batch timeout in milliseconds (default: 5000).
128
+ * Batch timeout in milliseconds (default: 2000).
129
129
  * Only used if enableBatching is true.
130
130
  * Min: 100, Max: 30000
131
131
  */
132
132
  batchTimeout?: number;
133
133
  }
134
134
  /**
135
- * Event properties that can be attached to any event
135
+ * Base event properties that can be attached to any event
136
136
  */
137
- export interface EventProperties {
137
+ export interface BaseEventProperties {
138
+ /** Page URL */
139
+ __path?: string;
140
+ /** Page title */
141
+ __title?: string;
142
+ /** Referrer URL */
143
+ __referrer?: string;
144
+ /** Event timestamp in milliseconds */
145
+ __timestamp_ms?: number;
146
+ /** Session ID */
147
+ sessionId?: string;
148
+ /** Session start time */
149
+ sessionStartTime?: number;
150
+ /** Page count in session */
151
+ page_count?: number;
152
+ /** Screen resolution */
153
+ screen_resolution?: string;
154
+ /** Viewport size */
155
+ viewport_size?: string;
156
+ /** User timezone */
157
+ timezone?: string;
158
+ /** User language */
159
+ language?: string;
160
+ /** UTM parameters */
161
+ utm_source?: string;
162
+ utm_medium?: string;
163
+ utm_campaign?: string;
164
+ utm_term?: string;
165
+ utm_content?: string;
166
+ }
167
+ /**
168
+ * Custom event properties that can be attached to any event
169
+ */
170
+ export interface EventProperties extends BaseEventProperties {
138
171
  /** Custom properties for the event */
139
- [key: string]: string | number | boolean | null | undefined | EventProperties;
172
+ [key: string]: string | number | boolean | null | undefined;
140
173
  }
174
+ /**
175
+ * Pre-defined event types with their specific properties
176
+ */
177
+ export interface EventTypeMap {
178
+ screen_view: {
179
+ time_on_page?: number;
180
+ scroll_depth?: number;
181
+ interaction_count?: number;
182
+ has_exit_intent?: boolean;
183
+ is_bounce?: 0 | 1;
184
+ };
185
+ page_exit: {
186
+ time_on_page: number;
187
+ scroll_depth: number;
188
+ interaction_count: number;
189
+ has_exit_intent: boolean;
190
+ page_count: number;
191
+ is_bounce: 0 | 1;
192
+ };
193
+ button_click: {
194
+ button_text?: string;
195
+ button_type?: string;
196
+ button_id?: string;
197
+ element_class?: string;
198
+ };
199
+ link_out: {
200
+ href: string;
201
+ text?: string;
202
+ target_domain?: string;
203
+ };
204
+ form_submit: {
205
+ form_id?: string;
206
+ form_name?: string;
207
+ form_type?: string;
208
+ success?: boolean;
209
+ };
210
+ web_vitals: {
211
+ fcp?: number;
212
+ lcp?: number;
213
+ cls?: string;
214
+ fid?: number;
215
+ ttfb?: number;
216
+ load_time?: number;
217
+ dom_ready_time?: number;
218
+ render_time?: number;
219
+ request_time?: number;
220
+ };
221
+ error: {
222
+ message: string;
223
+ filename?: string;
224
+ lineno?: number;
225
+ colno?: number;
226
+ stack?: string;
227
+ error_type?: string;
228
+ };
229
+ [eventName: string]: EventProperties;
230
+ }
231
+ /**
232
+ * Available event names
233
+ */
234
+ export type EventName = keyof EventTypeMap;
235
+ /**
236
+ * Properties for a specific event type
237
+ */
238
+ export type PropertiesForEvent<T extends EventName> = T extends keyof EventTypeMap ? EventTypeMap[T] & EventProperties : EventProperties;
239
+ /**
240
+ * Databuddy tracker instance interface
241
+ */
242
+ export interface DatabuddyTracker {
243
+ /**
244
+ * Track a custom event
245
+ */
246
+ track<T extends EventName>(eventName: T, properties?: PropertiesForEvent<T>): Promise<void>;
247
+ /**
248
+ * Track a screen/page view
249
+ */
250
+ screenView(path?: string, properties?: EventProperties): void;
251
+ /**
252
+ * Set global properties that will be attached to all events
253
+ */
254
+ setGlobalProperties(properties: EventProperties): void;
255
+ /**
256
+ * Clear the current user session and generate new IDs
257
+ */
258
+ clear(): void;
259
+ /**
260
+ * Flush any queued events immediately
261
+ */
262
+ flush(): void;
263
+ /**
264
+ * Track a custom event with full type safety
265
+ */
266
+ trackCustomEvent(eventName: string, properties?: EventProperties): void;
267
+ }
268
+ /**
269
+ * Global window interface extensions
270
+ */
271
+ declare global {
272
+ interface Window {
273
+ databuddy?: DatabuddyTracker;
274
+ db?: {
275
+ track: DatabuddyTracker['track'];
276
+ screenView: DatabuddyTracker['screenView'];
277
+ clear: DatabuddyTracker['clear'];
278
+ flush: DatabuddyTracker['flush'];
279
+ setGlobalProperties: DatabuddyTracker['setGlobalProperties'];
280
+ trackCustomEvent: DatabuddyTracker['trackCustomEvent'];
281
+ };
282
+ }
283
+ }
284
+ /**
285
+ * Helper type for HTML data attributes for automatic tracking
286
+ */
287
+ export interface DataAttributes {
288
+ /** Event name to track when element is clicked */
289
+ 'data-track': string;
290
+ /** Additional data attributes (converted to camelCase) */
291
+ [key: `data-${string}`]: string;
292
+ }
293
+ /**
294
+ * Utility types for creating typed event tracking functions
295
+ */
296
+ export type TrackFunction = <T extends EventName>(eventName: T, properties?: PropertiesForEvent<T>) => Promise<void>;
297
+ export type ScreenViewFunction = (path?: string, properties?: EventProperties) => void;
298
+ export type SetGlobalPropertiesFunction = (properties: EventProperties) => void;
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "name": "@databuddy/sdk",
3
- "version": "1.2.0",
4
- "private": false,
5
- "description": "Official Databuddy Analytics SDK",
6
- "main": "dist/index.js",
7
- "exports": {
8
- ".": "./dist/index.js"
9
- },
10
- "files": [
11
- "dist"
12
- ],
13
- "keywords": [
14
- "analytics",
15
- "tracking",
16
- "databuddy",
17
- "sdk"
18
- ],
19
- "scripts": {
20
- "build": "tsc --project tsconfig.json"
21
- },
22
- "license": "MIT",
23
- "devDependencies": {
24
- "@types/node": "^20.0.0",
25
- "typescript": "^5.0.0"
26
- }
1
+ {
2
+ "name": "@databuddy/sdk",
3
+ "version": "1.4.0",
4
+ "private": false,
5
+ "description": "Official Databuddy Analytics SDK",
6
+ "main": "dist/index.js",
7
+ "exports": {
8
+ ".": "./dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "keywords": [
14
+ "analytics",
15
+ "tracking",
16
+ "databuddy",
17
+ "sdk"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc --project tsconfig.json"
21
+ },
22
+ "license": "MIT",
23
+ "devDependencies": {
24
+ "@types/node": "^20.0.0",
25
+ "typescript": "^5.0.0"
26
+ }
27
27
  }