使用 google 分析跟踪下载?
Tracking downloads with google analytics?
我一直在尝试设置代码来跟踪网站上的文件下载。我刚刚将代码从原始跟踪代码段更新为异步代码 ga.js,(在本地.php5 文件中跟踪网站上的所有页面)但我不知道要使用什么代码跟踪某个页面上的下载。
我找到了这段代码,但我不知道它是否正确;当我检查 GA 时,它没有显示任何事件。
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
$(document).ready(function(){
$('.dl-tracking').on('click', function (){
_gaq.push(['_trackEvent', 'download']);
});
});
</script>
某些链接是在 PHP 中使用 for-each 循环输出的,所以我尝试将此代码投入使用
$variable .= "<a href='$name/media/Material/$x->path' target='_blank onClick="_gaq.push(['_trackEvent', 'TM', 'Download',]);">$fileName</a>";
但我收到了该行的 T_STRING 错误。我对 PHP 有点陌生,所以我不知道我的错误在哪里。
在 Analytics 中跟踪事件时(无论是 ga.js 还是更新的 analytics.js),事件类别和事件操作是必需的(参见official documentation):
category (required): The name you supply for the group of objects you want to track.
action (required): A string that is uniquely paired with each category, and commonly used to define the type of user interaction for
the web object.
label (optional): An optional string to provide additional dimensions to the event data.
value (optional): An integer that you can use to provide numerical data about the user event.
non-interaction (optional): A boolean that when set to true, indicates that the event hit will not be used in bounce-rate
calculation.
因此你应该有类似的东西:
jQuery(document).ready(function ($) {
$('.dl-tracking').on('click', function () {
// You might want to also add the link text/href here:
_gaq.push(['_trackEvent', 'Download', 'Click']);
});
});
至于您的 PHP 异常,这是因为您的引号和双引号字符(' 和 ")应在以下代码行中进行转义:
$variable .= "<a href='$name/media/Material/$x->path' target='_blank onClick="_gaq.push(['_trackEvent', 'TM', 'Download',]);">$fileName</a>";
应该更正为类似于:
$variable .= '<a href="'.$name.'/media/Material/'.($x->path).'" target="_blank" onClick="_gaq.push([\'_trackEvent\', \'TM\', \'Download\']);">'.$fileName.'</a>';
我一直在尝试设置代码来跟踪网站上的文件下载。我刚刚将代码从原始跟踪代码段更新为异步代码 ga.js,(在本地.php5 文件中跟踪网站上的所有页面)但我不知道要使用什么代码跟踪某个页面上的下载。
我找到了这段代码,但我不知道它是否正确;当我检查 GA 时,它没有显示任何事件。
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
$(document).ready(function(){
$('.dl-tracking').on('click', function (){
_gaq.push(['_trackEvent', 'download']);
});
});
</script>
某些链接是在 PHP 中使用 for-each 循环输出的,所以我尝试将此代码投入使用
$variable .= "<a href='$name/media/Material/$x->path' target='_blank onClick="_gaq.push(['_trackEvent', 'TM', 'Download',]);">$fileName</a>";
但我收到了该行的 T_STRING 错误。我对 PHP 有点陌生,所以我不知道我的错误在哪里。
在 Analytics 中跟踪事件时(无论是 ga.js 还是更新的 analytics.js),事件类别和事件操作是必需的(参见official documentation):
category (required): The name you supply for the group of objects you want to track.
action (required): A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.
label (optional): An optional string to provide additional dimensions to the event data.
value (optional): An integer that you can use to provide numerical data about the user event.
non-interaction (optional): A boolean that when set to true, indicates that the event hit will not be used in bounce-rate calculation.
因此你应该有类似的东西:
jQuery(document).ready(function ($) {
$('.dl-tracking').on('click', function () {
// You might want to also add the link text/href here:
_gaq.push(['_trackEvent', 'Download', 'Click']);
});
});
至于您的 PHP 异常,这是因为您的引号和双引号字符(' 和 ")应在以下代码行中进行转义:
$variable .= "<a href='$name/media/Material/$x->path' target='_blank onClick="_gaq.push(['_trackEvent', 'TM', 'Download',]);">$fileName</a>";
应该更正为类似于:
$variable .= '<a href="'.$name.'/media/Material/'.($x->path).'" target="_blank" onClick="_gaq.push([\'_trackEvent\', \'TM\', \'Download\']);">'.$fileName.'</a>';