javascript 使用Google Analytics和BU GA跟踪器跟踪下载情况

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用Google Analytics和BU GA跟踪器跟踪下载情况相关的知识,希望对你有一定的参考价值。

(function ($) {
	var filter_from_url = window.location.protocol + "//" + window.location.host,
        tracked_types_formatted = '',
        trackedTypes = ['pdf'];
 	
	trackedTypes.forEach(function(t){
		tracked_types_formatted = tracked_types_formatted + "a[href$='."+t+"'],";
	});
 
	tracked_types_formatted = tracked_types_formatted.slice(0,-1);
 
 	function trackDownload(link, filetype, filename, target) {
		var args,
			currentPath = location.pathname.split('/');

		try {
			args = {
				category: "file downloaded - /" + currentPath[1],
				action: filetype,
				label: filename
			};
			bu_ga_track_event(args);

			if( "_blank" == target ){
				window.open( link );
			} else {
				setTimeout(function(){
					window.top.location.href = link;
				}, 150);
			}

		} catch (err) {}
		return;
	}
	
	function bu_ga_track_event(args) {
		if (typeof args.value === 'undefined' || typeof args.value === 'null' || isNaN(args.value)) {
			args.value = 0;
		}
	
		if (typeof args.delay === 'undefined' || typeof args.delay === 'null') {
			args.delay = 0;
		}
	
		if (typeof args.noninteract === 'undefined' || typeof args.noninteract === 'null') {
			args.noninteract = true;
		}
	
		args.value = parseFloat(args.value);
		var i, trNames = bu_ga_get_trackers();
	
		_gaq.push(['_trackEvent', args.category, args.action, args.label, args.value, args.noninteract]);
	
		for (i = 0; i < trNames.length; i++) {
			_gaq.push([trNames[i] + '._trackEvent', args.category, args.action, args.label, args.value, args.noninteract]);
		}
	
		if (args.delay) {
			setTimeout(function() {
				return true;
			}, 100);
		}
	}
 
	$(document).on("click", tracked_types_formatted, function (e) {
		var filetype = this.href.split('.').pop(),
		path = this.href.replace(window.location.protocol + "//" + window.location.host,"");
	
	        e.preventDefault();
	
	        trackDownload(this.href, filetype, path, this.target);
	});

}(jQuery));

以上是关于javascript 使用Google Analytics和BU GA跟踪器跟踪下载情况的主要内容,如果未能解决你的问题,请参考以下文章

使用 JavaScript 的 Google 大查询

javascript 使用Google Analytics跟踪JavaScript错误(作为事件)

javascript 使用Google Analytics跟踪JavaScript文件加载时间

javascript 使用Google的Universal Analytics跟踪JavaScript错误。

javascript 使用Google的Universal Analytics跟踪JavaScript错误。

使用 javascript 设置 Google +1 按钮网址