Fetching the Google (last) consents

Riccardo Malesani
4 min readMar 12, 2024

--

This article is about the Google Consent Mode API and the viewpoint of a former developer.

It’s been a while for me on this new technology, having experienced several ways of deployment in connection with different Consent Management Platforms (CMP), obviously delivered via GTM. But this article is not about the Consent Mode implementation. It’s on a requirement you may find yourself with later on, when it’s up and running: how to easily, quickly and surely fetch the most recent status of a specific consent storage ?

When a consent update command is fired, either by the CMP itself or by a custom template or event you set, the dataLayer object is filled with related arguments (consent storages), based on the Consent Mode API. This means no custom event, nor custom variables you can easily read from GTM, are available to you. More, this update command could be fired multiple times on the same page: think about a user changing preferences repeatedly or what happens in a single-page-application.

how to quickly and surely fetch the most recent status of a specific consent storage ?

This behaviour will result in a “consent update arguments sequence” in the dataLayer (among its other things). Let’s say the “analytics_storage” is denied on first, then updated to granted, then it’s denied again. The same might have happened for “ad_storage” or “personalization_storage” at the same time:

Let’s also say you need to know which is the most recent status of “analytics_storage”. You know it’s in the last “consent update” arguments here, at the very bottom of the sequence, but you cannot retrieve it…don’t believe me? I could have missed something, but there isn’t any Consent Mode API for that, nor a built-in way to read it via GTM variables or similar. I guess for Google it’s not a priority requirement for the users, but what if you need to develop your own logic in GTM for making something happened, based on the last status of a particular Consent Mode storage? That was my requirement, and what follows is my way to achieve it.

I built a custom JavaScript function, inspired by this article (thanks CookieBot) and called it consentStatus, that “stringifies” and loops the dataLayer, looking for the “consent update” members. When the wanted Google Consent Mode storage (the “analytics_storage” in my example) is found, the function creates an array of all its statuses in the sequence, and finally returns the last of them. Here it is — if you like, give it a try:

function consentStatus(storage) { 
var El = window["dataLayer"],
List = [],
Stringify = function(obj) {
var seen = [];
var output = JSON.stringify(obj, function(key, value) {
if (typeof value === "object" && value !== null) {
if (seen.indexOf(value) !== -1) return;
seen.push(value);
}
return value;
});
seen = null;
return output;
};
for (var i in El) {
if (Stringify(El[i])) var Value = Stringify(El[i]).replaceAll(/\"\d{1,}\":/g, "");
if (Value.match(/("consent","update"|"h"\:)/)) {
var Reg = new RegExp(storage + '_storage":"([^",]+)');
if (Value.match(Reg)) List.push(Value.match(Reg)[1]);

}

}
return List[List.length-1];
}

Of course you have to define this function in the most useful fashion, depending on how your website or application is built and how the pages are loaded. In my case, I included it in another HTML tag I always fire through GTM, but there are plenty of options, based on your final requirement.

How to use it ? Simply call it with the storage name you want the most recent consent status for. Just remember to omit the “_storage” part. You will pass “analytics” in place of “analytics_storage”, or “personalization” in place of “personalization_storage” (you can use it for the new Consent Mode v2 storages, as well).

That’s all! Thanks for reading till here and I hope this can help you with the Google Consent (missing) API. If you have suggestions, improvements or questions, just leave a comment below or contact me, I will be happy to catch up.

what if you need to develop your own logic in GTM for making something happened, based on the last consent status of a particular Consent Mode storage?

--

--