feat(analytics): persona logging upgrade and Sentry system config
Add client-logging SDK, expanded event taxonomy, API observability, admin domain events UI, and move SENTRY_DSN to HQ system settings with @sentry/node bootstrap after config preload. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import { getSessionId } from './session';
|
||||
|
||||
export type TrackParams = Record<string, unknown>;
|
||||
|
||||
export type UserTrackerOptions = {
|
||||
apiBase: string;
|
||||
clientApp: string;
|
||||
getToken?: () => string | null;
|
||||
};
|
||||
|
||||
export function createUserTracker(opts: UserTrackerOptions) {
|
||||
const getToken = opts.getToken ?? (() => localStorage.getItem('accessToken'));
|
||||
|
||||
function track(eventName: string, params?: TrackParams) {
|
||||
const sessionId = getSessionId();
|
||||
const token = getToken();
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Client-App': opts.clientApp,
|
||||
};
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
|
||||
void fetch(`${opts.apiBase}/analytics/events`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
sessionId,
|
||||
clientApp: opts.clientApp,
|
||||
events: [{ eventName, params: { sessionId, ...params } }],
|
||||
}),
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
function trackPageView(eventName: string, params?: TrackParams) {
|
||||
track(eventName, {
|
||||
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
function trackSessionStart() {
|
||||
track('session_start', {
|
||||
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
return { track, trackPageView, trackSessionStart };
|
||||
}
|
||||
|
||||
export type StoreTrackerOptions = {
|
||||
apiBase: string;
|
||||
clientApp: string;
|
||||
getToken: () => string | null;
|
||||
getStoreId: () => string | null;
|
||||
};
|
||||
|
||||
export function createStoreTracker(opts: StoreTrackerOptions) {
|
||||
function track(eventName: string, params?: TrackParams) {
|
||||
const token = opts.getToken();
|
||||
const storeId = opts.getStoreId();
|
||||
if (!token || !storeId) return;
|
||||
|
||||
void fetch(`${opts.apiBase}/analytics/store-events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
'X-Client-App': opts.clientApp,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
sessionId: getSessionId(),
|
||||
storeId,
|
||||
events: [{ eventName, params: { sessionId: getSessionId(), storeId, ...params } }],
|
||||
}),
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
function trackPageView(eventName: string, params?: TrackParams) {
|
||||
track(eventName, {
|
||||
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
return { track, trackPageView };
|
||||
}
|
||||
|
||||
export type PartnerTrackerOptions = {
|
||||
apiBase: string;
|
||||
clientApp: string;
|
||||
getToken: () => string | null;
|
||||
};
|
||||
|
||||
export function createPartnerTracker(opts: PartnerTrackerOptions) {
|
||||
function track(eventName: string, params?: TrackParams) {
|
||||
const token = opts.getToken();
|
||||
if (!token) return;
|
||||
|
||||
void fetch(`${opts.apiBase}/analytics/partner-events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
'X-Client-App': opts.clientApp,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
sessionId: getSessionId(),
|
||||
events: [{ eventName, params: { sessionId: getSessionId(), ...params } }],
|
||||
}),
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
function trackPageView(eventName: string, params?: TrackParams) {
|
||||
track(eventName, {
|
||||
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
return { track, trackPageView };
|
||||
}
|
||||
Reference in New Issue
Block a user