Implement a GDPR Cookie Consent Banner on Shopify

Erik ChenErik Chen
4 min read

Below is a step‐by‐step guide on how to configure your Shopify site—with Google Tag Manager (GTM) triggering Google Analytics (GA)—so that you can implement a GDPR-compliant cookie consent. In this setup, if a user declines tracking cookies, GA won’t fire (or will run in a restricted “denied” mode), helping you stay compliant.


a. Choose a Consent Solution:
You can either use a Shopify app (such as GDPR/CCPA + Cookie Management or Cookiebot) or add custom code that displays a cookie consent banner. Many of these apps allow users to accept or decline non-essential cookies.

b. Configure the Banner:

  • Ensure the banner clearly explains which cookies are used (e.g., for analytics).

  • Provide clear “Accept” and “Decline” options.

  • If possible, allow users to change their consent later.

c. Push Consent Events to the Data Layer:
For seamless integration with GTM, modify your consent solution (or use its built-in integration) so that when a user makes a choice, a dataLayer event is pushed. For example:

<script>
  // When a user accepts cookies
  dataLayer.push({
    'event': 'cookieConsent',
    'cookieConsentState': 'granted'
  });

  // When a user declines cookies
  dataLayer.push({
    'event': 'cookieConsent',
    'cookieConsentState': 'denied'
  });
</script>

This will allow GTM to read the user’s choice and control tag firing accordingly.


Google’s Consent Mode allows you to adjust how tags behave based on the user’s consent. Here’s how to integrate it:

  1. Set Default Consent Settings Early:
    Place this snippet (before your GTM container code) in your Shopify theme’s <head> section:

     <script>
       window.dataLayer = window.dataLayer || [];
       function gtag(){dataLayer.push(arguments);}
       // Set default state to denied
       gtag('consent', 'default', {
         'ad_storage': 'denied',
         'analytics_storage': 'denied'
       });
     </script>
    
  2. Update Consent on Acceptance:
    When a user accepts cookies, update the consent status. This can be done by adding a small script that runs after the consent banner registers an “accept” action:

     <script>
       // Update consent when user accepts
       gtag('consent', 'update', {
         'analytics_storage': 'granted'
       });
     </script>
    

This ensures that unless the user explicitly consents, Google Analytics (and any other tags) run with minimal data collection.

b. Use Custom Triggers in GTM Based on Data Layer Variables:

If you are not using Consent Mode or want additional control, you can set up custom triggers:

  1. Create a Data Layer Variable:

    • In GTM, go to Variables > New > Data Layer Variable.

    • Name it something like dlv - cookieConsentState and set the Data Layer Variable Name to cookieConsentState.

  2. Modify Your Google Analytics Tag Trigger:

    • Edit your GA tag (or create a new one) in GTM.

    • In the “Triggering” section, add a condition that checks that dlv - cookieConsentState equals granted.

This ensures the GA tag fires only if the user has given consent.

  1. Ensure a Default State:

    • If no consent decision has been made, you might want to prevent firing. Use a blocking trigger or set the default value of your data layer variable to denied until updated.

3. Configure Your GA Tag in GTM

  • For GA4 (Google Analytics 4):
    When setting up your GA4 Configuration tag, ensure that any fields related to cookies are controlled by the consent status. With Consent Mode in place, GA4 will automatically adjust data collection.

  • For Universal Analytics:
    Similar to GA4, ensure your GA tag only fires when consent is granted. Additionally, consider setting the field allowAdFeatures to false when consent is denied.


4. Test Your Implementation

Before going live, test your configuration:

  • Preview Mode in GTM:
    Use GTM’s preview mode to simulate the user journey. Check that the GA tag only fires when cookieConsentState is granted.

  • Browser Testing:
    Clear your cookies and test both accepting and declining. Verify that when a user declines, no tracking cookies (or only minimal ones) are set.

  • Use Tag Assistant or GA Debugger:
    Tools like the Google Tag Assistant or GA Debugger can help confirm that GA is not sending any personal data when consent is declined.


5. Additional Tips

  • Documentation & Updates:
    Keep an eye on updates from Google regarding Consent Mode and GA integrations. The implementations may evolve as privacy regulations and best practices update.

  • User Revocation:
    Ensure your consent solution provides a method for users to change their cookie settings later, and update GTM accordingly if the status changes.

  • Privacy Policy:
    Update your privacy policy to clearly explain how cookies are used and how users can control them.


By following these steps, you can configure your Shopify site so that Google Analytics is only triggered when a user gives consent, ensuring GDPR compliance. This setup prevents tracking when users decline cookies and leverages GTM’s flexibility to conditionally fire tags based on the consent state.

0
Subscribe to my newsletter

Read articles from Erik Chen directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Erik Chen
Erik Chen