Events not being tracked in new Google Analytics (

2019-02-08 00:10发布

I have a website that I am using the new Universal Analytics (analytics.js) to track. Everything is setup and working (pageviews, referrals, etc.) using the following code snippet:

<script>
  (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','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-39570713-1', 'site.com');
  ga('send', 'pageview');

</script>

That is located before the </head> tag.

I am using JQuery to fire off an event. I tested the JQuery with an alert message and it is getting called, so that isn't the problem. Here is the snippet that fires when a button is clicked:

$('#submitButton').on('click', function() {
      ga('send', 'event', 'button', 'click', 'contact form');
    });

Nothing is appearing in the Events section of Analytics. I keep clicking the button, even from different computers just to make sure it isn't excluding my IP address. Because the Analytics doc that Google provides does not provide a whole lot of explanation I'm at a loss here.

14条回答
叼着烟拽天下
2楼-- · 2019-02-08 00:30

I got it working - my example is using the new Universal Analytics.

<script type="text/javascript">
    function sliderOnChange() {
    var period = window.convertDays(($("#PeriodSlider").slider("value")));
    var amount_of_credit = $("#AmountOfCreditSlider").slider("value");
    var gaEventInput = "£" + amount_of_credit + " for " + period;
    ga('send', 'event', 'slider', 'sliding', gaEventInput);
}
</script>
  1. Make sure Google Analytics / Google Tag Manager filters are not excluding any traffic from different domain. (Maybe you are testing it to get this working using different domain)
  2. Recheck your GA id and domain in ga('create', 'UA-39570713-1', 'site.com');
  3. Create a new profile in Google Analytics (GA) for testing purposes and debug your html in the same domain you define in GA.
  4. Change the date to be today in GA - you might also need to wait some time before it appears in GA
查看更多
贼婆χ
3楼-- · 2019-02-08 00:30

You can also use a jquery plugin I wrote for the new Universal Analytics: https://github.com/pascalvgemert/jquery-analytics-event-tracking

查看更多
ら.Afraid
4楼-- · 2019-02-08 00:32

I cannot see anything wrong with the code itself. Have you tried using the alternative event tracking?

ga('send', {
  'hitType': 'event',          // Required.
  'eventCategory': 'button',   // Required.
  'eventAction': 'click',      // Required.
  'eventLabel': 'contact form'
});

I would also suggest testing the website with GA Debug Chrome addon, which allows you to see the tracking beacon was sent or not.

"Official" debugging documentation for Universal Analytics is still missing as of now, but hopefully it will be added soon as ga_debug.js provides lot of useful ways how to find out what's wrong with Analytics implementation...

查看更多
Anthone
5楼-- · 2019-02-08 00:32

I have the same problem, and it looks like events are tracked, but GA dashboard doesn't allow to browse them. This is the only way how I could interprete the "Visits with events: 1071" but "Total events: 0" that GA dashboard shows me.

UPD: With GA Chrome debug, have found a problem; 1st method is not working (sends the event without any data attached), but the 2nd one is OK.

查看更多
家丑人穷心不美
6楼-- · 2019-02-08 00:33

You should also consider that it is likely that the page gets reloaded after the submit event was fired before the ga script was able to execute the 'send' method. To avoid this you could employ the 'hitCallback' mechanism, i.e. prevent the submit, call the ga send-method and submit the form data in the callback.

查看更多
不美不萌又怎样
7楼-- · 2019-02-08 00:39

The only way I solved the problem was rolling back to the previous version of Analytics (non-beta):

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-39570713-2']);
  _gaq.push(['_setDomainName', 'optimino.com']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
查看更多
登录 后发表回答