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 */
117124export 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 */
131140export 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}
0 commit comments