|
1 | 1 | <script lang="ts"> |
2 | | - import { inject } from '@vercel/analytics'; |
| 2 | + import { onMount } from 'svelte'; |
| 3 | + import posthog from 'posthog-js'; |
| 4 | +
|
3 | 5 | import { mdiArrowTopRight, mdiGithub, mdiTwitter } from '@mdi/js'; |
4 | 6 | import 'prism-themes/themes/prism-vsc-dark-plus.css'; |
5 | 7 | import { AppBar, AppLayout, Button, QuickSearch, Tooltip, settings, sortFunc } from 'svelte-ux'; |
6 | 8 |
|
7 | 9 | import { dev } from '$app/environment'; |
8 | 10 | import { afterNavigate, goto } from '$app/navigation'; |
| 11 | + import { page } from '$app/stores'; |
9 | 12 |
|
10 | 13 | import NavMenu from './_NavMenu.svelte'; |
11 | 14 |
|
12 | | - inject({ mode: dev ? 'development' : 'production' }); |
13 | | -
|
14 | 15 | settings({ |
15 | 16 | theme: { |
16 | 17 | // AppBar: 'bg-accent-500 text-white shadow-md', |
|
37 | 38 | }; |
38 | 39 | }) |
39 | 40 | .sort(sortFunc((d) => groups.indexOf(d.group))); |
| 41 | +
|
| 42 | + let currentPath = ''; |
| 43 | + onMount(() => { |
| 44 | + // Posthog analytics |
| 45 | + if (!dev) { |
| 46 | + const unsubscribePage = page.subscribe(($page) => { |
| 47 | + if (currentPath && currentPath !== $page.url.pathname) { |
| 48 | + // Page navigated away |
| 49 | + posthog.capture('$pageleave'); |
| 50 | + } |
| 51 | + console.log('entering'); |
| 52 | + // Page entered |
| 53 | + currentPath = $page.url.pathname; |
| 54 | + posthog.capture('$pageview'); |
| 55 | + }); |
| 56 | + const handleBeforeUnload = () => { |
| 57 | + // Hard reloads or browser exit |
| 58 | + posthog.capture('$pageleave'); |
| 59 | + }; |
| 60 | + window.addEventListener('beforeunload', handleBeforeUnload); |
| 61 | + return () => { |
| 62 | + unsubscribePage(); |
| 63 | + window.removeEventListener('beforeunload', handleBeforeUnload); |
| 64 | + }; |
| 65 | + } |
| 66 | + }); |
40 | 67 | </script> |
41 | 68 |
|
42 | 69 | <AppLayout> |
|
0 commit comments