Skip to content

Commit 79ee787

Browse files
committed
helper utils in sdk
1 parent f28259f commit 79ee787

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

packages/sdk/build.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default defineBuildConfig({
1111
'./src/node/index.ts',
1212
'./src/ai/vercel/index.ts',
1313
],
14-
externals: ['react', 'react-dom', 'vue', 'jotai', '@ai-sdk/provider', 'ai', 'tokenlens'],
14+
externals: ['react', 'react-dom', 'vue', 'jotai', '@ai-sdk/provider', 'ai', 'tokenlens', 'msw'],
1515
declaration: true,
1616
alias: {
1717
'@': resolve(__dirname, 'src'),

packages/sdk/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "@databuddy/sdk",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "Official Databuddy Analytics SDK",
55
"main": "./dist/core/index.mjs",
66
"types": "./dist/core/index.d.ts",
77
"license": "MIT",
88
"private": false,
99
"scripts": {
1010
"build": "unbuild",
11-
"test": "bun test"
11+
"test": "bun test",
12+
"vscode:prepublish": "bun run build"
1213
},
1314
"devDependencies": {
1415
"@ai-sdk/provider": "^2.0.0",

packages/sdk/src/core/tracker.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ export function trackError(
110110
return track('error', { message, ...properties });
111111
}
112112

113+
/**
114+
* Get anonymous ID from multiple sources
115+
* Priority: URL params > tracker instance > localStorage
116+
*/
117+
export function getAnonymousId(urlParams?: URLSearchParams): string | null {
118+
if (typeof window === 'undefined') return null;
119+
return urlParams?.get('anonId') || window.databuddy?.anonymousId || localStorage.getItem('did') || null;
120+
}
121+
122+
/**
123+
* Get session ID from multiple sources
124+
* Priority: URL params > tracker instance > sessionStorage
125+
*/
126+
export function getSessionId(urlParams?: URLSearchParams): string | null {
127+
if (typeof window === 'undefined') return null;
128+
return urlParams?.get('sessionId') || window.databuddy?.sessionId || sessionStorage.getItem('did_session') || null;
129+
}
130+
113131
/**
114132
* Get tracking IDs (anonymous ID and session ID) from multiple sources
115133
* Priority: URL params > tracker instance > localStorage/sessionStorage
@@ -118,30 +136,18 @@ export function getTrackingIds(urlParams?: URLSearchParams): {
118136
anonId: string | null;
119137
sessionId: string | null;
120138
} {
121-
if (typeof window === 'undefined') {
122-
return { anonId: null, sessionId: null };
123-
}
124-
125-
const urlAnonId = urlParams?.get('anonId');
126-
const urlSessionId = urlParams?.get('sessionId');
127-
128-
const trackerAnonId = window.databuddy?.anonymousId;
129-
const trackerSessionId = window.databuddy?.sessionId;
130-
131-
const storageAnonId = localStorage.getItem('did');
132-
const storageSessionId = sessionStorage.getItem('did_session');
133-
134139
return {
135-
anonId: urlAnonId || trackerAnonId || storageAnonId || null,
136-
sessionId: urlSessionId || trackerSessionId || storageSessionId || null,
140+
anonId: getAnonymousId(urlParams),
141+
sessionId: getSessionId(urlParams),
137142
};
138143
}
139144

140145
/**
141146
* Get tracking IDs as URL search params string
142147
*/
143148
export function getTrackingParams(urlParams?: URLSearchParams): string {
144-
const { anonId, sessionId } = getTrackingIds(urlParams);
149+
const anonId = getAnonymousId(urlParams);
150+
const sessionId = getSessionId(urlParams);
145151
const params = new URLSearchParams();
146152
if (anonId) params.set('anonId', anonId);
147153
if (sessionId) params.set('sessionId', sessionId);

0 commit comments

Comments
 (0)