1

I have GTM installed on my site. We already have canonical URLs on all of our pages, but Google Analytics is not tracking the canonical URLs, so the analytics are getting duplicate pages where the capitalization or querystrings are different. I've found answers on how to modify the Google Analytics snippet to track canonical URLs, such as this answer here, but the issue is that because I have GTM installed my code doesn't have the Google Analytics snippet. Instead, we have a GTM snippet:

<!-- 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; f.parentNode.insertBefore(j, f);
    })(window, document, 'script', 'dataLayer', <asp:Literal runat="server" id="gtmTag" />);</script>
<!-- End Google Tag Manager -->

How do I make Google Analytics use the canonical URL when I'm using GTM?

Erica Stockwell-Alpert
  • 4,624
  • 10
  • 63
  • 130

1 Answers1

0

Create a DOM type variable to get the canonical link (choose "CSS Selector", the proper Selector should be along the lines of link[rel='canonical']). Expand the "format value" section, check "Convert 'undefined' to" and enter the page url variable as value (so this will return a value even when a page does not have a canonical). Create an Url Variable, set "component type" to "page path", and in "more settings" use the previously created DOM variable as URL source (all this might be a bit overkill, but should be reliable even with missing canonical tags).

In you Analytics tag or your GA settings variable expand the "More Settings" section, expand "fields to set" (which allows you to set properties of the GA tracker), enter "page" as field name and the variable created above as value. This will set the page path value ("page" in Google Analytics) to the canonical value (or default value) extracted above.

Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62