Saturday, March 1, 2025
HomeAnalyticsGoogle Tag Manager And jQuery

Google Tag Manager And jQuery


The jQuery JavaScript library is by almost any means of counting the most popular JavaScript library used in websites around the world. It’s so influential, in fact, that its evolution is tightly bound to the JavaScript standardization effort itself, and it’s an integral part of the JS Foundation’s efforts to build a community for JavaScript developers.

Google Tag Manager, similarly, is the most popular tag management system used in websites, globally. Thus, by way of weak correlation, it makes sense to expect some synergy between the two.

In this article, I want to explore the relationship between the two tools. Both serve a similar purpose – abstraction and facilitation of doing stuff with JavaScript on a web page. I also want to dispel a myth about Google Tag Manager bundling jQuery natively, and I want to explore when it might make sense not to use jQuery.


X


The Simmer Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava into your email inbox!

No, Google Tag Manager does not come bundled with the jQuery library

Some people have peeked into the depths of the gtm.js library downloaded by the browser when the container snippet is executed. This library contains the GTM container in all its glory. Within the minified, obfuscated JavaScript code, sharp eyes can find the following comment:

This comment might tempt you to think that GTM bundles the full jQuery library within the container JavaScript.

It doesn’t.

The comment is there because Google Tag Manager uses one method that is heavily inspired by a similar functionality in the jQuery library. You can find the source of this in the data-layer-helper GitHub project, where the is_plain_object.js contains the explanation for why there is a licence notice like this:

By the way, the data-layer-helper project is a great way to get acquainted with how GTM’s internal data model works!

Check if jQuery is already running

Chances are, your site is already running jQuery. You can test this by opening the JavaScript console in your favorite web browser. Once the console is open, type in the following text and press enter:

typeof window.jQuery !== 'undefined' ? console.log(window.jQuery.fn.jquery) : 'jQuery not found!'

If your site is running jQuery, you’ll see the version number output into the console. If your site is not running jQuery, you’ll see the text: jQuery not found!.

If jQuery was found, you can tentatively use it in your Custom HTML tags and your Custom JavaScript variables.

However, jQuery is (or at least should be) loaded asynchronously, which means there might be a race condition where you try to call its methods using GTM before the jQuery library has actually loaded. Thus, whenever you want to use jQuery, you should hedge it with some safeguards.

Use jQuery safely

Basically, if you want to use jQuery, you should always check if it has been initialized with something like this:

if (typeof window.jQuery !== 'undefined') {
  // Do something with jQuery
} else {
  // Fallback in case jQuery hasn't been loaded
}

If jQuery is found, you can use it at will, and if it isn’t found, the else block is the fallback you’ll use in such cases. Here’s an example:

function() {
  var el = {{Click Element}};
  if (typeof window.jQuery !== 'undefined') {
    return window.jQuery(el).find('h1').text();
  } else {
    return el.querySelector('h1').textContent;
  }
}

The code in the if block and the code in the else block are essentially the same, though jQuery has some benefits, such as making sure that text() returns the text content regardless of browser type and version.

Load jQuery using Google Tag Manager

If jQuery isn’t used by your site, and you still want to leverage it, you can always load it in a Custom HTML tag.

First, discuss this with your developers. jQuery can introduce quite a bit of bloat into the page, and the developers might have had a good reason not to use jQuery at all. It’s possible they use another library that has the same functionality.

The best way to load jQuery is to use a Custom HTML tag in a tag sequence with whatever tag(s) you have that might need to use jQuery.

Let’s start with what the Custom HTML tag looks like:

script>
  (function() {
    var el = document.createElement('script');
    el.src = 'https://code.jquery.com/jquery-3.3.1.min.js';
    el.async = true;
    el.addEventListener('load', function() { window.google_tag_manager[{{Container ID}}].onHtmlSuccess({{HTML ID}})});
    el.addEventListener('error', function() { window.google_tag_manager[{{Container ID}}].onHtmlFailure({{HTML ID}})});
    document.head.appendChild(el);
  })();
script>

You’ll need to enable the Container ID and HTML ID built-in variables for this. Do not add any triggers to this tag.

Basically, you create an asynchronous

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

Skip to toolbar