javascript 尝试使用Google Analytics进行jQuery外部链接跟踪
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 尝试使用Google Analytics进行jQuery外部链接跟踪相关的知识,希望对你有一定的参考价值。
<script>
$(document).ready(function(){
var domain = document.location.host; //includes port if exists, whereas hostname does not
var curPage = document.location.href;
var downloadTypes = /\.(pdf|docx?|xlsx?|pptx?|mp3|wmv|avi|pub|mdb|zip|exe|rar|msi)$/i;
//track outbound links with google analytics
$('a[href^="http"]:not([href*="'+domain+'"])').each(function(event){
//track the events: 1st = method, 2nd = category, 3rd = action, 4th = label, 5th = (int)value
//http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
$(this).click(function(event){
//track an outbound link-click event
//_gaq.push(['_trackEvent', 'Outbound', curPage, $(this).attr('href'),,true]);
//ga('send', 'event', 'category', 'action', 'opt_label', opt_value, {'nonInteraction': 1});
ga('send', 'event', 'Outbound', curPage, $(this).attr('href'),, {'nonInteraction': 1});
if(!event.metaKey && !event.ctrlKey) {
event.preventDefault();
setTimeout('document.location = "' + $(this).attr('href') + '"', 3000);
}
});
});
//track file downloads
$('a[href*="."]').each(function(event){ //best I could figure out... :-/
var curHref = $(this).attr('href');
if(downloadTypes.test(curHref)){
$(this).click(function(event){
//track an outbound link-click event
//_gaq.push(['_trackEvent', 'Download', curPage, curHref,, true]);
//ga('send', 'event', 'category', 'action', 'opt_label', opt_value, {'nonInteraction': 1});
ga('send', 'event', 'Download', curPage, curHref,, {'nonInteraction': 1});
if(!event.metaKey && !event.ctrlKey) {
event.preventDefault();
setTimeout('document.location = "' + curHref + '"', 3000);
}
});
}
});
//track search form submissions too!
$('#searchform').bind('submit', function(event){
event.preventDefault();
//_gaq.push(['_trackEvent', 'Search', curPage, 'Catalog Search',, true]);
//ga('send', 'event', 'category', 'action', 'opt_label', opt_value, {'nonInteraction': 1});
ga('send', 'event', 'Search', curPage, 'Catalog Search',, {'nonInteraction': 1});
//use an anonymous function with the closure to pass an element
(function(el){
setTimeout(function(){
$(el).unbind('submit');
$(el).submit()
}, 3000);
})($(this));
});
});
$(window).load(function(){
//get viewport data
var myWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var myHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
//_gaq.push(['_trackEvent', 'Viewport', 'Size', myWidth+'x'+myHeight,, true]);
//_gaq.push(['_trackEvent', 'Viewport', 'Width', myWidth+'x'+myHeight, myWidth, true]);
//_gaq.push(['_trackEvent', 'Viewport', 'Height', myWidth+'x'+myHeight, myHeight, true]);
//ga('send', 'event', 'category', 'action', 'opt_label', opt_value, {'nonInteraction': 1});
ga('send', 'event', 'Viewport', 'Size', myWidth+'x'+myHeight,, {'nonInteraction': 1});
ga('send', 'event', 'Viewport', 'Width', myWidth+'x'+myHeight, myWidth, {'nonInteraction': 1});
ga('send', 'event', 'Viewport', 'Height', myWidth+'x'+myHeight, myHeight, {'nonInteraction': 1});
});
</script>
以上是关于javascript 尝试使用Google Analytics进行jQuery外部链接跟踪的主要内容,如果未能解决你的问题,请参考以下文章
使用javascript通过google api发送邮件失败
使用 CDN 的 Google PageSpeed Insight CSS Javascript“首屏”问题
通过 Google Chrome 使用 javascript 传递参数