Skip to content

Commit

Permalink
feat: optimize posthog loading for no consent
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Feb 18, 2024
1 parent f50f60c commit ca84382
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 62 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ MATOMO_API_KEY=

SENTRY_DSN=
SENTRY_AUTH_TOKEN=""

POSTHOG_PUBLIC_KEY=phc_...
POSTHOG_HOST=https://eu.posthog.com
2 changes: 1 addition & 1 deletion app.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup>
<script setup>
const config = useRuntimeConfig()
useHead({
Expand Down
13 changes: 10 additions & 3 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ export default defineNuxtConfig({
PROXY_URL: process.env.PROXY_URL,

SENTRY_DSN: process.env.SENTRY_DSN,

POSTHOG_PUBLIC_KEY: process.env.POSTHOG_PUBLIC_KEY,
POSTHOG_HOST: process.env.POSTHOG_HOST
}
},

Expand Down Expand Up @@ -257,6 +254,16 @@ export default defineNuxtConfig({
cssText: 'html, body { background-color: black; }'
}
],
script: [
// PostHog
{
src: '/js/posthog.js',
type: 'text/plain',
async: true,
'data-category': 'analytics',
'data-service': 'posthog'
}
]
}
},

Expand Down
55 changes: 0 additions & 55 deletions plugins/050.posthog.client.ts

This file was deleted.

26 changes: 26 additions & 0 deletions plugins/050.posthog.client.ts.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {defineNuxtPlugin} from '#app'


/**
* Track page view using Posthog
* For the Script,
* @see nuxt.config.js
*
* @see https://posthog.com/docs/libraries/nuxt-js
*/
export default defineNuxtPlugin(nuxtApp => {
const router = useRouter()

// Make sure that pageviews are captured with each route change
router.afterEach((to) => {
if (!window.posthog) {
return
}

nextTick(() => {
window.posthog.capture('$pageview', {
current_url: to.fullPath
})
})
})
})
9 changes: 9 additions & 0 deletions public/js/posthog.js

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

0 comments on commit ca84382

Please sign in to comment.