如何使用Google Analytics进行跟踪点击事件

时间:2014-04-14 22:28:04

标签: javascript jquery google-analytics analytics event-tracking

我正在尝试在用户点击特定元素时记录事件,但没有记录任何事件。这是我的代码:

/* JQUERY PREPENDED ALREADY... */

/* STANDARD GOOGLE ANALYTICS CODE */
(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-12345678-1', 'mysite.com');
ga('send', 'pageview');

var _gaq = _gaq || [];

/* MY EVENT TRACKING CODE (NOT WORKING) */
$(".logo-btn-js").click( function() {
        _gaq.push(['_trackEvent', 'Clicks', 'Logo', 'User clicked the "logo" on first screen']);
    }
);
$(".websites-btn-js").click( function() {
        _gaq.push(['_trackEvent', 'Clicks', 'Websites', 'User clicked the "websites" btn']);
    }
);
...

我做错了什么?

1 个答案:

答案 0 :(得分:17)

您正在使用Universal Analytics (analytics.js)

(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-12345678-1', 'mysite.com');
ga('send', 'pageview');

使用Classic Analytics (ga.js) event syntax(不起作用):

_gaq.push(['_trackEvent', 'Clicks', 'Logo', 'User clicked the "logo" on first screen']);

要修复,请使用Universal Analytics event语法:

$(".logo-btn-js").click( function() {
        ga('send', 'event', 'Clicks', 'Logo', 'First Screen');
});