Skip to content
Advertisement

Distinguishable Event tracking in function using Google Analytic

I’m trying to add an event tracking in my code in HTML/JS web page. So each time that a button is clicked a function executed and for each such execution I want a different event tracker.

For example:
If it is the first click then increase the first counter.
if it is the second click, increase the second counter.
No need to worry here the number of such executions in no more then three.

So far the function looks like this (more or less):

    function next_click()
    {
        var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxx-x']);
  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_trackEvent', 'Follow', 'Facebook']);
  _gaq.push(['_trackPageview']);

  (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-xxxxxx-x', '127.0.0.1');
  ga('send', 'pageview');
        if (document.getElementById("step" + (current_step + 1)) == undefined)
        return;
    if (!validate_step(current_step))
        return;
    document.getElementById("step" + current_step).style.display = "none";
    current_step++;
    page_count++;
    }

I’m having trouble configuring the events.

Advertisement

Answer

Found the solution. The problem was that I wasn’t using the following line:

ga('send', 'event', 'Category', 'action','lable', value);

instead of the global tracking line:

ga('send', 'pageview');

So that each time the counter increased/decreased it never was send back to Google. Now after adding this line, the event shows up.

And the way to distinguish between the event is simple by changing the Category field or/and the Action field. For more reference, you can read here: Event Tracking

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement