Reading the Consent Status
This guide describes, among other things, how to retrieve the visitor’s saved consent status in order to use it for running your own scripts on the website.
Alternatively, you can also execute your own JavaScript code via a “Custom” tag in the etracker tag manager or modify the code so that it is executed only when called by a “Custom” tag in the etracker tag manager after the appropriate consent has been granted (requires etracker analytics Enterprise Edition).
To ensure that embedded third-party services are used in compliance with data protection regulations, the Content Blocker only loads the contents after the user has given consent. However, this built-in feature requires that the contents be embedded in the website via an iFrame. To ensure that services integrated directly into the website also comply with data protection regulations by requiring the visitor’s consent, the necessary information can be read from the DataLayer.
For example, you should only launch an integrated video player, map widgets, or chat tools once you have received specific consent to do so.
The etracker consent manager can therefore make visitors’ consent data available not only to the etracker tag manager, but also to your own custom JavaScript or other tag management systems integrated into your website.
Functionality
The etracker consent manager writes an entry to the ` window.dataLayer` array in two situations:
- Each time the page is loaded, using the currently saved status.
- Whenever a change occurs—that is, whenever a visitor changes their selection in the consent dialog or unblocks iFrame contents via the overlay.
The entry is called ” etConsentStatus.” The ” action ” field tells you which of the two situations applies (onInitialPageLoad / onUpdateServices).
If the visitor has not yet made a decision, the entry initially contains only event and action:
{
"event": "etConsentStatus",
"action": "onInitialPageLoad"
}
For each service for which a decision has been made, the entry contains a key consisting of the consent category and provider ID (e.g., performance:youtube) with the value true (consent granted) or false (consent denied).
Example of an entry when a page is loaded with a saved decision:
{
"event": "etConsentStatus",
"action": "onInitialPageLoad",
"statistics:etracker": false,
"performance:youtube": false,
"marketing:123456": false
}
Example of the entry after saving in the consent dialog:
{
"event": "etConsentStatus",
"action": "onUpdateServices",
"statistics:etracker": true,
"performance:youtube": true,
"marketing:123456": false
}
An Overview of the Fields
| Field | Type | Values |
event | String | always etConsentStatus |
action | String | onInitialPageLoad when the page is loaded, onUpdateServices after a change |
<kategorie>:<anbieter-id> | Boolean | true = Consent granted, false = Consent denied;The key is missing until a decision has been made. For standard providers predefined by JustRelate etracker, the ID is the provider’s name (youtube, …). For custom providers, however, a numeric ID is used. |
Distinguish between three states:
truemeans “Consent granted”falsemeans “Consent denied”- If there is no key, it means the visitor hasn’t made up their mind yet.
Therefore, do not treat a missing key as false!
Here’s How to Find the Keys to Your Services
The category is the service’s consent category:
- Marketing:
marketing - Functional:
performance - Statistics:
statistics - Required:
no_consent
You can find the provider ID in the etracker tag manager under “My Providers ” in the “ID” column. It can be a name such as “youtube” (for predefined default providers) or a number (for custom providers).
The quickest way to view your website’s keys is to read the stored status in the browser console. To ensure that ALL entries are displayed, click the “Allow All” button in the consent dialog first.
localStorage.getItem('et_oi_services');
The value has the same keys as the dataLayer entry. For more information, see ” Cookies Used.”
A service will only appear in the consent dialog and in the status once a published tag has been assigned to it in the etracker tag manager. Simply creating a provider under “My Providers” is not sufficient on its own.
Adding a provider using a pseudo-tag in the etracker tag manager
To have a provider appear in the consent dialog, create an empty “Custom” pseudo-tag (requires etracker analytics Enterprise Edition) and assign the desired consent category and the corresponding service provider to the tag. It is sufficient to simply publish the tag; it does not need to be triggered.
If a visitor revokes consent, the tag will no longer be executed. However, a service that has already started will continue to run until the page is reloaded. If you want to stop it immediately, use a trigger on the dataLayer entry, as described in the next section.
Responding Indirectly to Consent Status Using the etracker Tag Manager
You only need the dataLayer entry in the etracker tag manager if a tag needs to read the consent status instead of being directly linked to it (as is usually the case):
- If a request is rejected or revoked, a one-day grace period should be allowed—for example, to remove a running player or integrate an alternative placeholder into the website.
- A day should depend on the status of a service other than the one to which it is linked.
To do this, follow these steps:
- Create a variable. Create a variable of type ” Datalayer variable ” and enter the service key (e.g., ” Datalayer variable name “) in the “Datalayer variable name” field.
performance:youtube) one. Activate the option ” Search only current item ” so that the variable only reads the triggering entry. - Create a trigger. Create a “Data Layer Push” trigger with the condition
eventIst gleichetConsentStatus. Under “Trigger applies to (all conditions must be met),” enter the variable from Step 1 asa“Custom Event” with the value to which the tag should respond, for example,Ist gleichfalse.
If you want the tag to respond only to a change and not when the page is first loaded, create another etracker variable for the DataLayer variableaction. In the trigger, set the following additional condition for this variable:Ist gleichonUpdateServices. - Create a tag. Since the tag must run even if consent is denied, it is assigned the “None” consent category. By assigning the trigger from Step 2, it will always be executed whenever the consent status changes—as in our example, to ”
false.”
A tag with a trigger set to ` etConsentStatus ` is executed every time a matching entry is found in the DataLayer. If a visitor gives consent multiple times during the same page view or changes their selection multiple times, the tag is fired accordingly. Make sure in your code that repeated calls are harmless.
A missing key does not meet the condition Ist gleich false. As long as the visitor has not actively denied consent, such a trigger will not fire.
Use the consent status for your own JavaScript
If your code isn’t in the etracker tag manager but is embedded directly in the website’s code, you can read the dataLayer yourself. The following pattern first processes the consent entries that were saved before your script started, and then responds to new entries. Include it after the etracker code.
window.dataLayer = window.dataLayer || [];
(function () {
var state = null; // null = no status has been read yet
var lastAction;
var listeners = [];
function apply(entry) {
if (!entry || entry.event !== 'etConsentStatus') return;
var next = {};
Object.keys(entry).forEach(function (key) {
if (key.indexOf(':') > -1) next[key] = entry[key];
});
state = next;
lastAction = entry.action;
listeners.forEach(function (fn) { fn(state, entry.action); });
}
// 1. Entries Already Written
window.dataLayer.forEach(apply);
// 2. Later Entries
var originalPush = window.dataLayer.push;
window.dataLayer.push = function () {
var result = originalPush.apply(this, arguments);
Array.prototype.forEach.call(arguments, apply);
return result;
};
window.etConsent = {
// true = consent, false = decline, undefined = no decision
get: function (key) { return state ? state[key] : undefined; },
onChange: function (fn) {
listeners.push(fn);
if (state) fn(state, lastAction);
}
};
})();
Here’s how to set it up so that a video player starts only after consent is given, and only once:
window.initVideoPlayer = window.initVideoPlayer || (function () {
var started = false;
return function () {
if (started) return;
started = true;
// Initialize the player here
};
})();
etConsent.onChange(function (status) {
if (status['performance:youtube'] === true) {
window.initVideoPlayer();
}
});
You can also read the saved status from localStorage at any time, for example, before you want to render a placeholder:
var consent = {};
try {
consent = JSON.parse(localStorage.getItem('et_oi_services') || '{}');
}
catch (e) {
consent = {};
}
var youtubeAllowed = consent['performance:youtube'] === true;
localStorage does not notify you of changes. Therefore, use it for the initial state and the dataLayer for changes.
Use the consent status for other tag management systems
If you use the etracker consent manager in conjunction with a tag management system from another provider, set up a trigger there for the dataLayer event ` etConsentStatus ` and, following these instructions, read the keys for your services as dataLayer variables. In the condition, check for the value ` true`, and do not treat a missing key as ` false `.
Testing Guidelines
- In the etracker tag manager’s debug mode, use the URL parameter `
et_debug=1` to test whether your tag runs exactly once when the page is loaded, after consent is given without reloading, and after a page reload. - You can test how a tag behaves after revocation during the same page load using published tags without debug mode. In debug mode, a tag may be executed one additional time after revocation.
- To set the status to “no decision,” remove
et_oi_servicesandet_allow_cookiesfrom localStorage and from your domain’s cookies, and then reload the page. - Use only the entry `
etConsentStatus` to control your logic. Any other entries that the etracker consent manager writes to the dataLayer are not part of this interface.