0

I'm trying to add Google Tag Manager to the <head> of my document.

Unfortunately, the way I'm doing it with an HTML tag render element, the ampersands (&) are being escaped.

function mymodule_page_attachments(array &$page) {
  $config = \Drupal::config('mymodule.config');
  $gtm_id = $config->get('google_tag_manager_id');
  $gtm_auth = $config->get('gtm_auth');
  $gtm_preview = $config->get('gtm_preview');

  /** @var \Drupal\mymodule\AnalyticsUtility $utility */
  $utility = \Drupal::service('mymodule.utility');

  if (!empty($gtm_id) && !empty($gtm_auth) && !empty($gtm_preview) && $utility->isAdminPage() === FALSE) {


    $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={$gtm_auth}&gtm_preview={$gtm_preview}&gtm_cookies_win=x';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{$gtm_id}');\n";


    $page['#attached']['html_head'][] = [
      ['#tag' => 'script', '#value' => $script],
      'google_tag_manager',
    ];
  }
}

This results in ampersands in the code.

<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'?'&amp;l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl+ '&amp;gtm_auth=testrWktRcmjpViiQKW&amp;gtm_preview=env-6&amp;gtm_cookies_win=x';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-K53TEST');</script>

There are two modules with Google Tag Manager and they both add the code using hook_page_attachments().

The recommendation for adding JavaScript is to use an external file; yet, the Google Tag Manager documentation recommends adding the script in <head>.

Is there a way to do this with Drupal settings and a javascript file in my custom module? Alternatively, is there a way not to convert the ampersands to HTML entities?

  • Can I ask why no Google Tag module? Works great for me. – Kevin Feb 16 '18 at 0:48
  • I was acting under the erroneous assumption that Google Tag module did a lot more than it does. Now that I look at it, I think it's exactly what I need. – oknate Feb 16 '18 at 13:58
  • I was acting under the erroneous assumption that Google Tag module did a lot more than it does, when it says "allows non-technical stakeholders to manage the analytics for their website ". Now that I look at it, I think it's exactly what I need. It looks like I'll need to set up a custom module to set up the dataLayer parameters. There is drupal.org/project/datalayer, I'll take a look at that. – oknate Feb 16 '18 at 14:05
2

The recommendation for adding javascript is to use an external file

That is not always the case and it is more of a guideline than a hard rule.

There is a way to do what you ask, but you shouldn't because the google_tag module does exactly what you to want to do. It also allows you to include the GTM snippet inline or as a file.

google_tag module's settings

  • Thanks, It turns out I hadn't explored the google_tag module in enough before I tried to just add the script myself. The google_tag module looks like it will do what I need. – oknate Feb 16 '18 at 13:59
  • No worries. I learn by doing things myself so I completely understand. If you look at the internals of the google_tag module you can see how they injected the snippet in the head without having the markup converted to html entities. – ec0g Feb 16 '18 at 17:15
  • Yes, they use a generated file. It's pretty nifty. – oknate Feb 16 '18 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.