ga ou gaq.tentar localizar eventos do Google Analytics?

Eu gostaria de rastrear um {[[2]} de um botão em uma página em um site, depois que uma condição é passada, verificando se um cookie está presente.

Muito simples, mas qual sintaxe funcionaria melhor?

eu pesquisei o ga e gaq_push prefixo da sintaxe de rastreamento de eventos GA e (perdoe-me se estou errado) mas eles parecem bastante semelhantes?

_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>
Author: eyal-lezmy, 2013-09-09

5 answers

Se utilizar ga.js (código assíncrono" tradicional") tem de usar o _gaq.empurrar. Se utilizar Análise.js {[5] } tens de usar o ga send. Os métodos não são intercambiáveis, eles pertencem a duas versões diferentes do código de rastreamento do Google Analytics.

Até agora (2017) há uma nova versão de código (gtag.js), por isso, se estiver a usar isso, não use nem ga nem _gaq.push mas, em vez disso, siga as orientações de migração para actualizar o seu código até à última versão (ou você começa a usar o Google Tag Manager de forma sensata).

 78
Author: Eike Pierstorff, 2017-11-20 12:31:58

Se tivesses ambas as análises.js e ga.js executando em seu site, que é recomendado enquanto análise.js ainda está em beta, você pode executar ambos, embora eu iria combiná-los na função notregisterbtn, assim:

    <script type="text/javascript">
    jQuery(document).ready(function () {
        if (jQuery.cookie('entry_winagrand_cookie') !== null) {
            jQuery('notregisterbtn').on('click', function () {
                //you should first check if ga is set
                if (typeof ga !== 'undefined') {
                    ga('send', 'event', 'QR_Win_A_Grand', 'Clicked_through_to_register');
                 }
                //check if _gaq is set too
                if (typeof _gaq !== 'undefined') {
                    _gaq.push(['_trackEvent', 'QR_Win_A_Grand', 'Clicked through to Register']);
                }
             });
        }
    });
    </script>
 17
Author: Blexy, 2014-03-21 14:04:12

Eu criaria uma função se você precisa rastrear eventos diferentes, dessa forma o seu código será mais limpo.

Análise.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');
 15
Author: jLuna, 2014-07-10 19:20:26
Este é o meu guião que tenho na página do Google Analytics.
 <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-77777777-2', 'auto');
    ga('send', 'pageview');
</script>
Para seguir as minhas outras páginas na coluna vertebral.js, coloquei este código em cada coluna vertebral.programa de visualização do js:
ga('send', 'pageview', myUrl);
 0
Author: Artem Zaika, 2015-08-27 18:28:03
/* universal event tracking */
function trackEventTag(category, action, opt_label) {
    /* analytics.js send event */
    ga('send', 'event', { 'eventCategory': category, 'eventAction': action, 'eventLabel': opt_label });
    /* add delay or additional tracking here */
    return true;
}
/* send ga.js _gaq.push() events to universal tracker */
var _gaq = window._gaq || {
    push: function (ar) {
        if (ar && ar.constructor === Array && 0 in ar) {
            if (ar[0] == '_trackEvent') {
                var category = 1 in ar ? ar[1] : null, action = 2 in ar ? ar[2] : null, opt_label = 3 in ar ? ar[3] : null;
                return trackEventTag(category, action, opt_label);
            }
            /* test for others you want to translate here */
        }
        return true;
    }
};
 0
Author: AnOldMan, 2016-03-30 16:13:58