Skip to content

Commit

Permalink
Setup Posthog analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
techniq committed Dec 14, 2023
1 parent ff5cd75 commit 7d3d91b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/svelte-ux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"date-fns": "^2.30.0",
"immer": "^10.0.3",
"lodash-es": "^4.17.21",
"posthog-js": "^1.95.1",
"rehype-slug": "^6.0.0",
"tailwind-merge": "^2.0.0",
"zod": "^3.22.4"
Expand Down
35 changes: 34 additions & 1 deletion packages/svelte-ux/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { onMount } from 'svelte';
import posthog from 'posthog-js';
import 'prism-themes/themes/prism-vsc-dark-plus.css';
import { dev } from '$app/environment';
import { mdiArrowTopRight, mdiGithub, mdiTwitter } from '@mdi/js';
import AppBar from '$lib/components/AppBar.svelte';
Expand All @@ -9,7 +10,11 @@
import Tooltip from '$lib/components/Tooltip.svelte';
import NavMenu from './_NavMenu.svelte';
import QuickSearch from '$lib/components/QuickSearch.svelte';
import { dev } from '$app/environment';
import { afterNavigate, goto } from '$app/navigation';
import { page } from '$app/stores';
import { settings } from '$lib';
settings({
Expand Down Expand Up @@ -46,6 +51,34 @@
};
})
.sort((a, b) => groups.indexOf(a.group) - groups.indexOf(b.group));
let currentPath = '';
onMount(() => {
// Posthog analytics
if (!dev) {
const unsubscribePage = page.subscribe(($page) => {
if (currentPath && currentPath !== $page.url.pathname) {
// Page navigated away
posthog.capture('$pageleave');
}
console.log('entering');
// Page entered
currentPath = $page.url.pathname;
posthog.capture('$pageview');
});
const handleBeforeUnload = () => {
// Hard reloads or browser exit
posthog.capture('$pageleave');
};
window.addEventListener('beforeunload', handleBeforeUnload);
return () => {
unsubscribePage();
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}
});
</script>

<AppLayout>
Expand Down
15 changes: 15 additions & 0 deletions packages/svelte-ux/src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
import posthog from 'posthog-js';

import { browser, dev } from '$app/environment';

// Disable server-side rendering until AppLayout shift is fixed (issue #22)
export const ssr = false;

export const load = async () => {
// Setup Posthog
if (browser && !dev) {
posthog.init('phc_F78mUWQpKPpsXS1mamJFzDWM8bivZrwIx4Nm1cI8BSb', {
api_host: 'https://app.posthog.com',
capture_pageview: false,
capture_pageleave: false,
});
}
};
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d3d91b

Please sign in to comment.