Skip to main content

Debug mode and Google tag order

Google tags read the consent state when they start. If they start too early, they can read the wrong state. This page explains the warning Avacy prints in the browser console, how to turn on debug mode, and what to do when a tag is late.

NOTE: This page applies to the new Avacy banner (v3). The previous banner does not run this check and has no window.Avacy.

Google tag order​

Why the order matters​

When the Avacy core starts, it checks whether a Google tag (gtag.js, Google Tag Manager, analytics.js) ran before:

  • the Google Consent Mode default call, or
  • the TCF stub.

A Google tag that runs earlier may read the wrong consent state.

The check runs once, when the core starts. It runs only if the configuration has Google Consent Mode or TCF. Without either, it prints nothing. It never blocks the consent update.

The console warning​

When a Google tag is late, the console always shows a warning that starts like this, even with debug mode off:

[Avacy] Google tags ran before the Consent Mode default or the TCF stub: Google may have read the wrong consent state. …

The warning gives the steps described below in "What to do if the result is LATE" and links to this page.

The warning appears at most once per page.

If the page uses Google Tag Manager and TCF, the message is different. See GTM and TCF.

Turn debug mode on and off​

Debug mode prints more detail in the console, including the result of this check. Open the browser console and run:

window.Avacy.debugModeOn();

The call saves the flag in localStorage (key avacy:debug) and reloads the page. To turn it off:

window.Avacy.debugModeOff();

You can also turn it on with a configuration that has the console logger.

PASS and LATE​

With debug mode on, the console always prints one result.

  • PASS: no Google tag ran before the Consent Mode default or the TCF stub.
  • LATE: a Google tag ran first. The warning above is printed too.

The line also shows the data behind the result. It contains the time of the check in ms, the position of the default and of the first Google command in the dataLayer, the Google consent state and the TCF stub state.

Example:

Google tag order: PASS (check at 412 ms; dataLayer: default #0, first Google command #1; Google consent state (google_tag_data.ics): usedDefault true, Google wasSetLate: false; Google scripts already downloaded (info only): none; TCF stub: not needed)

How the result is decided​

The order of the commands in the dataLayer decides the result.

  • Consent Mode: the consent default must come before the first Google command. That is js, config or event from gtag, or the gtm.js event. If it does, the result is PASS.
  • TCF: if Google commands are already in the dataLayer and the __tcfapi stub is missing, the result is LATE. If there are no Google commands yet, the result is PASS.

A default set by a Google Tag Manager tag is not in the dataLayer. Avacy reads Google's own consent state (google_tag_data.ics) for this case. If a default was set and no Google tag had read the consent before it, the warning is not printed.

If the only Google command is gtm.js and no default is visible, Avacy waits before it warns. It waits for the page load event, 3 seconds at most. If the page is already loaded, it waits half a second. Then it checks once more.

A custom dataLayer name is not read by the check.

What to do if the result is LATE​

  1. Check whether the late tag uses Google Tag Gateway (GTG). See How to verify if a tag is enrolled in GTG.
  2. If it does, follow the Google Tag Gateway and Consent Management page.
  3. If it does not, load the Avacy snippet before the Google tags.

Google guide: Set up consent mode on websites.

GTM and TCF​

With Google Tag Manager and TCF, you cannot load the Avacy snippet before GTM. The banner starts later. What must come first is the TCF stub. It is a small script that installs window.__tcfapi and queues the calls until the banner starts.

If GTM starts without the stub, the result is LATE. The console warning starts like this:

[Avacy] Google Tag Manager ran before the TCF stub: Google may have read the wrong consent state. …

This happens when all of these are true:

  • TCF is in the configuration.
  • Google commands are already in the dataLayer. Either the Avacy GTM template is active, or the first command is the gtm.js event.
  • There is no __tcfapi function.

To fix it, load the stub as a synchronous script in the <head>. Put it before the GTM snippet and before any script that calls __tcfapi.

<head>
<!-- 1. TCF stub: synchronous, before GTM. -->
<script src="https://<tenant>.avacy-cdn.com/v3/tcf-stub.js"></script>
<!-- 2. GTM snippet. -->
<!-- ... -->
<!-- 3. Avacy banner. -->
</head>

<tenant> is a placeholder: use the same domain as the banner snippet. This address always serves the latest version of the stub. If the stub is already on the page, the banner does not overwrite it.