前端埋点

// JavaScript Document
if (window.$ == undefined) {
	document.writeln('<script type="text/javascript" src="http://www.test.biz/seajs/jquery/jquery/1.11.3/jquery.js"></script>');
}

var _locationurl = window.location.href;
var _submiturl = '';
if(_locationurl.indexOf('testabc') >= 0){
	_submiturl = 'http://user.testabc.com?m=user&a=sendpostmis';
}
else{
	_submiturl = 'http://www.test.biz?m=user&a=sendpostmis';
}
setInterval(function () { //每个a添加埋点
	$('input').click(function () {
		if ($(this).attr('type') == 'button') {
			if (typeof ($(this).attr("bindanchor")) == "undefined") {
				$(this).attr('bindanchor', 1);
			}
			if (parseInt($(this).attr('bindanchor'))) { //判断是否需要添加埋点
			var ahref = window.location.href; //参数连接
			//判断uid是否传参。如果没有传默认值
			var buid = 0;
			var ahtml = $(this).val(); //参数
			$.ajax({ //功能提交
				type: 'get',
				async: true,
				data: {
					element: ahtml,
					modified_element: '',
					pageurl: ahref,
					uid: buid
				}, //参数
				url: _submiturl, //地址
				dataType: "jsonp",
				/*加上datatype*/
			});
			$(this).attr('bindanchor', 0); //动作完成后修改自加参数,避免多次提交
		}
		}
		
	});

	$('a').click(function () {
		if (typeof ($(this).attr("bindanchor")) == "undefined") {
			$(this).attr('bindanchor', 1);
		}
		if (parseInt($(this).attr('bindanchor'))) { //判断是否需要添加埋点
			var formid = $(this).parents('form').attr('id'); //取得对应按钮上的formID
			var inputparent = new Array(); //初始化数组
			if (formid) {
				var inputname = $('#' + formid + ' input'); //取得对应表单下面输入框集合
				$.each(inputname, function (name, object) { //循环所有输入框name不为空的写入数组
					if (object.name != '') {
						inputparent.push(object.name);
					}
				});
			}
			var ahref = ''; //参数连接
			if(typeof ($(this).attr("href")) != "undefined"){
				if (parseInt($(this).attr('href').indexOf('void')) >= 0 || parseInt($(this).attr('href').indexOf('javascript:;')) >= 0) { //判断href中是否包含了不必要的字符串
					ahref = window.location.href;
				} else if ($(this).attr('href') != '') { //取得a标签地址
					ahref = $(this).attr('href');
				} 
			}
			else { //没有参数时取得当前连接
					ahref = window.location.href;
				}
			//判断uid是否传参。如果没有传默认值
			var buid = 0;
			if ("undefined" != typeof _buid) {
				buid = _buid;
			}

			var ahtml = ''; //参数
			var atitle = $(this).attr('title');
			if (atitle != undefined) {
				ahtml = atitle;
			} else {
				var ainnerText = $(this).text(); //文本内容
				ainnerText = ainnerText.replace(/[^u4e00-u9fa5]/gi, ""); //提取中文
				if (ainnerText) {
					ahtml = ainnerText;
				}
			}
			//alert(window.location.href);
			//用户画像功能
			var modifieds = '';
			if (inputparent.length) { //判断输入是否为空
				modifieds = inputparent.join(','); //拼接字符串
			}

			$.ajax({ //功能提交
				type: 'get',
				async: true,
				data: {
					element: ahtml,
					modified_element: modifieds,
					pageurl: ahref,
					uid: buid
				}, //参数
				url: _submiturl, //地址
				dataType: "jsonp",
				/*加上datatype*/
			});
			$(this).attr('bindanchor', 0); //动作完成后修改自加参数,避免多次提交
		}
	});
}, 1000);

  

原文地址:https://www.cnblogs.com/rsapaper/p/9638630.html