yqb
Version:
Static Package Manager
150 lines (124 loc) • 4.75 kB
JavaScript
/*
* 数据采集脚本
* author: <%= author %>
* version: <%= version %>
* website: http://www.431103.com
*/
/*
使用方法
1.引入js
<script type="text/javascript" src="framework/js/webCollection.js"></script>
2.需要埋点的json数据
TJ.update("eventid",{"activityId":"", "phone":""})
3. body加入channelId属性,区分不同业务channelId
<body data-channelId="H-100001">
*/
(function(root, factory) {
if (typeof exports === 'object') {
factory(exports);
} else if (typeof define === 'function' && define.amd) {
define(['exports'], factory);
} else {
factory(root);
}
}(this, function(exports) {
var version = 'v1.0.0';
var tongjing = function(channelId, eventid, objParams) {
this.appId = '6fYG0kV6gd';
this.channelId = channelId || '3I843W45C4';
this.eventid = eventid;
this.objParams = objParams;
this.onInit = null;
this.onUpadate = null;
var self = this;
// 加载成功,初始化
this.getWebCollection(function() {
self.hashMap = new HashMap();
self.init();
});
};
tongjing.prototype = {
init: function() {
Agent.init(this.appId, this.channelId);
// 非产线环境,自动开启调试模式
this.isProduction();
if ($.isFunction(this.onInit)) {
this.onInit();
}
this.objParams = this.getUrlParms();
this.update(this.eventid, this.objParams);
if ($.isFunction(this.onUpadate)) {
this.onUpadate();
}
},
update: function(eventid, objParams) {
var self = this;
this.objParams = objParams;
this.eventid = eventid;
// channelId 被修改,重新初始化
if (this.channelId != '3I843W45C4') {
Agent.init(this.appId, this.channelId);
}
Agent.setUrl(document.URL);
if ($.isFunction(this.onUpadate)) {}
if ($.isPlainObject(self.objParams)) {
$.each(self.objParams, function(k, v) {
self.hashMap.put(k, v);
});
}
if (self.eventid == '' || self.eventid == null) {
Agent.enterPage(document.URL, self.hashMap);
} else {
Agent.customizeEvent(self.eventid, self.hashMap);
}
},
click: function(eventid) {
Agent.clickEvent(eventid);
},
autobindEvent: function() {
var self = this;
$(document).on('click', '[data-agent]', function(e) {
var $agent = $(e.target);
self.click($agent.data('agent'));
});
},
isProduction: function() {
// 非产线环境,自动开启调试模式
var url = document.URL;
if (url.match(/1qianbao.com/i) != '1qianbao.com') {
Agent.setDebug(true);
} else {
console.log('%c \u6B64\u6D4F\u89C8\u5668\u529F\u80FD\u4E13\u4F9B\u5F00\u53D1\u8005\u4F7F\u7528\u3002\u8BF7\u4E0D\u8981\u5728\u6B64\u7C98\u8D34\u6267\u884C\u4EFB\u4F55\u5185\u5BB9\uFF0C\u8FD9\u53EF\u80FD\u4F1A\u5BFC\u81F4\u60A8\u7684\u8D26\u6237\u53D7\u5230\u653B\u51FB\uFF0C\u7ED9\u60A8\u5E26\u6765\u635F\u5931\20\uFF01', 'color:#fff;');
}
},
getWebCollection: function(onSuccess) {
var url = null;
if (/https/i.test(window.location.protocol)) {
url = "https://ms.1qianbao.com/uba/webCollection.js";
} else {
url = "http://d.1qianbao.com/uba/webCollection.js";
}
$.getScript(url, function() {
onSuccess && onSuccess();
});
},
getUrlParms: function() {
var args = new Object();
var query = location.search.substring(1);
if (query == "") {
query = location.hash.substring(1);
}
var pairs = query.split("&");
for (var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('=');
if (pos == -1) continue;
var argname = pairs[i].substring(0, pos);
var value = pairs[i].substring(pos + 1);
args[argname] = unescape(value);
}
return args;
}
}
exports.TJ = new tongjing($('body').data('channelid'));
return TJ;
}));