Skip to content

Commit

Permalink
feat: add gtag
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli committed Jan 6, 2023
1 parent 0ec540b commit 0f1c451
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 41 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NUXT_PUBLIC_GOOGLE_ANALYTICS_ID=
47 changes: 12 additions & 35 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,20 @@ export default defineNuxtConfig({
targetCookieIds: ['i18n_redirected'],
},
],
// optional: [
// {
// name: 'Google Analytics',
// id: 'ga',
// // targetCookieIds: ['_ga', '_gat', '_gid'],
// // accepted: () => {
// // const { $ga } = useNuxtApp()
// // $ga.enable()
// // },
// // declined: () => {
// // const { $ga } = useNuxtApp()
// // $ga.disable()
// // },
// },
// ],
optional: [
{
description: {
de: 'Hilft uns dabei Nutzerverhalten zu verstehen und unsere Dienste zu verbessern.',
en: 'Helps us understand user behavior and optimize our services.',
},
name: 'Google Analytics',
targetCookieIds: ['_ga', '_ga_K4R41W62BR'],
},
],
},
locales: ['en', 'de'],
},
],
// [
// '@nuxtjs/google-analytics',
// {
// disabled: () => {
// const enabledCookies =
// document.cookie
// .match(
// '(^|;)\\s*' +
// 'cookie_control_enabled_cookies' +
// '\\s*=\\s*([^;]+)'
// )
// ?.pop() || ''
// return !enabledCookies.split(',').includes('ga')
// },
// },
// ],
[
'@nuxtjs/html-validator',
{
Expand Down Expand Up @@ -118,10 +97,8 @@ export default defineNuxtConfig({
},
runtimeConfig: {
public: {
googleAnalytics: {
debug: process.env.NODE_ENV !== 'production',
},
isTesting: false, // set via environment variables only
googleAnalyticsId: '', // set via environment variable `NUXT_PUBLIC_GOOGLE_ANALYTICS_ID` only
isTesting: false, // set via environment variable `NUXT_PUBLIC_IS_TESTING` only
},
},
typescript: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"tailwindcss": "3.2.4",
"typescript": "4.9.4",
"vue": "3.2.45",
"vue-gtag": "2.0.1",
"vue-tsc": "1.0.22"
},
"pnpm": {
Expand Down
19 changes: 19 additions & 0 deletions plugins/gtag.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import VueGtag from 'vue-gtag'

export default defineNuxtPlugin((nuxtApp) => {
const config = useRuntimeConfig()
const router = useRouter()
const cookieControl = useCookieControl()

nuxtApp.vueApp.use(
VueGtag,
{
bootstrap:
cookieControl.cookiesEnabledIds.value.includes('google-analytics'),
config: {
id: config.public.googleAnalyticsId,
},
},
router
)
})
18 changes: 13 additions & 5 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion public/assets/static/favicon/site.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
],
"theme_color": "#ffffff",
"background_color": "#ffffff",
"start_url": "https://jonas-thelemann.de/",
"start_url": "/",
"display": "standalone"
}

0 comments on commit 0f1c451

Please sign in to comment.