Skip to content
Home > Posts > WooCommerce and Google Analytics fix

WooCommerce and Google Analytics fix

By now, you have probably heard of the issues we are seeing with Google Analytics conflicting with WooCommerce sites. Thankfully, there is a solution!

Wait, what’s the issue again?​​

When we add the Google Analytics script to the site, it conflicts with how the cart loads and/or caches. When an item is added to the cart, and you navigate to the Cart page, it will show as empty. However, if you refresh the page or wait a few seconds before navigating to the cart, it will display as normal.

The fix:

​​Most of us have been using the gtag.js script to add Google Analytics to our sites, either because that is what the customer provided, or because that’s just what we’re used to using.

Here’s an example of the gtag.js script:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src=""></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_MEASUREMENT_ID');

The gtag.js script is the culprit behind our cart issues. However, there is an alternative script we can use, that does not produce the same issues – analytics.js.

Here’s an example of the analytics.js script:

<!-- Google Analytics -->
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
<!-- End Google Analytics -->

Using the above example, you would replace ‘UA-XXXXX-Y’ with the customer’s UA ID.

So, what’s the difference between gtag.js and analytics.js?​​

Really, not a whole lot (for our purposes and scope). gtag.js is a new version of Google Analytics’ JavaScript capture API, which is meant to streamline tracking across all Google products. The main difference is that it includes data layers for tag management systems, such as Google Tag Manager. However, on the backend it is simply loading in the older analytics.js library. A situation where gtag.js would be needed is if the customer is using a Tag Management System (TMS) or Google Tag Manager (GTM). However, we will know if they are using Google Tag Manager because they will provide two Google Tag Manager scripts to be placed on the site, which will look like this:

GTM <head> code:

<!-- Google Tag Manager -->
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
<!-- End Google Tag Manager -->

GTM <body> code:

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src=""
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->


Use analytics.js instead of gtag.js.

Solution by Laura Kulas

Scroll To Top