Skip to content

Commit

Permalink
fix: track pageleaves with posthog, update posthog config
Browse files Browse the repository at this point in the history
  • Loading branch information
olegshilov committed Aug 23, 2024
1 parent be7855b commit b4cc56f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
7 changes: 3 additions & 4 deletions src/components/identify-wallet-users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ export function IdentifyWalletUsers() {
const isHaqqWallet = Boolean(window.ethereum?.isHaqqWallet);

if (isHaqqWallet) {
const distinctId =
window.__HAQQWALLET__?.POSTHOG_DISTINCT_ID ??
posthog.get_distinct_id();
const walletDistinctId = window.__HAQQWALLET__?.POSTHOG_DISTINCT_ID;
console.log('IdentifyWalletUsers', { walletDistinctId });

posthog.identify(distinctId);
posthog.identify(walletDistinctId ?? posthog.get_distinct_id());
}
}
}, [posthog]);
Expand Down
40 changes: 12 additions & 28 deletions src/components/posthog-page-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,21 @@ export function PostHogPageView() {
const posthog = usePostHog();

useEffect(() => {
const trackPageView = () => {
if (pathname && posthog) {
let url = window.origin + pathname;
if (searchParams.toString()) {
url += `?${searchParams.toString()}`;
}
posthog.capture('$pageview', {
$current_url: url,
$set: {
$browser_language: window.navigator.language,
},
});
}
};

const trackPageLeave = () => {
if (posthog) {
posthog.capture('$pageleave', {});
}
};

// Track pageviews
trackPageView();
if (pathname && posthog) {
let url = window.origin + pathname;

// Add event listener to track page leave
window.addEventListener('beforeunload', trackPageLeave);
if (searchParams.toString()) {
url = url + `?${searchParams.toString()}`;
}

// Cleanup the event listener when the component unmounts
return () => {
window.removeEventListener('beforeunload', trackPageLeave);
};
posthog.capture('$pageview', {
$current_url: url,
$set: {
$browser_language: window.navigator.language,
},
});
}
}, [pathname, searchParams, posthog]);

return null;
Expand Down
5 changes: 4 additions & 1 deletion src/providers/posthog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { PropsWithChildren, useMemo } from 'react';
import posthog from 'posthog-js';
import { PostHogProvider } from 'posthog-js/react';
import { env } from '@/env/client';
import { env } from '../env/client';

export function PHProvider({ children }: PropsWithChildren) {
const postHogInstance = useMemo(() => {
Expand All @@ -13,10 +13,13 @@ export function PHProvider({ children }: PropsWithChildren) {
) {
return undefined;
}

const phInstance = posthog.init(env.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: '/api/ingest',
ui_host: env.NEXT_PUBLIC_POSTHOG_HOST,
capture_pageview: false,
capture_pageleave: true,
persistence: 'localStorage+cookie',
});

if (!phInstance) {
Expand Down

0 comments on commit b4cc56f

Please sign in to comment.