Skip to content

Commit 8853333

Browse files
committed
fix(rybbit): prefer top level functions
1 parent ab89f42 commit 8853333

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

docs/content/scripts/analytics/rybbit-analytics.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ If you are using a self-hosted version of Rybbit Analytics, you can provide a cu
9090
useScriptRybbitAnalytics({
9191
scriptInput: {
9292
src: 'https://your-rybbit-instance.com/api/script.js'
93-
}
93+
},
9494
siteId: 'YOUR_SITE_ID'
9595
})
9696
```
@@ -99,8 +99,38 @@ useScriptRybbitAnalytics({
9999

100100
```ts
101101
export interface RybbitAnalyticsApi {
102+
/**
103+
* Tracks a page view
104+
*/
102105
pageview: () => void
103-
event: (eventName: string, properties?: Record<string, any>) => void
106+
107+
/**
108+
* Tracks a custom event
109+
* @param name Name of the event
110+
* @param properties Optional properties for the event
111+
*/
112+
event: (name: string, properties?: Record<string, any>) => void
113+
114+
/**
115+
* Sets a custom user ID for tracking logged-in users
116+
* @param userId The user ID to set (will be stored in localStorage)
117+
*/
118+
identify: (userId: string) => void
119+
120+
/**
121+
* Clears the stored user ID
122+
*/
123+
clearUserId: () => void
124+
125+
/**
126+
* Gets the currently set user ID
127+
* @returns The current user ID or null if not set
128+
*/
129+
getUserId: () => string | null
130+
/**
131+
* @deprecated use top level functions instead
132+
*/
133+
rybbit: RybbitAnalyticsApi
104134
}
105135
```
106136

@@ -110,7 +140,7 @@ You must provide the options when setting up the script for the first time.
110140

111141
```ts
112142
export const RybbitAnalyticsOptions = object({
113-
siteId: string(), // required
143+
siteId: union([string(), number()]), // required
114144
trackSpa: optional(boolean()),
115145
trackQuery: optional(boolean()),
116146
skipPatterns: optional(array(string())),
@@ -140,12 +170,12 @@ const { proxy } = useScriptRybbitAnalytics()
140170
141171
// Track a pageview manually
142172
function trackPage() {
143-
proxy.rybbit.pageview()
173+
proxy.pageview()
144174
}
145175
146176
// Track a custom event
147177
function trackEvent() {
148-
proxy.rybbit.event('button_click', { buttonId: 'signup' })
178+
proxy.event('button_click', { buttonId: 'signup' })
149179
}
150180
</script>
151181
@@ -161,4 +191,4 @@ function trackEvent() {
161191
</template>
162192
```
163193

164-
::
194+
::

src/runtime/registry/rybbit-analytics.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export interface RybbitAnalyticsApi {
4242
* @returns The current user ID or null if not set
4343
*/
4444
getUserId: () => string | null
45+
/**
46+
* @deprecated use top level functions instead
47+
*/
48+
rybbit: RybbitAnalyticsApi
4549
}
4650

4751
declare global {
@@ -65,7 +69,17 @@ export function useScriptRybbitAnalytics<T extends RybbitAnalyticsApi>(_options?
6569
schema: import.meta.dev ? RybbitAnalyticsOptions : undefined,
6670
scriptOptions: {
6771
use() {
68-
return { rybbit: window.rybbit }
72+
if (typeof window.rybbit === 'undefined') {
73+
return null
74+
}
75+
return {
76+
pageview: window.rybbit.pageview,
77+
event: window.rybbit.event,
78+
identify: window.rybbit.identify,
79+
clearUserId: window.rybbit.clearUserId,
80+
getUserId: window.rybbit.getUserId,
81+
rybbit: window.rybbit,
82+
} satisfies RybbitAnalyticsApi
6983
},
7084
},
7185
}

0 commit comments

Comments
 (0)