Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/app/src/components/PostHogProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import posthog from 'posthog-js';
import { PostHogProvider as PHProvider, usePostHog } from 'posthog-js/react';
import { Suspense, useEffect } from 'react';

const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY;

export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
if (!posthogKey) {
return;
}

posthog.init(posthogKey, {
api_host: '/stats',
ui_host: posthogUIHost,
capture_pageview: false, // We capture pageviews manually
Expand All @@ -18,6 +24,10 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
});
}, []);

if (!posthogKey) {
return <>{children}</>;
}

return (
<PHProvider client={posthog}>
<SuspendedPostHogPageView />
Expand Down
10 changes: 8 additions & 2 deletions apps/app/src/posthog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { PostHog } from 'posthog-node';

export default function PostHogClient() {
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
export default function PostHogClient(): PostHog | null {
const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY;

if (!posthogKey) {
return null;
}

const posthogClient = new PostHog(posthogKey, {
host: 'https://eu.i.posthog.com',
flushAt: 1,
flushInterval: 0,
Expand Down
10 changes: 8 additions & 2 deletions packages/analytics/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { PostHog } from 'posthog-node';

export default function PostHogClient() {
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
export default function PostHogClient(): PostHog | null {
const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY;

if (!posthogKey) {
return null;
}

const posthogClient = new PostHog(posthogKey, {
host: 'https://eu.i.posthog.com',
flushAt: 1,
flushInterval: 0,
Expand Down
28 changes: 21 additions & 7 deletions packages/analytics/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import PostHogClient from './client';

const posthog = PostHogClient();

function isPostHogEnabled(): boolean {
return posthog !== null;
}

/**
* Analytics utility functions for tracking user events
*/
Expand All @@ -25,12 +29,16 @@ export async function trackEvent({
event,
properties,
}: AnalyticsEvent): Promise<void> {
posthog.capture({
if (!isPostHogEnabled()) {
return;
}

posthog!.capture({
distinctId,
event,
properties,
});
await posthog.shutdown();
await posthog!.shutdown();
}

/**
Expand All @@ -56,27 +64,33 @@ export async function identifyUser({
distinctId,
properties,
}: AnalyticsIdentify): Promise<void> {
posthog.identify({
if (!isPostHogEnabled()) {
return;
}

posthog!.identify({
distinctId,
properties,
});
await posthog.shutdown();
await posthog!.shutdown();
}

/**
* Track multiple events in sequence
*/
export async function trackEvents(events: AnalyticsEvent[]): Promise<void> {
if (events.length === 0) return;
if (events.length === 0 || !isPostHogEnabled()) {
return;
}

events.forEach(({ distinctId, event, properties }) => {
posthog.capture({
posthog!.capture({
distinctId,
event,
properties,
});
});
await posthog.shutdown();
await posthog!.shutdown();
}

/**
Expand Down
Loading