Google Tag Manager with CookieRay Pro

CookieRay Team
•
4 min read

Google Tag Manager with CookieRay Pro

CookieRay Pro can add your Google Tag Manager (GTM) container to every page and tie its loading to the visitor’s consent. You paste a container ID; CookieRay outputs the code in the page head.

This page covers GTM only. For the other two tools see Meta Pixel and Adobe Experience Platform, or the overview of all integrations.

Set it up

  1. Make sure your Pro license is active.
  2. Open CookieRay → Settings → Integrations.
  3. Switch on Google Tag Manager.
  4. Enter your Container ID, for example GTM-XXXXXXX.
  5. Click Save Integrations.

The GTM code is output only when the card is switched on and the Container ID is filled in.

What CookieRay outputs

How GTM loads depends on your consent mode in Settings → Consent Behavior.

Consent modeWhat happens
Strict (Block Until Consent)CookieRay creates the dataLayer straight away, but holds back the gtm.js script until the visitor grants analytical consent.
Log Only (Log Consent Only)The standard GTM snippet loads immediately. Consent is recorded but nothing is blocked.

In Strict mode, a visitor who declines analytical cookies never loads gtm.js at all.

Each time a visitor makes a choice, and again on every later page load for a returning visitor, CookieRay pushes this to the dataLayer:

{
  event: 'cookieray_consent_update',
  analytics_storage: 'granted',   // or 'denied'
  ad_storage: 'denied'            // or 'granted'
}
  • analytics_storage is granted when the visitor allowed the Analytical category.
  • ad_storage is granted when the visitor allowed the Marketing category.

In GTM you can create a Custom Event trigger with the event name cookieray_consent_update, and read analytics_storage and ad_storage through Data Layer Variables. Use it to fire a tag after consent changes.

Because GTM itself does not load in Strict mode until analytical consent, a visitor who has not allowed analytics will not run any tag in the container, whatever its triggers say.

In Strict mode GTM loads after the visitor consents, so triggers that rely on the built-in page view or gtm.js events may not fire on that first page. For tags that need consent, use the Custom Event trigger on cookieray_consent_update, and check your setup in GTM’s preview mode (Tag Assistant).

Consent Mode v2 is a free feature of CookieRay and is separate from this integration. When it is switched on, CookieRay sets the default consent state to denied before the GTM code, so your tags see the denied state first, and updates it after the visitor chooses. See Google Consent Mode v2 and the tutorial Set up Consent Mode v2 with GA4 and GTM.

Avoid loading GTM twice

If your theme, another plugin, or hard-coded code already adds the same container, turn that off or leave this integration switched off. Two copies of the container can load twice and double-count events.

Test it

  1. Open your site in a private window.
  2. In developer tools → Network, filter for googletagmanager.
  3. In Strict mode, confirm there is no gtm.js request before you click the banner.
  4. Click Accept All and confirm gtm.js now loads. In the console, type dataLayer and look for the cookieray_consent_update entry.
  5. Repeat with Decline All. gtm.js should not load in Strict mode.

If the banner is hidden for a visitor’s country by Geo Targeting, or switched off, the GTM code is still printed but no consent push happens. In Strict mode gtm.js never loads for those visitors. In Log Only mode it loads straight away. See Integrations.

Troubleshooting

See Pro troubleshooting if GTM does not appear in the page source or loads twice.

Was this article helpful?

Your response is saved on this device.