Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ga or _gaq.push for Google Analytics event tracking?

I would like to track an onclick of a button on a page on a site, after a condition is passed checking if a cookie is present.

Very simple but which syntax would work best?

I have researched the ga and gaq_push prefix of the GA event tracking syntax and (forgive me if I'm wrong) but they seem pretty similar?

_gaq.push

<script type="text/javascript"> jQuery(document).ready(function () {     if (jQuery.cookie('entry_winagrand_cookie') !== null) {         jQuery('notregisterbtn').on('click', function () {             _gaq.push(['_trackEvent', 'QR_Win_A_Grand', 'Clicked through to Register']);         });     } }); </script> 

ga

<script type="text/javascript"> jQuery(document).ready(function () {      if (jQuery.cookie('entry_winagrand_cookie') !== null) {          jQuery('notregisterbtn').on('click', function () {              ga('send', 'event', 'button', 'click', 'QR_Win_A_Grand', 'Clicked_through_to_register');          });      } }); </script> 
like image 269
Harry Lincoln Avatar asked Sep 09 '13 11:09

Harry Lincoln


People also ask

Should I use GTAG or GA?

So if you want to install Google Analytics by adding its code directly to the source code of your website, always go with GTAG. In fact, this is the library that is displayed in the interface of Google Analytics. But if you ask me, I would always recommend installing Google Analytics with Google Tag Manager.

What is GA push?

GA Push offers an API to enqueue informations that are sent GA servers, and three submodules to track browser events (using jQuery), form validation errors and log exceptions.


2 Answers

If you use ga.js ("traditional" asynchronous code) you have to use _gaq.push. If you use analytics.js you need to use ga send. The methods are not interchangeable, they belong to two different versions of the Google Analytics tracking code.

By now (2017) there is a new code version (gtag.js), so if you are using that you use neither ga nor _gaq.push but instead follow the migration guidelines to bring your code up to the latest version (or you quite sensibly start to use Google Tag Manager).

like image 63
Eike Pierstorff Avatar answered Oct 09 '22 05:10

Eike Pierstorff


I would create a function if you need to track different events, that way your code will be cleaner.

analytics.js

ga.js

function TrackEventGA(Category, Action, Label, Value) {     "use strict";     if (typeof (_gaq) !== "undefined") {         _gaq.push(['_trackEvent', Category, Action, Label, Value]);     } else if (typeof (ga) !== "undefined") {         ga('send', 'event', Category, Action, Label, Value);     } } TrackEventGA('QR_Win_A_Grand', 'Clicked_through_to_register'); 
like image 30
jLuna Avatar answered Oct 09 '22 05:10

jLuna