Skip to content

Commit dba9828

Browse files
committed
feat: add PostHog analytics integration with configuration in .env and Nuxt setup
1 parent 98029a1 commit dba9828

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# Public URL, used for OG Image when running nuxt generate
22
NUXT_PUBLIC_SITE_URL=
3+
4+
# PostHog Analytics (Europe region)
5+
# Get your project API key from: https://eu.posthog.com/settings/project
6+
NUXT_PUBLIC_POSTHOG_PUBLIC_KEY=
7+
8+
# SubmitJSON API Key
9+
NUXT_PUBLIC_SUBMIT_JSON_API_KEY=

app/plugins/posthog.client.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import posthog from 'posthog-js'
2+
3+
export default defineNuxtPlugin(() => {
4+
const runtimeConfig = useRuntimeConfig()
5+
const posthogClient = posthog.init(runtimeConfig.public.posthogPublicKey, {
6+
api_host: runtimeConfig.public.posthogHost,
7+
person_profiles: 'identified_only',
8+
capture_pageview: false, // We'll handle this manually with the router,
9+
cookieless_mode: 'always', // Use cookieless mode to avoid cookie consent issues
10+
loaded: (posthog) => {
11+
if (import.meta.env.MODE === 'development') posthog.debug()
12+
}
13+
})
14+
15+
// Track page views with Vue Router
16+
const router = useRouter()
17+
router.afterEach((to) => {
18+
nextTick(() => {
19+
posthog.capture('$pageview', {
20+
current_url: to.fullPath
21+
})
22+
})
23+
})
24+
25+
return {
26+
provide: {
27+
posthog: () => posthogClient
28+
}
29+
}
30+
})

nuxt.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export default defineNuxtConfig({
4141

4242
runtimeConfig: {
4343
public: {
44-
submitJsonApiKey: ''
44+
submitJsonApiKey: '',
45+
posthogPublicKey: '',
46+
posthogHost: 'https://eu.i.posthog.com'
4547
}
4648
},
4749

0 commit comments

Comments
 (0)