Implement consent mode
💡 Consent mode is a complicated subject... In this article, we'll try to shed some light on how to implement it efficiently with CMP and your dataLayer.
👨💻 Since we love open source, here are a few additional resources:
Code snippets are available on our newly opened GitHub directory.
starfox-snippets/axeptio_consentMode.html at main - Starfox-Analytics/starfox-snippets Useful code extracts concerning tracking implementations - starfox-snippets/axeptio_consentMode.html at main - Starfox-Analytics/starfox-snippets
📔 S tay tuned for our next publications!
- How talking to a developer can help us be more efficient on a branding plan
- How to manage media tags across multiple sites and languages in a single GTM variable...
What is consent mode
This is the equivalent of Analytics solutions consent-exempt solutions but with a Google twist.
The idea remains the same: if a user refuses the cookie banner, we send anonymized data (i.e. not linked to a cookie):
- In the case of exempted solutions, these are pings that are directly integrated into the interfaces, without the possibility of tracing the user's path. For example, you'll be able to see all your arrival sources, but only the successive pages viewed by a given user.
- In the consent mode, an anonymized ping is sent to model the data, rather than storing it directly. The idea is that, after a learning period during which the solution receives 100% of the data, the restrictions of the cookie banner are applied, and the solution is then able to model the remaining data fairly well.
It's worth noting that the solution was first implemented for Google Ads, as this is where the business stakes are highest in terms of steering media spend as precisely as possible. There may also be conditions on the volume of traffic and conversions required for learning to take place.
For further information:
- google support site for business explanations
- the google developer site for integration
But how do you implement it?
Apparent complexity
Spending a little time on the subject of implementation soon raises the classic question of implementation via GTM. Several articles exist, including one by Simo which details the use of a custom tag template.

There are several points to bear in mind when configuring your system!
- create a tag for initialization and another for consent update
- in addition, entanglement with the GTM loading sequence and the tags is an issue.
- Another complex issue is updating the consent mode: if it is triggered by an event in the
dataLayer
pushed by a CMP, like aactivate_google_analytics
In this case, you'll have to wait for the next message before the consent mode is taken into account, or you'll have to sequence the tags that are also triggered by this message.
The code to the rescue, yes sir!
It's always good to understand what's going on under the hood. As with Google Analytics tracking, the technical specification is the source of truth, and it just so happens that the consent mode specification (available at the following link following link) is pretty straightforward!
In fact, we can see that only two bits of code are needed to set up the consent mode. The first to initialize the parameters, the second to update them when cookies are accepted or rejected.
// Initialisation au plus tôt dans la page
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied'
});
// Update lors de l'acceptation ou refus des cookies
gtag('consent', 'update', {
'ad_storage': 'granted'
});
This puts the GTM custom template solution in a different light: it removes the need for this code configuration, at the cost of additional GTM complexity. What's more, we're only talking about a few lines of code: knowing how to manage them on the developer's side should be beneficial.
At Starfox we're not afraid to look code in the face and talk to a developer. In a previous article article that it was possible to control the sequence of GTM / CMP / dataLayer loads. And it just so happens that the consent mode fits perfectly into this logic.
As a reminder, each CMP has methods for indicating that a choice has been made: this is exactly what we need to integrate the consent mode update. An example with Axeptio below, with the code of the previous updated article
window._axcb.push(function(axeptio) {
axeptio.on("cookies:complete", function(choices) {
var consentSettings = {
ad_storage: !!choices['Google_Ads'] ? 'granted' : 'denied',
analytics_storage: !!choices['google_analytics'] ? 'granted' : 'denied',
};
gtag("consent", "update", consentSettings);
dataLayer.push({ event: 'page_view' });
envoiEventsSupplementaires();
});
});
You'll find a sample html page to test on our GitHub at the link below. You'll see that when you play with the Google Analytics / Google Ads consents, the GA4 hit updates nicely.
starfox-snippets/axeptio_consentMode.html at main - Starfox-Analytics/starfox-snippets
Useful code extracts concerning tracking implementations - starfox-snippets/axeptio_consentMode.html at main - Starfox-Analytics/starfox-snippets
Below are the hits that should be triggered if you test the html file yourself:


A need, a question?
Write to us at hello@starfox-analytics.com.
Our team will get back to you as soon as possible.