如何使用Universal Analytics跟踪Google Analytics锚点击次数 [英] How to track Google Analytics anchor clicks using Universal Analytics
问题描述
我试图根据以下相关问题来实现anchor(index.html#anchor)跟踪:
如何使用Google Analytics(分析)跟踪定位标记
我使用锚点在进入网站,我想跟踪所有的锚点,因为它们将是一个不同的页面(网址)。
所以我会跟踪:
link.html 20个网页浏览
link.html#anchor1 5综合浏览量
link.html#anchor2 8综合浏览量
javascript的复活节蛋功能目前像这样加载:
$(function()// on page load
{
if(window.location.hash.slice(1)!=)
launchEasterEgg(window.location.hash.slice(1));
});
$ b $(window).bind('hashchange',function()//检测到哈希变化
{
var hash = window.location.hash.slice(1);
launchEasterEgg(hash);
返回true;
});
我不确定是否应该启动跟踪代码:
- 检测到散列更改+加载页面
- 或直接添加到以下链接:
< a href =#anchoronclick =eventTrackingFunction()> link< / a>
- 或者只是以某种方式设置:
_gaq.push(['_ setAllowAnchor',true]);
与我在前一版GA代码中所做的一样
另外,我不太明白我应跟踪哪种Google Analytics事件( https://developers.google.com/analytics/devguides/collection/analyticsjs/events )
除了示例问题顶部,我使用的是Universal 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-XXXXXXXX-X','mydomain.com');
ga('send','pageview');
< / script>
我现在跟踪复活节彩蛋推出时的访问者:
function launchEasterEgg(hash)
{
ga 'send','pageview','/#'+ hash); //跟踪哈希
// +我的函数代码
}
用于跟踪不会启动复活节彩蛋的访客:
$(function()// on page load
{
if(window.location.hash.slice(1)!=)
launchEasterEgg(window.location.hash.slice(1));
else
ga( 'send','pageview'); //跟踪没有哈希pageview
});
当然,我之前加载了Universal分析代码(没有发送网页浏览一行) p>
I'm trying to implement anchor (index.html#anchor) tracking according to this related question: How to track anchor tags with Google Analytics
I use anchors to launch javascript easter eggs upon entering the website and I would like to track all the anchors as they would be a different pages (urls).
So I would have tracked:
link.html 20 pageviews
link.html#anchor1 5 pageviews
link.html#anchor2 8 pageviews
The javascript easter eggs functions are currently loaded like this:
$(function () // on page load
{
if(window.location.hash.slice(1) != "")
launchEasterEgg(window.location.hash.slice(1));
});
$(window).bind('hashchange', function () //hash change detected
{
var hash = window.location.hash.slice(1);
launchEasterEgg(hash);
return true;
});
I'm not sure whether I should launch the tracking code on:
- "hash change detected" + "on page load"
- OR add directly to the links like:
<a href="#anchor" onclick="eventTrackingFunction()">link</a>
- OR just somehow set:
_gaq.push(['_setAllowAnchor', true]);
as I did in the previous version of GA code
Also I don't quite understand what type of Google Analytics Event should I track (https://developers.google.com/analytics/devguides/collection/analyticsjs/events)
Apart from the sample question at the top, I'm using Universal Analytics so my tracking code is different:
<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-XXXXXXXX-X', 'mydomain.com');
ga('send', 'pageview');
</script>
I've managed to get it working by myself: I now track visitors when Easter egg is launched:
function launchEasterEgg(hash)
{
ga('send', 'pageview', '/#'+hash); // track hash
// + my function code
}
For tracking visitors that don't launch Easter egg I use:
$(function () // on page load
{
if(window.location.hash.slice(1) != "")
launchEasterEgg(window.location.hash.slice(1));
else
ga('send', 'pageview'); // track no hash pageview
});
Of course I load the Universal analytics code before (without the "send pageview" line).
这篇关于如何使用Universal Analytics跟踪Google Analytics锚点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!