WPConsent Documentation

Documentation, Reference Materials, and Tutorials for WPConsent

How to Trigger Google Tag Manager Tags Based on User Consent

This guide will walk you through the process of configuring Google Tag Manager (GTM) to fire your tracking tags based on the specific consent choices a user makes through the WPConsent plugin.

When a user saves their preferences using the WPConsent banner, the plugin pushes a custom event to the GTM Data Layer. You can use this event, along with the consent choices for both broad categories (like marketing) and specific services (like Facebook Pixel), to create precise firing rules for your tags, ensuring you remain compliant with privacy regulations like GDPR.

The process involves three main steps:

  1. Creating a Custom Event Trigger to listen for the consent event.
  2. Creating Data Layer Variables to read the user’s specific choices.
  3. Configuring Your Tags to use the new trigger and variables as firing conditions.

Step 1: Create the Custom Event Trigger

First, you need to create a trigger in GTM that listens for the specific event sent by WPConsent after a user interacts with the consent banner.

  1. In your GTM container, navigate to Triggers and click New.
  2. Name your trigger something descriptive, like Event - Consent Processed.
  3. Click on Trigger Configuration and choose the trigger type Custom Event.
  4. In the Event name field, enter exactly: wpconsent_consent_processed.
  5. Leave the trigger to fire on “All Custom Events”. We will add specific conditions directly to the tags later.
  6. Click Save.

You now have a trigger that will activate whenever the WPConsent plugin records a user’s consent choice.


Step 2: Create Data Layer Variables

The WPConsent plugin doesn’t just send an event; it also sends an object containing the user’s specific preferences. You need to create variables in GTM to capture these values so you can use them in your trigger conditions.

Let’s create variables for the main marketing and statistics consent categories.

  1. In your GTM container, navigate to Variables.
  2. Under User-Defined Variables, click New.
  3. Name the variable DLV - Consent - Marketing.
  4. Click on Variable Configuration and choose the variable type Data Layer Variable.
  5. In the Data Layer Variable Name field, enter wpconsentPreferences.marketing. This dot notation tells GTM to look inside the wpconsentPreferences object for the marketing key. The value will be either true or false.
  6. Click Save.
  7. Repeat the process for the statistics category:
    • Name: DLV - Consent - Statistics.
    • Type: Data Layer Variable.
    • Data Layer Variable Name: wpconsentPreferences.statistics.
    • Click Save.

If you have configured WPConsent to allow users to toggle individual services, you can capture consent for each one. The process is the same, but the variable name changes.

For example, let’s say you have a service with the ID facebook_pixel configured in the plugin settings. To capture consent specifically for this service:

  1. Create a New User-Defined Variable.
  2. Name it DLV - Consent - Facebook Pixel.
  3. Choose Data Layer Variable as the type.
  4. For the Data Layer Variable Name, use wpconsentPreferences.facebook_pixel.
    • Important: The key (facebook_pixel in this case) must exactly match the Service ID you have configured in the WPConsent plugin settings.
  5. Click Save.

You can repeat this for any individual service you need to control, such as Google Ads, Hotjar, etc.


Now you can combine the trigger and variables to control when a specific tag (like a Google Analytics 4 tag or a Meta Pixel) should fire.

In this example, we’ll configure a Meta (Facebook) Pixel tag to fire only when the user has given consent specifically for it.

  1. Navigate to Tags and select the Meta Pixel tag you want to configure.
  2. Click on the Triggering section.
  3. Remove any existing triggers that fire the tag on page load, such as “All Pages”. This is a critical step to prevent the tag from firing before consent is given.
  4. Click to add a new trigger and select the custom event trigger you created in Step 1 (Event - Consent Processed).
  5. After adding the trigger, choose to have it fire on Some Custom Events.
  6. Configure the condition as follows:
    • Select the service-specific variable you created: DLV - Consent - Facebook Pixel.
    • Select the operator: equals.
    • Enter the value: true.
  7. Click Save on the trigger configuration, and then Save the tag.

Your Meta Pixel tag is now configured to fire only when the wpconsent_consent_processed event occurs AND the user has explicitly consented to the facebook_pixel service. You can follow the same logic for your category-based tags (e.g., using DLV - Consent - Marketing equals true) or any other service-specific tags.


Step 4: Test Your Configuration

It’s crucial to test your setup using GTM’s Preview mode.

  1. Click the Preview button in your GTM workspace and enter your website’s URL.
  2. On your website, interact with the WPConsent banner. For a thorough test, accept a specific service (like Facebook Pixel) but decline the general category it might be in.
  3. In the GTM Preview debug panel, you should see the wpconsent_consent_processed event appear in the left-hand summary list after you save your preferences.
  4. Click on this event.
  5. Go to the Variables tab in the debug panel. Check that your variables (DLV - Consent - Marketing, DLV - Consent - Facebook Pixel, etc.) show the correct true/false values based on your selection.
  6. Check the Tags tab. You should see that your Meta Pixel tag Fired, while other tags you did not consent to remained in the Tags Not Fired section.

If the tags behave as expected, your configuration is successful. You can now publish your GTM container.

Was this article helpful?

Related Articles