Skip to content

Commit

Permalink
fix: fix gtm
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Oct 2, 2023
1 parent edb611f commit 704a333
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
56 changes: 29 additions & 27 deletions lib/tracker-google.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
import { createGtm } from '@gtm-support/vue-gtm'
import { App } from 'nuxt/dist/app/compat/vue-demi'
import urlSlug from 'url-slug'

import { Event, Tracker } from '~/lib/trackers'

export default class Google implements Tracker {
waitForConsent: boolean
gtm: any
googleTagManagerId: string

constructor(
nuxtApp: any,
waitForConsent: boolean,
googleTagManagerId: string
) {
constructor(app: App, waitForConsent: boolean, googleTagManagerId: string) {
const gtm = createGtm({
id: googleTagManagerId,
defer: true,
compatibility: false,
enabled: false,
enabled: !waitForConsent,
})
nuxtApp.vueApp.use(gtm)
app.use(gtm)

this.waitForConsent = waitForConsent
this.gtm = gtm
this.googleTagManagerId = googleTagManagerId
if (!waitForConsent) {
this.gtm.init(googleTagManagerId)
}
}

consent() {
consent(app: App) {
if (this.waitForConsent) {
this.gtm.init(this.googleTagManagerId)
const gtm = app.config.globalProperties.$gtm
gtm.enable(true)
}
}

track(event: Event) {
track(app: App, event: Event) {
const gtm = app.config.globalProperties.$gtm
console.error(gtm)
if (!gtm.enabled()) {
return
}

switch (event.type) {
case 'page': {
this.gtm.push({
window.dataLayer?.push({
event: 'pageview',
pageType: 'PageView',
pageTitle: event.title,
Expand All @@ -49,7 +47,7 @@ export default class Google implements Tracker {
break
}
case 'menu': {
this.gtm.push({
window.dataLayer?.push({
event: 'pageview',
pageType: 'PageView',
pageTitle: event.title,
Expand All @@ -58,7 +56,7 @@ export default class Google implements Tracker {
break
}
case 'category_event': {
this.gtm.push({
window.dataLayer?.push({
event: event.type,
action: event.event,
categoryId: event.categoryId,
Expand All @@ -68,7 +66,7 @@ export default class Google implements Tracker {
}
case 'notebook_event':
case 'search': {
this.gtm.push({
window.dataLayer?.push({
event: 'pageview',
pageType: 'PageView',
pageTitle: event.type,
Expand All @@ -78,7 +76,7 @@ export default class Google implements Tracker {
}
// case 'search_query': {}
case 'search_result_event': {
this.gtm.push({
window.dataLayer?.push({
event: event.type,
action: event.event,
type: event.resultType,
Expand All @@ -87,7 +85,7 @@ export default class Google implements Tracker {
break
}
case 'popup': {
this.gtm.push({
window.dataLayer?.push({
event: 'pageview',
pageType: 'PageView',
pageTitle: event.title,
Expand All @@ -99,7 +97,7 @@ export default class Google implements Tracker {
break
}
case 'popup_event': {
this.gtm.push({
window.dataLayer?.push({
event: event.type,
action: event.event,
poiId: event.poiId,
Expand All @@ -109,19 +107,23 @@ export default class Google implements Tracker {
break
}
case 'map_control_event': {
this.gtm.push({ event: event.type, action: event.event })
window.dataLayer?.push({ event: event.type, action: event.event })
break
}
case 'favorites_event': {
this.gtm.push({ event: event.type, action: event.event })
window.dataLayer?.push({ event: event.type, action: event.event })
break
}
case 'external_link': {
this.gtm.push({ event: event.type, url: event.url, title: event.title })
window.dataLayer?.push({
event: event.type,
url: event.url,
title: event.title,
})
break
}
case 'details_event': {
this.gtm.push({
window.dataLayer?.push({
event: event.type,
action: event.event,
poiId: event.poiId,
Expand Down
14 changes: 5 additions & 9 deletions lib/tracker-matomo.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { App } from 'nuxt/dist/app/compat/vue-demi'
import urlSlug from 'url-slug'
// @ts-ignore
import VueMatomo from 'vue-matomo'

import { Event, Tracker } from '~/lib/trackers'

export default class Matomo implements Tracker {
constructor(
nuxtApp: any,
waitForConsent: boolean,
url: string,
siteId: string
) {
nuxtApp.vueApp.use(VueMatomo, {
constructor(app: App, waitForConsent: boolean, url: string, siteId: string) {
app.use(VueMatomo, {
host: url,
siteId,
requireConsent: waitForConsent,
Expand All @@ -20,7 +16,7 @@ export default class Matomo implements Tracker {
})
}

consent() {
consent(_app: App) {
let delai = 1000
const timeout = () => {
setTimeout(function () {
Expand All @@ -44,7 +40,7 @@ export default class Matomo implements Tracker {
set()
}

track(event: Event) {
track(_app: App, event: Event) {
// @ts-ignore
const _paq = window._paq
switch (event.type) {
Expand Down
6 changes: 4 additions & 2 deletions lib/trackers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { App } from 'nuxt/dist/app/compat/vue-demi'

import { ApiPoiId } from './apiPois'

import { ApiMenuCategory, MenuItem } from '~/lib/apiMenu'
Expand Down Expand Up @@ -77,6 +79,6 @@ export type Event =
}

export interface Tracker {
consent(): void
track(event: Event): void
consent(app: App): void
track(app: App, event: Event): void
}
8 changes: 4 additions & 4 deletions plugins/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineNuxtPlugin((nuxtApp) => {
if (googleTagManagerId) {
trackers.push(
new Google(
nuxtApp,
nuxtApp.vueApp,
Boolean(config.COOKIES_CONSENT),
googleTagManagerId
)
Expand All @@ -27,7 +27,7 @@ export default defineNuxtPlugin((nuxtApp) => {
if (matomoUrl && matomoIdsite) {
trackers.push(
new Matomo(
nuxtApp,
nuxtApp.vueApp,
Boolean(config.COOKIES_CONSENT),
matomoUrl,
matomoIdsite
Expand All @@ -40,7 +40,7 @@ export default defineNuxtPlugin((nuxtApp) => {
tracking_consent: (): void => {
if (trackers.length > 0) {
trackers.forEach((tracker) => {
tracker.consent()
tracker.consent(nuxtApp.vueApp)
})
}
},
Expand All @@ -51,7 +51,7 @@ export default defineNuxtPlugin((nuxtApp) => {
}

trackers.forEach((tracker) => {
tracker.track(event)
tracker.track(nuxtApp.vueApp, event)
})
},
},
Expand Down

0 comments on commit 704a333

Please sign in to comment.