我有一个网站需要向两个分析帐户提供数据,我不知道如何实现trackEvent,因为有两个不同的帐号。会去哪一个或两者兼而有之?这是我的代码:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['_trackPageview']);
// Second tracker
_gaq.push(['secondTracker._setAccount','UA-YYYYYYY-2']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['secondTracker._trackPageview']);
function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
</script>
</head> listed here to indicate placement
<script type="text/javascript"> (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);
})();
</script>
</body> listed here to indicate placement
该网站是具有移动子域的主域(这就是为什么存在setDomainName)。
问题: 1)两个GA帐户的trackEvent记录? 2)我是否正确拆分了异步代码并且recordOutboundLink与gaq_push在同一个脚本中?
谢谢, 卡伦
答案 0 :(得分:1)
_trackEvent
来电只会将数据发送到单个GA个人资料。如果要将数据发送到两个配置文件,则需要进行多次调用。recordOutboundLink
放在定义_gaq
的同一个脚本中没有问题。尝试以下内容:
function recordOutboundLink(link, category, action) {
_gaq.push(['_trackEvent', category , action ]);
_gaq.push(['secondTracker._trackEvent', category , action ]);
setTimeout(function(){document.location = link.href}, 100);
return false;
}
<a href="linkToTrack" onclick="return recordOutboundLink(this, 'category', 'action')"