Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom event tracking with Google Analytics

Google Analytics custom event not working, as it is not tracked on Google Analytics statistic View. I written code for onclick, but it is not tracked. Here is my code:

    <script>
 var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-42651041-1']);
_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-42651041-1', '1800accountant.com');
   ga('send', 'pageview', '/Step1');
   </script>

   <script type="text/javascript">
   function test(){


_gaq.push(['_trackEvent','Popup','Click','Step1']);
alert("GA Code executed");
    }
    </script>

    <p onClick="return test()" style="cursor:pointer;">Click Here</p> 
like image 826
user2962526 Avatar asked Dec 03 '13 20:12

user2962526


People also ask

How do I track custom events?

Tracking a Custom Event: Call to Action Clicks. To track clicks on a call-to-action button using GA, we will need three things: A website with some call-to-action (CTA) buttons. A functional Google Analytics (GA) setup sending proper analytical data.


1 Answers

This is because you are using the old syntax gaq.push, which is for the Classic Analytics. You have actually implemented the newer version of GA called Universal Analytics, which uses a different syntax for sending events.

The correct code to use, according to the Google Developer information, it should be written like this:

ga('send', 'event', 'category', 'action', 'label', value);

So, using your example, I'd write it something like this:

ga('send', 'event', 'Popup', 'Click','Step1');

Event, category and action are mandatory, while label and value are optional.

like image 147
MrSponge Avatar answered Sep 21 '22 05:09

MrSponge