mirror of
https://github.com/papra-hq/papra
synced 2026-04-21 13:37:23 +00:00
62 lines
1.3 KiB
TypeScript
62 lines
1.3 KiB
TypeScript
import posthog from 'posthog-js';
|
|
import { buildTimeConfig, isDev } from '../config/config';
|
|
|
|
type TrackingServices = {
|
|
capture: (args: {
|
|
event: string;
|
|
properties?: Record<string, unknown>;
|
|
}) => void;
|
|
|
|
reset: () => void;
|
|
|
|
identify: (args: {
|
|
userId: string;
|
|
email: string;
|
|
}) => void;
|
|
};
|
|
|
|
const dummyTrackingServices: TrackingServices = {
|
|
capture: ({ event, ...args }) => {
|
|
if (isDev) {
|
|
// eslint-disable-next-line no-console
|
|
console.log(`[dev] captured event ${event}`, ...(Object.keys(args).length ? [args] : []));
|
|
}
|
|
},
|
|
reset: () => {},
|
|
identify: () => {},
|
|
};
|
|
|
|
function createTrackingServices(): TrackingServices {
|
|
const { isEnabled, apiKey, host } = buildTimeConfig.posthog;
|
|
|
|
if (!isEnabled) {
|
|
return dummyTrackingServices;
|
|
}
|
|
|
|
if (!apiKey) {
|
|
console.warn('PostHog API key is not set');
|
|
return dummyTrackingServices;
|
|
}
|
|
|
|
posthog.init(
|
|
apiKey,
|
|
{
|
|
api_host: host,
|
|
capture_pageview: false,
|
|
},
|
|
);
|
|
|
|
return {
|
|
capture: ({ event, properties }) => {
|
|
posthog.capture(event, properties);
|
|
},
|
|
reset: () => {
|
|
posthog.reset();
|
|
},
|
|
identify: ({ userId, email }) => {
|
|
posthog.identify(userId, { email });
|
|
},
|
|
};
|
|
}
|
|
|
|
export const trackingServices = createTrackingServices();
|