Introduction
Updated for 2026 compliance practices.
Google Analytics 4 Vue cookie consent is a practical compliance topic for website owners validating consent, tags, and disclosures. If you run a Vue.js application and use Google Analytics 4 (GA4), you need to ensure that tracking scripts respect user consent choices. This guide walks through the technical steps to implement cookie consent in a Vue project, block GA4 until consent is given, and verify everything works correctly. We focus on actionable implementation details, common pitfalls, and how to validate your setup using GDPRChecker’s scanning tools.
Important: This guide provides technical implementation guidance, not legal advice. For legal questions, consult a qualified professional. The requirements discussed are based on general principles of data protection law, particularly the GDPR, but your specific obligations may vary.
What Google Analytics 4 Vue Cookie Consent Means for Website Owners
When we talk about Google Analytics 4 Vue cookie consent, we refer to the technical and procedural measures needed to ensure that your Vue.js website only loads GA4 tracking scripts and sets cookies after obtaining valid user consent. Under regulations like the GDPR, analytics cookies are not strictly necessary for the core functionality of a website, so they require prior consent. This means you cannot fire GA4 tags on page load by default; you must wait for the user to take an affirmative action, such as clicking “Accept” on a cookie banner.
For Vue developers, this introduces specific challenges. Vue’s reactivity and component lifecycle can make it tricky to conditionally load external scripts. You need a consent management mechanism that integrates with Vue’s state management, and you must ensure that no GA4 network requests occur before consent. This includes not only the main analytics script but also any subsequent events or page views.
Beyond the technical setup, you also need to consider disclosure requirements. Your cookie banner should clearly inform users about the purposes of data collection, and your privacy policy must detail how GA4 processes personal data. Additionally, you should provide an easy way for users to withdraw consent. GDPRChecker’s scans can help verify that your banner appears correctly, that pre-consent network requests are blocked, and that your disclosures are in order.
Requirements and Compliance Expectations
Before diving into code, let’s clarify the key compliance expectations for GA4 on a Vue site. These are based on official guidance from Google and European data protection authorities.
Consent Must Be Prior and Granular
Consent must be obtained before any non-essential cookies are set or any personal data is processed. For GA4, this means you cannot initialize the analytics library or send any hits until the user has explicitly agreed. Moreover, consent should be granular: users should be able to accept analytics cookies separately from marketing cookies, for example.
Blocking Pre-Consent Requests
A common mistake is to load the GA4 script but delay sending hits. Even loading the script can set cookies or transmit data (like IP address) in the request headers. Therefore, you must block the script entirely until consent is given. This can be done by conditionally injecting the script tag or by using a tag manager that respects consent signals.
Consent Mode Integration
Google’s Consent Mode allows you to adjust how Google tags behave based on consent state. For GA4, you can send a default consent state of 'denied' and update it to 'granted' when the user consents. This ensures that even if a tag fires, it will operate in a cookieless mode until consent is given. However, Consent Mode alone does not replace the need for a consent banner; it’s a complementary mechanism. You can learn more in our guide on Google Consent Mode v2.
Cookie Banner and Privacy Policy
Your site must display a cookie banner that: - Informs users about the use of cookies and their purposes. - Obtains affirmative consent before setting non-essential cookies. - Provides a clear option to reject all non-essential cookies. - Links to a comprehensive privacy policy.
The privacy policy should disclose the use of GA4, the data it collects, and how users can exercise their rights. For more details, see our cookie banner requirements guide.
Ongoing Verification
Compliance is not a one-time task. After any changes to your Vue app, tag configuration, or consent setup, you should re-verify that everything works as expected. GDPRChecker’s scanner can automate this by checking for pre-consent requests, banner behavior, and disclosure gaps.
How to Implement Google Analytics 4 Vue Cookie Consent Step by Step
Now, let’s walk through a practical implementation. We’ll assume you have a Vue 3 project and want to use a consent management platform (CMP) or a custom solution. The steps below are framework-agnostic but tailored to Vue’s patterns.
Step 1: Choose a Consent Management Approach
You have two main options: 1. Use a third-party CMP: Many CMPs offer Vue-specific integrations or can be added via a script tag. They handle banner display, consent storage, and triggering of tags. 2. Build a custom consent solution: You create your own banner component and manage consent state in a Vuex/Pinia store or local storage.
For this guide, we’ll focus on a custom solution to illustrate the core concepts, but the principles apply to CMPs as well. If you’re unsure whether you need a CMP, check our article Do I Need a CMP if I Do Not Run Google Ads?.
Step 2: Set Up Consent State Management
Create a reactive consent state that persists across sessions. Using Pinia (or Vuex), define a store:
// stores/consent.js
import { defineStore } from 'pinia'
export const useConsentStore = defineStore('consent', {
state: () => ({
analytics: false,
marketing: false,
preferences: false,
hasInteracted: false
}),
actions: {
setConsent(type, value) {
this[type] = value
this.hasInteracted = true
localStorage.setItem('consent', JSON.stringify(this.$state))
},
loadConsent() {
const saved = localStorage.getItem('consent')
if (saved) {
this.$patch(JSON.parse(saved))
}
}
}
})
This store holds consent flags for different categories and persists them to localStorage. The hasInteracted flag indicates whether the user has made a choice, so you can decide whether to show the banner.
Step 3: Create a Cookie Banner Component
Build a Vue component that displays the consent options. It should appear only if hasInteracted is false. Use the store to update consent when the user clicks “Accept All”, “Reject All”, or saves custom preferences.
<template>
<div v-if="!consentStore.hasInteracted" class="cookie-banner">
<p>We use cookies to improve your experience. Accept or manage your preferences.</p>
<button @click="acceptAll">Accept All</button>
<button @click="rejectAll">Reject All</button>
<button @click="showPreferences = true">Customize</button>
<!-- Preferences modal -->
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useConsentStore } from '@/stores/consent'
const consentStore = useConsentStore()
const showPreferences = ref(false)
function acceptAll() {
consentStore.setConsent('analytics', true)
consentStore.setConsent('marketing', true)
consentStore.setConsent('preferences', true)
}
function rejectAll() {
consentStore.setConsent('analytics', false)
consentStore.setConsent('marketing', false)
consentStore.setConsent('preferences', false)
}
</script>
Ensure the banner is prominently displayed and that rejecting is as easy as accepting. The reject flow is critical: many implementations make it harder to reject than accept, which can be non-compliant.
Step 4: Conditionally Load GA4 Based on Consent
Now, you need to load the GA4 script only when analytics consent is true. You can do this in your main App.vue or a dedicated plugin. Use a watcher on the consent store:
<script setup>
import { watch } from 'vue'
import { useConsentStore } from '@/stores/consent'
const consentStore = useConsentStore()
watch(() => consentStore.analytics, (newVal) => {
if (newVal) {
loadGA4()
}
}, { immediate: true })
function loadGA4() {
// Only load if not already loaded
if (window.gtag) return
const script = document.createElement('script')
script.src = `https://www.googletagmanager.com/gtag/js?id=YOUR-MEASUREMENT-ID`
script.async = true
document.head.appendChild(script)
window.dataLayer = window.dataLayer || []
function gtag(){dataLayer.push(arguments)}
gtag('js', new Date())
gtag('config', 'YOUR-MEASUREMENT-ID')
}
</script>
This approach ensures no GA4 code runs until consent is granted. If the user has already consented (e.g., returning visitor), the watcher fires immediately on mount and loads the script.
Step 5: Implement Consent Mode (Optional but Recommended)
To further align with Google’s requirements, you can implement Consent Mode v2. This involves setting default consent states before any tags fire. Add this in your index.html or as early as possible in your Vue app:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied'
});
</script>
Then, when consent is granted, update the consent state:
gtag('consent', 'update', {
'analytics_storage': 'granted'
})
This way, even if the GA4 script loads before consent (which you should still avoid), it will respect the denied defaults. For a deeper dive, see our Google Consent Mode v2 guide and our Consent Mode v2 vs Google Certified CMP comparison.
Step 6: Handle Page Views and Events After Consent
Once GA4 is loaded, you need to send the initial page view and any subsequent events. If you’re using Vue Router, you can watch route changes and send page views:
import { watch } from 'vue'
import { useRouter } from 'vue-router'
import { useConsentStore } from '@/stores/consent'
const router = useRouter()
const consentStore = useConsentStore()
watch(() => router.currentRoute.value.fullPath, (to) => {
if (consentStore.analytics && window.gtag) {
gtag('config', 'YOUR-MEASUREMENT-ID', { page_path: to })
}
})
Make sure you don’t send any events before consent. If you have custom event tracking, wrap those calls in a check for consentStore.analytics.
Step 7: Provide a Way to Change Consent
Users must be able to withdraw consent easily. Add a link or button (e.g., “Cookie Settings”) that reopens the consent preferences. This can be a persistent element in your footer.
Common Mistakes and How to Avoid Them
Even with careful implementation, several pitfalls can undermine your compliance. Here are the most frequent issues and how to address them.
1. Loading GA4 Before Consent
The most critical mistake is firing the GA4 script or sending hits before the user has consented. This often happens when developers place the GA4 snippet directly in index.html without any conditional logic. Always use a consent gate before loading the script.
Verification: Use GDPRChecker’s scanner to check for pre-consent network requests. It will flag any requests to google-analytics.com or googletagmanager.com that occur before consent.
2. Ignoring the Reject Flow
Many implementations only handle the “Accept” path. If a user clicks “Reject”, you must ensure that no analytics cookies are set and that the GA4 script is not loaded. Test the reject flow thoroughly: after rejecting, refresh the page and confirm that no GA4 requests appear in the network tab.
3. Not Updating Consent Mode Defaults
If you use Consent Mode but forget to set the default to 'denied', Google tags may still set cookies even without consent. Always set the default before any tag fires.
4. Incomplete Privacy Policy Disclosures
Your privacy policy must mention GA4 specifically, including the types of data collected and the purposes. It should also explain how users can exercise their data subject rights. GDPRChecker’s scanner can identify gaps in your policy disclosures.
5. Forgetting About Subdomains or Embedded Content
If your Vue app loads content from other domains (e.g., embedded videos, social media widgets), those may also set cookies. Ensure your consent solution covers all third-party resources, not just GA4.
6. Relying Solely on Consent Mode Without a Banner
Consent Mode is not a replacement for a cookie banner. You still need to obtain explicit consent. Consent Mode only adjusts tag behavior; it doesn’t collect consent.
How to Validate Google Analytics 4 Vue Cookie Consent with GDPRChecker
After implementing consent, you must verify that everything works correctly. Manual testing can be error-prone, so using an automated scanner like GDPRChecker is highly recommended.
Pre-Consent Request Detection
GDPRChecker scans your site and identifies any network requests that occur before user consent. It will list all requests to Google domains, helping you confirm that GA4 is blocked until consent is given.
Banner Behavior Analysis
The scanner checks whether your cookie banner appears correctly, whether it offers a reject option, and whether it reappears if consent is not given. It also verifies that the banner doesn’t use dark patterns (e.g., pre-ticked boxes). See EDPB Guidelines 05/2020 on consent.
Disclosure Gap Identification
GDPRChecker analyzes your privacy policy and cookie banner text to ensure they include required disclosures about GA4. It can flag missing information, such as the absence of a data retention period or a link to Google’s privacy policy.
Post-Change Verification
Whenever you update your Vue app, tag configuration, or consent logic, run a new scan. This ensures that no regressions have occurred. GDPRChecker’s scans are designed to be part of your continuous compliance workflow.
To get started, run a free scan on your site and review the report. It will highlight any issues and provide actionable recommendations. For more on verifying Google Consent Mode specifically, see our Google Consent Mode v2 Checker guide.
Implementation Checklist
Use this checklist to ensure you’ve covered all bases:
- Consent state is managed in a reactive store (Pinia/Vuex) and persisted.
- Cookie banner is displayed on first visit and blocks interaction until a choice is made.
- Banner includes clear “Accept All”, “Reject All”, and “Customize” options.
- Rejecting is as easy as accepting (no dark patterns).
- GA4 script is loaded only after analytics consent is granted.
- Consent Mode defaults are set to
'denied' before any tags fire. - Consent Mode is updated to
'granted' when consent is given. - Page views and events are only sent after consent.
- A persistent “Cookie Settings” link allows users to change consent.
- Privacy policy discloses GA4 data collection and user rights.
- Pre-consent network requests are verified using GDPRChecker scanner.
- Full reject flow is tested: no GA4 requests after rejection.
FAQ
What is Google Analytics 4 Vue cookie consent?
Google Analytics 4 Vue cookie consent refers to the technical implementation of obtaining and respecting user consent before loading GA4 tracking scripts in a Vue.js application. It involves conditionally loading GA4 based on user choices, typically managed through a cookie banner and consent state management.
Do I need Google Analytics 4 Vue cookie consent for GDPR?
Yes, if your Vue site targets users in the European Economic Area, you must obtain prior consent before setting GA4 cookies, as they are not strictly necessary. This applies regardless of whether you use a CMP or a custom solution.
How do I implement Google Analytics 4 Vue cookie consent?
Implement by: 1) creating a consent state store, 2) building a cookie banner that sets consent flags, 3) conditionally loading the GA4 script only when analytics consent is true, and 4) optionally integrating Google Consent Mode. Always test the reject flow.
How can I verify Google Analytics 4 Vue cookie consent with a scanner?
Use GDPRChecker to scan your site. It checks for pre-consent network requests to Google domains, verifies banner behavior, and identifies disclosure gaps in your privacy policy. Run scans after any changes to maintain compliance.
What are common Google Analytics 4 Vue cookie consent mistakes?
Common mistakes include loading GA4 before consent, ignoring the reject flow, not setting Consent Mode defaults to denied, incomplete privacy policy disclosures, and forgetting about third-party cookies from embedded content.
Next step
Run a GDPRChecker scan to validate consent behavior, trackers, and disclosures after you implement the checklist above.
Copyright and editorial notice
© GDPRChecker
This original AI-assisted editorial draft was selected, reviewed, and published by GDPRChecker. All rights are reserved where protected by applicable law. Do not reproduce the article without permission.