Skip to content

Commit 16a389b

Browse files
committed
Integrate PostHog for social media share tracking and add configuration
1 parent a793c0f commit 16a389b

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

components/blog/SocialMediaShare.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ const props = defineProps({
2323
// Function to track share events (for future PostHog integration)
2424
const trackShareEvent = (platform: string) => {
2525
// This function will be used for PostHog tracking in the future
26-
// For now, it's just a placeholder
27-
console.log(`Shared on ${platform}`);
26+
posthog.capture('social_share', {
27+
platform: platform,
28+
url: props.url,
29+
title: props.title
30+
});
2831
};
2932
3033
// Function to add tracking parameter for large pages

nuxt.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ export default defineNuxtConfig({
151151
runtimeConfig: {
152152
public: {
153153
siteUrl: 'https://blog.onelitefeather.net',
154+
posthogPublicKey: 'phc_t9nBlYL9LcDj4LDKZfQ97m5nbvFDTugkdQqAAspfdI',
155+
posthogHost: 'https://eu.i.posthog.com'
154156
}
155157
},
156158
site: {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@
2929
"unstorage": "^1.16.0",
3030
"vue": "^3.5.14",
3131
"vue-router": "^4.5.1"
32+
},
33+
"dependencies": {
34+
"posthog-js": "^1.249.0"
3235
}
3336
}

plugins/posthog.client.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineNuxtPlugin } from '#app'
2+
import posthog from 'posthog-js'
3+
export default defineNuxtPlugin(nuxtApp => {
4+
const runtimeConfig = useRuntimeConfig();
5+
const posthogClient = posthog.init(runtimeConfig.public.posthogPublicKey, {
6+
api_host: runtimeConfig.public.posthogHost,
7+
person_profiles: 'identified_only', // or 'always' to create profiles for anonymous users as well
8+
capture_pageview: false, // we add manual pageview capturing below
9+
loaded: (posthog) => {
10+
if (import.meta.env.MODE === 'development') posthog.debug();
11+
}
12+
})
13+
14+
// Make sure that pageviews are captured with each route change
15+
const router = useRouter();
16+
router.afterEach((to) => {
17+
nextTick(() => {
18+
posthog.capture('$pageview', {
19+
current_url: to.fullPath
20+
});
21+
});
22+
});
23+
24+
return {
25+
provide: {
26+
posthog: () => posthogClient
27+
}
28+
}
29+
})

pnpm-lock.yaml

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)