0

I'm working on a website that I didn't build, which already has Google Analytics on it, but not the standard analytics.js function. This is what I'm referring to:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('send', 'pageview');

Instead, what I've found on the website is this GTM snippet in the <head> section:

(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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXX');

And this at the end of the <body>:

<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

I suspect the doubled pageviews are due to the analytics.js and GTM snippets co-existing, but when I try to track click events using the onclick method and just the GTM snippet, I get Uncaught ReferenceError: ga is not defined in the browser console. For reference, this is what I'm using: onclick="ga('send', 'event', 'CATEGORY', 'ACTION', 'LABEL')".

I also tried logging when the elements in question are clicked, which worked.

I've installed the Chrome GA debugger extension and when I click on the element, it logs Command ignored. Unknown target: undefined.

I guess my question comes down to: How do I track click events with GA and GTM running at the same time? I've used this GA method before and it has worked, but I've never used GTM snippets and I haven't removed it from this site to avoid breaking any other tracking since I'm not familiar with how it works in relation to GA.

2

I suspect the doubled pageviews are due to the analytics.js and GTM snippets co-existing.

Are double pageviews being tracked? You mentioned initially that the GA Universal tracking code snippet was not installed but instead a GTM container code is installed.

I would assume if that is the case, then the pageview tracking is being handled via a Universal analytics pageview tag, set to fire on all pages, configured within the GTM container.

If you want to track onclick events, using GTM, then you would create a Universal Analytics tag in the GTM container, of the event type Event, with an onclick trigger to fire based on the element ID or element class or something similar.

The event tag would have either the GA Settings variable added (which should be configured to use the GA tracking/property ID) or you would add the tracking id manually to the Universal Analytics event tag.

If for some reason you need to add the onclick code and use analytics.js tracking rather than tags within GTM, then if you add the tracking code and remove the following line

ga('send', 'pageview');  

to disable the tracking code snippet from sending pageviews, to prevent duplicate pageview tracking.

Adding the GA tracking code and setting up further code for onclick tracking seems redundant if a GTM container is already installed and you are able to configure the onclick tracking via GTM and a UA event tag instead.

2
  • Yes, pageviews spiked in GA while I had the analytics.js function on the site. I had thought of removing that pageview line too, but was also wondering if I should be handling event tracking through GTM, as you mentioned, it's just that I've never done that before and I'm not sure where to start. Thank you, that gives me some guidance and I think the solution is to stick with just GTM. – Mario Parra Feb 10 '20 at 17:52
  • @MarioParra rather than reinventing the wheel, if its not something you are familiar with setting up, there is a great article here that goes quite indepth that can help you get up and running with this analyticsmania.com/post/google-tag-manager-click-tracking – Bronwyn V Feb 10 '20 at 20:40

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.