javascript 来自http://exisweb.net/link-tracking-universal-analytics

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 来自http://exisweb.net/link-tracking-universal-analytics相关的知识,希望对你有一定的参考价值。

//Track outbounds
(function trackOutbounds() {
	
	var hitCallbackHandler = function(url,win) {
	    if (win.length > 0) {
		    window.open(url, win);
	    } else {
        	window.location.href = url;
        }
    };
    
	if (document.getElementsByTagName) {
		var el = document.getElementsByTagName('a');
		var getDomain = document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0];
		
		// Look thru each a element
		for (var i=0; i < el.length;i++) {
		
			// Extract it's href attribute
			var href = (typeof(el[i].getAttribute('href')) == 'string' ) ? el[i].getAttribute('href') : '';
			
			// Query the href for the top level domain (xxxxx.com)
			var myDomain = href.match(getDomain);
			
			// If link is outbound and is not to this domain	
			if ((href.match(/^https?\:/i)  && !myDomain) || href.match(/^mailto\:/i)) {
			
				// Add an event to click
				el[i].addEventListener('click', function(e) {
					var url = this.getAttribute('href'), win = (typeof(this.getAttribute('target') == 'string')) ? this.getAttribute('target') : '';
							
					// Log even to Analytics, once done, go to the link
					ga('send', 'event', 'outbound', 'click', url,
						{'hitCallback': hitCallbackHandler(url,win)},
						{'nonInteraction': 1}
					);
					
					e.preventDefault();
				});
			}
		}
	}
})();

以上是关于javascript 来自http://exisweb.net/link-tracking-universal-analytics的主要内容,如果未能解决你的问题,请参考以下文章

将javascript注入dom,来自javascript [重复]

javascript ui选择器来自javascript中的list元素

JavaScript 在javascript中仅接受来自键盘的数字数据

获取来自 WKWebView 的 JavaScript 错误

Python string.replace 等效项(来自 Javascript)

来自 appDelegate 的 javascript 调用:phonegap iOS