0

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.

  • GTM does not replace GA (the data is not collected by GTM, in particular), but I believe gtag() does replace ga(). That's not the most current way to use GTM, though - is it possible for you to change it? The current method would allow you to set up tracking without writing new functions in your JS files/script tags. – Reve Nov 20 '18 at 14:13
  • We use GA but not GTM - we don't actually have a GTM account as far as I can see. All I really want to do is be able to add code to my <a> tags that's equivalent of ga('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. – Andy Nov 20 '18 at 15:03
  • So - news to me, gtag has nothing to do with GTM. developers.google.com/analytics/devguides/collection/gtagjs/… says you'll want 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/… – Reve Nov 20 '18 at 17:38
  • Found the anwer? – Volatil3 Nov 21 '18 at 14:58
  • @Volatil3 No. 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. – Andy Nov 21 '18 at 15:18
1

ga('send', 'event', 'training', 'contact', 'Course name'); is giving a reference error re ga is not defined (due to not using universal -analytics.js)

For global site tag (gtag.js) the following should work for an event

onclick="gtag('event', 'contact', {'event_category': 'training', 'event_label': 'Course name'});"

https://developers.google.com/analytics/devguides/collection/gtagjs/events

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.