@databuddy/sdk 1.2.0 → 1.3.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 +1 -1
- package/dist/Databuddy.js +2 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/tracker.d.ts +43 -0
- package/dist/tracker.js +86 -0
- package/dist/types.d.ts +181 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -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 | `
|
|
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
|
@@ -16,7 +16,8 @@ export function Databuddy(props) {
|
|
|
16
16
|
return;
|
|
17
17
|
const script = document.createElement('script');
|
|
18
18
|
script.src = props.scriptUrl || 'https://app.databuddy.cc/databuddy.js';
|
|
19
|
-
script.
|
|
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
package/dist/index.js
CHANGED
|
@@ -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;
|
package/dist/tracker.js
ADDED
|
@@ -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,7 +16,7 @@ export interface DatabuddyConfig {
|
|
|
16
16
|
clientSecret?: string;
|
|
17
17
|
/**
|
|
18
18
|
* Custom API endpoint for event ingestion.
|
|
19
|
-
* Default: 'https://
|
|
19
|
+
* Default: 'https://basket.databuddy.cc'
|
|
20
20
|
*/
|
|
21
21
|
apiUrl?: string;
|
|
22
22
|
/**
|
|
@@ -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
|
|
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
|
-
|
|
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
|
|
83
|
+
* Track bounce rate (default: false).
|
|
88
84
|
*/
|
|
89
|
-
|
|
85
|
+
trackBounceRate?: boolean;
|
|
90
86
|
/**
|
|
91
|
-
* Track
|
|
87
|
+
* Track page performance metrics (default: true).
|
|
92
88
|
*/
|
|
93
|
-
|
|
89
|
+
trackPerformance?: boolean;
|
|
94
90
|
/**
|
|
95
|
-
* Track
|
|
91
|
+
* Track Web Vitals metrics (default: false).
|
|
96
92
|
*/
|
|
97
|
-
|
|
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:
|
|
118
|
+
* Enable event batching (default: false).
|
|
119
119
|
*/
|
|
120
120
|
enableBatching?: boolean;
|
|
121
121
|
/**
|
|
122
|
-
* Number of events to batch before sending (default:
|
|
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:
|
|
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
|
-
*
|
|
135
|
+
* Base event properties that can be attached to any event
|
|
136
136
|
*/
|
|
137
|
-
export interface
|
|
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
|
|
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;
|