Tag Manager Environments not working.

Created on 21 August 2023, 10 months ago
Updated 23 February 2024, 4 months ago

Problem/Motivation

Having a problem getting analytics data when using Google Tag Manager Environments. It seems like tags might not be populating correctly?

Based on the following, which is provided when you "get snippet" from a Tag Manager environment,

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl+ '&gtm_auth=123456789E01e2abcdefgh&gtm_preview=env-2&gtm_cookies_win=x';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-P12345N');</script>
<!-- End Google Tag Manager -->
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-P12345N&gtm_auth=123456789E01e2abcdefgh&gtm_preview=env-2&gtm_cookies_win=x"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

it seems like the tag manager urls need the gtm_auth and gtm_preview query parameters for both the script tag in the head, as well as the noscript tag in the body.

Steps to reproduce

  • Set up an Environment or two in Tag Manager - you can follow this handy guide.
  • "get snippet" from your environment and note the gtm_preview value and the gtm_auth value.
  • in the google_tag configuration, check "Include an environment"
    • enter the gtm_preview value in "Environment ID"
    • enter the gtm_auth value in "Environement Token"

save, refresh, clear cache, etc. and visit a page that should be tagged for analytics data.

(Partial) Proposed resolution

One thing I did notice was that the query parameter variables in .module line 84 are flipped. They are:

        $query_params['gtm_auth'] = $adv_settings['environment_id'];
        $query_params['gtm_preview'] = $adv_settings['environment_token'];

and they should be:

        $query_params['gtm_auth'] = $adv_settings['environment_token'];
        $query_params['gtm_preview'] = $adv_settings['environment_id'];

While that may well address the noscript tag in the body, it does not address the main script tag in the head - I haven't found where that is dealt with in the module.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024