Skip to content

Commit

Permalink
fix(gtag): correct initialization update
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli committed Sep 29, 2024
1 parent 7b60fec commit 2a5b6b1
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/composables/useVioGtag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { GTAG_COOKIE_ID } from '../utils/constants'

export const useVioGtag = () => {
const {
gtag,
Expand All @@ -8,31 +6,42 @@ export const useVioGtag = () => {
enableAnalytics,
} = useGtag()
const cookieControl = useCookieControl()
const updateConsent = ({ isGranted }: { isGranted: boolean }) => {
gtag('consent', 'update', {
// // the following are denied per default in the gtag module configuration
// ad_user_data: 'denied',
// ad_personalization: 'denied',
// ad_storage: 'denied',
analytics_storage: isGranted ? 'granted' : 'denied',
})
}
const enableGtag = () => {
updateConsent({ isGranted: true })
initializeGtag()
enableAnalytics()
}
const disableGtag = () => {
updateConsent({ isGranted: false })
disableAnalytics()
}

if (cookieControl.cookiesEnabledIds.value?.includes(GTAG_COOKIE_ID)) {
initializeGtag()
enableGtag()
}

watch(cookieControl.cookiesEnabledIds, (current, previous) => {
if (
!previous?.includes(GTAG_COOKIE_ID) &&
current?.includes(GTAG_COOKIE_ID)
) {
gtag('consent', 'update', {
ad_user_data: 'denied',
ad_personalization: 'denied',
ad_storage: 'denied',
analytics_storage: 'granted',
})
initializeGtag()
enableAnalytics()
enableGtag()
}

if (
previous?.includes(GTAG_COOKIE_ID) &&
!current?.includes(GTAG_COOKIE_ID)
) {
disableAnalytics()
disableGtag()
}
})
}

0 comments on commit 2a5b6b1

Please sign in to comment.