Skip to content

Commit 3bd0bf1

Browse files
committed
fix: minor custom event tracking issue
1 parent 5b7001b commit 3bd0bf1

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@databuddy/sdk",
3-
"version": "2.2.11",
3+
"version": "2.2.12",
44
"description": "Official Databuddy Analytics SDK",
55
"main": "./dist/core/index.mjs",
66
"types": "./dist/core/index.d.ts",

packages/sdk/src/core/tracker.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
* Provides type-safe tracking functions
44
*/
55

6-
import type {
7-
DatabuddyTracker,
8-
EventName,
9-
PropertiesForEvent,
10-
TrackFunction,
11-
} from "./types";
6+
import type { DatabuddyTracker } from "./types";
127

138
/**
149
* Check if the Databuddy tracker is available
@@ -28,29 +23,41 @@ export function getTracker(): DatabuddyTracker | null {
2823
}
2924

3025
/**
31-
* Type-safe track function
26+
* Track a custom event (lean, goes to custom_event_spans table)
3227
*/
33-
export const track: TrackFunction = async <T extends EventName>(
34-
eventName: T,
35-
properties?: PropertiesForEvent<T>
36-
): Promise<void> => {
28+
export function trackCustomEvent(
29+
name: string,
30+
properties?: Record<string, unknown>
31+
): void {
3732
if (typeof window === "undefined") {
3833
return;
3934
}
4035

41-
// Try window.db first (shorthand), then window.databuddy
42-
const tracker = window.db?.track || window.databuddy?.track;
36+
const tracker =
37+
window.db?.trackCustomEvent || window.databuddy?.trackCustomEvent;
4338

4439
if (!tracker) {
4540
return;
4641
}
4742

4843
try {
49-
await tracker(eventName, properties);
44+
tracker(name, properties);
5045
} catch (error) {
51-
console.error("Databuddy tracking error:", error);
46+
console.error("Databuddy trackCustomEvent error:", error);
5247
}
53-
};
48+
}
49+
50+
/**
51+
* Track a custom event (alias for trackCustomEvent)
52+
* Goes to the lean custom_event_spans table
53+
*/
54+
export function track(
55+
eventName: string,
56+
properties?: Record<string, unknown>
57+
): void {
58+
trackCustomEvent(eventName, properties);
59+
}
60+
5461
/**
5562
* Clear the current session
5663
*/
@@ -106,16 +113,18 @@ export function trackError(
106113
error_type?: string;
107114
[key: string]: string | number | boolean | null | undefined;
108115
}
109-
): Promise<void> {
110-
return track("error", { message, ...properties });
116+
): void {
117+
track("error", { message, ...properties });
111118
}
112119

113120
/**
114121
* Get anonymous ID from multiple sources
115122
* Priority: URL params > tracker instance > localStorage
116123
*/
117124
export function getAnonymousId(urlParams?: URLSearchParams): string | null {
118-
if (typeof window === "undefined") return null;
125+
if (typeof window === "undefined") {
126+
return null;
127+
}
119128
return (
120129
urlParams?.get("anonId") ||
121130
window.databuddy?.anonymousId ||
@@ -129,7 +138,9 @@ export function getAnonymousId(urlParams?: URLSearchParams): string | null {
129138
* Priority: URL params > tracker instance > sessionStorage
130139
*/
131140
export function getSessionId(urlParams?: URLSearchParams): string | null {
132-
if (typeof window === "undefined") return null;
141+
if (typeof window === "undefined") {
142+
return null;
143+
}
133144
return (
134145
urlParams?.get("sessionId") ||
135146
window.databuddy?.sessionId ||
@@ -159,7 +170,11 @@ export function getTrackingParams(urlParams?: URLSearchParams): string {
159170
const anonId = getAnonymousId(urlParams);
160171
const sessionId = getSessionId(urlParams);
161172
const params = new URLSearchParams();
162-
if (anonId) params.set("anonId", anonId);
163-
if (sessionId) params.set("sessionId", sessionId);
173+
if (anonId) {
174+
params.set("anonId", anonId);
175+
}
176+
if (sessionId) {
177+
params.set("sessionId", sessionId);
178+
}
164179
return params.toString();
165180
}

packages/sdk/src/core/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ export type DatabuddyTracker = {
318318
flush(): void;
319319

320320
/**
321-
* Track a custom event with full type safety
321+
* Track a lean custom event (goes to custom_event_spans table)
322322
*/
323-
trackCustomEvent(eventName: string, properties?: EventProperties): void;
323+
trackCustomEvent(eventName: string, properties?: Record<string, unknown>): void;
324324
}
325325

326326
/**

packages/tracker/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export class Databuddy extends BaseTracker {
182182
properties[key] = attr.value;
183183
}
184184
}
185-
this.track(eventName, properties);
185+
this.trackCustomEvent(eventName, properties);
186186
}
187187
}
188188
};

0 commit comments

Comments
 (0)