遗传算法或_gaq。推动谷歌分析事件跟踪?

时间:2021-08-26 14:19:58

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.

我希望在通过条件检查是否存在cookie之后,跟踪站点页面上按钮的onclick。

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?

我研究了ga和gaq_push前缀的ga事件跟踪语法(如果我错了请原谅我),但它们看起来很相似?

_gaq.push

_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>

5 个解决方案

#1


76  

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.

如果你使用。js(“传统”异步代码)必须使用_gaq.push。如果你使用分析。你需要使用ga发送。这些方法不可互换,它们属于谷歌分析跟踪代码的两个不同版本。

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).

到现在(2017年),已经有了一个新的代码版本(gtag.js),所以如果您正在使用它,那么您既不使用ga,也不使用_gaq。按一下,而是按照迁移指导方针将代码带到最新的版本(或者您非常明智地开始使用谷歌标记管理器)。

#2


16  

If you had both analytics.js and ga.js running on your site, which is recommended while analytics.js is still in beta, you can run both, although I would combine them in the notregisterbtn function, like so:

如果你有两个分析。js和ga。在你的站点上运行js,这是在分析时推荐的。js还在beta中,你可以同时运行这两个函数,尽管我将它们合并到notregisterbtn函数中,如下所示:

    <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>

#3


15  

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

如果您需要跟踪不同的事件,我将创建一个函数,这样您的代码就会更清晰。

analytics.js

analytics.js

ga.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');

#4


0  

This is my script I've got on Google Analytics page

这是我在谷歌分析页面上的脚本

 <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>

To track my other pages in Backbone.js, I put this code in each Backbone.js View script:

在主干中跟踪我的其他页面。js,我把这些代码放到每个主干中。js脚本:

ga('send', 'pageview', myUrl);

#5


0  

/* 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;
    }
};

#1


76  

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.

如果你使用。js(“传统”异步代码)必须使用_gaq.push。如果你使用分析。你需要使用ga发送。这些方法不可互换,它们属于谷歌分析跟踪代码的两个不同版本。

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).

到现在(2017年),已经有了一个新的代码版本(gtag.js),所以如果您正在使用它,那么您既不使用ga,也不使用_gaq。按一下,而是按照迁移指导方针将代码带到最新的版本(或者您非常明智地开始使用谷歌标记管理器)。

#2


16  

If you had both analytics.js and ga.js running on your site, which is recommended while analytics.js is still in beta, you can run both, although I would combine them in the notregisterbtn function, like so:

如果你有两个分析。js和ga。在你的站点上运行js,这是在分析时推荐的。js还在beta中,你可以同时运行这两个函数,尽管我将它们合并到notregisterbtn函数中,如下所示:

    <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>

#3


15  

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

如果您需要跟踪不同的事件,我将创建一个函数,这样您的代码就会更清晰。

analytics.js

analytics.js

ga.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');

#4


0  

This is my script I've got on Google Analytics page

这是我在谷歌分析页面上的脚本

 <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>

To track my other pages in Backbone.js, I put this code in each Backbone.js View script:

在主干中跟踪我的其他页面。js,我把这些代码放到每个主干中。js脚本:

ga('send', 'pageview', myUrl);

#5


0  

/* 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;
    }
};