Edit: We are not using Google Tag Manager and this question isn't related to it - I want to know how you do this via code on the website and Google Analytics itself - i.e. without using Google Tag Manager at all.
It's been a while since I used Google Analytics but am having a difficult time finding information which will help.
I have a website which includes the following tracking code on all pages of the website:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-***********"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '***********');
</script>
The *** represent the UA-
account number.
When I login to the Google Analytics website (https://analytics.google.com/analytics/) data is being collected without any problem.
I want to add Events so that I can track some of the links. I added the following to one such <a>
tag:
onclick="ga('send', 'event', 'training', 'contact', 'Course name');"
I clicked on the link a few times and expected that when I went to Behavior > Events > Overview in Analytics it would show an event had taken place. No data is appearing.
I've read the following resource, https://www.analyticsmania.com/post/google-tag-manager-vs-google-analytics/, to try and understand the difference between Google Analytics and Google Tag Manager; I noticed that the script on my website is being loaded from www.googletagmanager.com
.
It says at the end of the above resource:
Google Tag Manager does not replace Google Analytics.
So I assumed I can still do everything I need via the Google Analytics website.
When I try and execute the following in my browser console:
ga('send', 'event', 'training', 'contact', 'Course name');
I get an error saying undefined
.
So I've read https://stackoverflow.com/questions/31274803/uncaught-referenceerror-ga-is-not-defined but this doesn't help.
I'm finding this quite tedious and wondered if someone can help. All I'm trying to do is track some events using Analytics, which I remember years back wasn't so difficult, but everything has changed.
<a>
tags that's equivalent ofga('send', ... )
so that the data is picked up in GA. I'm a developer so I don't feel I have any need for GTM because I can update the tags within the code myself easily.gtag('event', 'contact', {'event_category': 'training', 'event_label': 'course name'})
, if I'm guessing the meaning of the ga() inputs correctly. I have never used GA except through GTM, though. This page of the docs may be helpful too: developers.google.com/analytics/devguides/collection/gtagjs/…gtag()
is giving 'undefined' in the console. No data is appearing under Events in the GA website. We are not using Google Tag Manager and this question isn't related to it - I want to know how you do this via code on the website and Google Analytics itself.