Skip to main content

Condition a GTM tag to fire only when a specific event occurs

Written by Alexandre Dias Da Silva

This article is part of the series on cookie blocking using Google Tag Manager.

Before following the guide below, make sure you have reviewed the introductory article.

Are you instead looking to condition consent for a tag that should fire on all pages? This way!

In cases where you have tags that fire on particular events (on a link click, on a custom event, etc.), you can use the Axeptio variable as a condition. This will allow you to control consent on your existing triggers.

Does your tag fire on a custom event (add to cart, purchase, etc.)? Please refer to the Special case: firing on a custom event section below.

Remember what we said in the introductory article – here is what we are trying to do:

The tag should fire when the event occurs EXCEPT if consent has not been obtained.

To translate this "EXCEPT" into GTM, you will add an exception to your tag.

In GTM, an exception is a special trigger that prevents a tag from firing, even if the other firing conditions are met.

And so that we can know whether consent has been obtained or not, GTM will need to read the content of the axeptio_authorized_vendors variable that Axeptio sends to GTM. To do this, we will start by creating a new variable.

Install the Axeptio Consent State variable template

Our Axeptio Consent State variable template, available in the GTM gallery, allows you to easily retrieve the list of services for which the user has given consent.

First, you need to get the variable template from the Google Tag Manager gallery:

  1. Open your GTM container.

  2. In the left menu, click on Templates.

  3. At the bottom of the page, in the Variable templates block, click the Search gallery button in the top right of the block.

  4. In the panel that appears, click the magnifying glass in the top right, and search for the variable template named Axeptio Consent State.

  5. In the top right, click the Add to workspace button.

  6. Click Add to confirm.

✅ The template is now installed and visible in your list of variable templates.

Create a variable from the template

It is now possible for you to create a variable from this template:

  1. In the left menu, click on Variables.

  2. At the bottom of the page, in the User-Defined Variables block, click the New button in the top right.

  3. In the panel that appears, click inside the Variable Configuration block.

  4. In the panel that appears, in the Custom templates category, click on Axeptio Consent State.

  5. Click the Save button. You can keep the default variable name (Axeptio Consent State), or rename it.

✅ Once the variable is created, you can add conditions to your existing triggers.

Create the trigger

  1. In the left menu, click on Triggers.

  2. In the Triggers block, click the New button in the top right.

  3. In the panel that appears, click inside the Trigger Configuration block.

  4. In the panel that appears, in the Other category at the bottom of the list, click on Custom Event.

  5. In the Event name field, enter .*.

  6. Check the Use regular expression matching checkbox.

  7. Under Conditions when this trigger fires:, select Some custom events.

  8. Under Fire this trigger when an event occurs and all of these conditions are true, add the following condition by filling in the 3 fields as follows:

  9. Click the Save button. You can name your trigger "Axeptio Exception - <service name>".

Add the trigger as an exception

  1. In the left menu, click on Tags.

  2. Click inside the Firing block.

  3. In the block, click Add an exception.

  4. In the panel that appears, select the trigger you just created.

  5. Click the Save button.

If your tag does not fire

You have added the exception to your tag and tested its firing in GTM preview mode, but it does not fire?

Several things to check:

  1. Have you properly added the service in question to your cookie banner?

  2. Have you properly accepted the service in your cookie banner (either by accepting all cookies via the "OK for me" button, or by specifically accepting the service in question via the "I choose" button)?

  3. Did the event your tag is listening for (purchase, click, etc.) actually occur? Do you see it in the list of events in the left column of Tag Assistant?

  4. If yes, at what point was the event sent in relation to the axeptio_update event? If you see that your event is sent before the axeptio_update event, then it is normal that the tag could not fire. Indeed, GTM evaluates the firing conditions at the moment your event occurs. However, if axeptio_update has not yet occurred, then Axeptio has not yet been able to populate the list of services for which the user has given consent, which triggers the exception and blocks the tag from firing. To resolve the issue, try using a trigger group instead of an exception, as described in the section below.

Special case: firing on a custom event

In the case of a tag that you fire on a custom event (add to cart, purchase, etc.), that your site sends to the dataLayer, prefer using a trigger group instead of an exception.

Indeed, some systems (PrestaShop or WooCommerce in particular) send the event to GTM very early in page loading, before the Axeptio variable has even been populated at the time of axeptio_update.

The trigger group guarantees that the tag fires as soon as your event occurs AND consent has been obtained, whether your event occurs before or after axeptio_update.

Example of a tag firing on a specific click, with a trigger group:

Details of the trigger group, including checking the consent status by listening for the axeptio_activate_google_analytics event:

Did this answer your question?