From 5aab7ecf7e3ba233a6ef70cd28fe035e94b99f10 Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Fri, 29 Nov 2024 14:57:50 +0000 Subject: [PATCH] Fix posthog pageview capturing - should have rtfm --- apps/desktop/src/routes/+layout.svelte | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/desktop/src/routes/+layout.svelte b/apps/desktop/src/routes/+layout.svelte index 320c40ff6c..7d90b1623e 100644 --- a/apps/desktop/src/routes/+layout.svelte +++ b/apps/desktop/src/routes/+layout.svelte @@ -45,11 +45,14 @@ } from '@gitbutler/shared/sharedRoutes'; import { LineManagerFactory } from '@gitbutler/ui/commitLines/lineManager'; import { LineManagerFactory as StackingLineManagerFactory } from '@gitbutler/ui/commitLines/lineManager'; + import posthog from 'posthog-js'; import { onMount, setContext, type Snippet } from 'svelte'; import { Toaster } from 'svelte-french-toast'; import type { LayoutData } from './$types'; import { dev } from '$app/environment'; + import { browser } from '$app/environment'; import { goto } from '$app/navigation'; + import { beforeNavigate, afterNavigate } from '$app/navigation'; import { env } from '$env/dynamic/public'; const { data, children }: { data: LayoutData; children: Snippet } = $props(); @@ -87,6 +90,12 @@ const accessToken = $derived($user?.github_access_token); const octokit = $derived(accessToken ? octokitFromAccessToken(accessToken) : undefined); + // Special initialization to capture pageviews for single page apps. + if (browser) { + beforeNavigate(() => posthog.capture('$pageleave')); + afterNavigate(() => posthog.capture('$pageview')); + } + // This store is literally only used once, on GitHub oauth, to set the // gh username on the user object. Furthermore, it isn't used anywhere. // TODO: Remove the gh username completely?