Skip to content

Commit

Permalink
Enhance Google Analytics and Consent Tracking (#241)
Browse files Browse the repository at this point in the history
Add checks for Google Analytics ID and consent mode before tracking page
views. Also introduce verification for Google Tag Manager setting before
displaying the consent modal.
  • Loading branch information
Gcolon021 authored Oct 3, 2024
1 parent 5489d17 commit 94b0bdf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ VITE_AUTH_PROVIDER_MODULE_GOOGLE_ALT=false
# Variant Explorer Settings
# VITE_VARIANT_EXPLORER_TYPE=aggregate
# VITE_VARIANT_EXPLORER_MAX_COUNT=10000
# VITE_VARIANT_EXPLORER_EXCLUDE_COLUMNS=["AC","AN"]
# VITE_VARIANT_EXPLORER_EXCLUDE_COLUMNS=["AC","AN"]

VITE_GOOGLE_ANALYTICS_ID=someid
VITE_GOOGLE_TAG_MANAGER_ID=someid
5 changes: 4 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ VITE_AUTH_PROVIDER_MODULE_FENCE_ALT=true
# Variant Explorer Settings
VITE_VARIANT_EXPLORER_TYPE=aggregate
VITE_VARIANT_EXPLORER_MAX_COUNT=20
VITE_VARIANT_EXPLORER_EXCLUDE_COLUMNS='[]'
VITE_VARIANT_EXPLORER_EXCLUDE_COLUMNS='[]'

VITE_GOOGLE_ANALYTICS_ID=someid
VITE_GOOGLE_TAG_MANAGER_ID=someid
13 changes: 10 additions & 3 deletions src/lib/components/tracking/GoogleAnalytics.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
let googleAnalyticsID = settings.google.analytics;
$: {
if (browser && typeof gtag === 'function') {
if (
googleAnalyticsID &&
browser &&
typeof gtag === 'function' &&
localStorage.getItem('consentMode')?.includes('granted')
) {
console.debug('Tracking page view with Google Analytics');
// Send page view to Google Analytics
gtag('config', googleAnalyticsID, {
Expand Down Expand Up @@ -41,6 +46,8 @@
</script>

<svelte:head>
<script async src="https://www.googletagmanager.com/gtag/js?id={googleAnalyticsID}">
</script>
{#if googleAnalyticsID}
<script async src="https://www.googletagmanager.com/gtag/js?id={googleAnalyticsID}">
</script>
{/if}
</svelte:head>
5 changes: 3 additions & 2 deletions src/lib/components/tracking/GoogleConsents.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import { onMount } from 'svelte';
import { gtag } from 'gtagjs';
import { branding } from '$lib/configuration';
import { branding, settings } from '$lib/configuration';
let googleTag = settings.google.tagManager;
$: googleConsentVisible = false;
function setUsersGoogleConsent(wasAccepted: boolean) {
Expand All @@ -28,7 +29,7 @@
});
</script>

{#if googleConsentVisible && branding?.privacyPolicy?.url && branding?.privacyPolicy?.title}
{#if googleTag && googleConsentVisible && branding?.privacyPolicy?.url && branding?.privacyPolicy?.title}
<div
data-testid="consentModal"
class="fixed"
Expand Down

0 comments on commit 94b0bdf

Please sign in to comment.