Files
calendar/packages/features/ee/event-tracking/lib/posthog/postHogClient.ts
T
ddfbe25903 feat: adding posthog libs for node + js (#16862)
* adding posthog libs for node + js

* updating lock

* move to init posthog client - pageview component

* adding identify in shell

* removing test event captures

* add signout tracking to dropdown as it was missed in merge

---------

Co-authored-by: sean <sean@brydon.io>
Co-authored-by: Bailey Pumfleet <bailey@pumfleet.co.uk>
2024-11-04 11:46:25 -05:00

41 lines
1.0 KiB
TypeScript

// eslint-disable-next-line no-restricted-imports
import { noop } from "lodash";
import { PostHog, type PostHog as PostHogType } from "posthog-node";
let postHog: {
capture: PostHogType["capture"];
identify: PostHogType["identify"];
};
// eslint-disable-next-line turbo/no-undeclared-env-vars
if (process.env.NEXT_PUBLIC_POSTHOG_KEY) {
// eslint-disable-next-line turbo/no-undeclared-env-vars
postHog = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
// eslint-disable-next-line turbo/no-undeclared-env-vars
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
flushAt: 1,
});
} else {
postHog = {
capture: noop,
identify: noop,
};
}
export function postHogClient() {
function capture(distinctId: string, event: string, properties?: Record<string, any>) {
postHog?.capture({ distinctId, event, properties });
}
function identify(distinctId: string, properties?: Record<string, any>) {
postHog?.identify({ distinctId, properties });
}
return {
capture,
identify,
};
}
export default postHogClient;