-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.js
600 lines (508 loc) · 15.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
var sdk = (function(root) {
'use strict';
//扩展帮助方法*/
var helper = {};
// 唯一标示 uuid,pageSessionId
helper.uuid = function() {
return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
//遍历
/**
* @method each
* @parame loopable 要遍历的对象
* @parame callback 回调函数
* @parame self 上下文
**/
helper.each = function(loopable, callback, self) {
var additionalArgs = Array.prototype.slice.call(arguments,3);
if(loopable) {
if(loopable.length === +loopable.length) {
for(var i=0; i<loopable.length; i++) {
callback.apply(self, [loopable[i],i].concat(additionalArgs));
}
} else {
for(var item in loopable) {
callback.apply(self, [loopable[item], item].concat(additionalArgs));
}
}
}
};
//扩展
/**
*@method extend
*@parame base 要扩展的对象
*@return base 返回扩展后的对象
**/
helper.extend = function(base) {
helper.each(Array.prototype.slice.call(arguments, 1), function(extensionObject) {
helper.each(extensionObject, function(value, key) {
if(extensionObject.hasOwnPrototype(key)) {
base[key] = value;
}
});
});
return base;
};
//返回数组元素所在的位置,确定是否包含在里面
/**
*@method indexOf
*@parame arrayToSearch 查找的对象
*@parame item 查找的元素
*@return args 返回位置
**/
helper.indexOf = function(arrayToSearch,item){
if(Array.prototype.indexOf){
return arrayToSearch.indexOf(item);
} else {
for(var i=0; i< arrayToSearch.length; i++){
if(arrayToSearch[i] === item) return i;
}
return -1;
}
};
//绑定事件
helper.on = function(target, type, handler) {
if(target.addEventListener) {
target.addEventListener(type, handler, false);
} else {
target.attachEvent("on" + type,
function(event) {
return handler.call(target, event);
}, false);
}
};
//取消事件监听
helper.remove = function(target, type, handler) {
if(target.removeEventListener) {
target.removeEventListener(type, handler);
} else {
target.detachEvent("on" + type,
function(event) {
return handler.call(target, event);
}, true);
}
};
//将json转为字符串
helper.changeJSON2Query = function (jsonObj) {
var args = '';
for (var i in jsonObj) {
if (args != '') {
args += '&';
}
args += i + '=' + encodeURIComponent(jsonObj[i]);
}
return args;
};
//将相对路径解析成文档全路径
helper.normalize = function (url){
var a=document.createElement('a');
a.setAttribute('href',url)
return a.href;
}
//拷贝元素
helper.copyObj = function (copyObj) {
var obj = {};
for ( var i in copyObj) {
obj[i] = copyObj[i];
}
return obj;
}
//初始化采集对象
var collect = {
deviceUrl:'//collect.xxx.com/rest/collect/device/h5/v1',
eventUrl:'//collect.xxx.com/rest/collect/event/h5/v1',
isuploadUrl:'//collect.xxx.com/rest/collect/isupload/app/v1',
parmas:{ ExtraInfo:{} },
device:{}
};
//设置设备信息
collect.setDevice = function () {
if(window && window.screen) {
this.device.ScreenHigh = window.screen.height || 0;
this.device.ScreenWide = window.screen.width || 0;
this.device.DeviceType = 'web';
}
if (navigator) {
this.device.Language = navigator.language || '';
}
}
collect.updatePageInfo = function(){
if(document) {
this.sourceUrl = this.origin || document.referrer || '';
this.currentUrl = this.current || document.URL || '';
this.parmas.PageTitle = document.title || '';
}
}
//设置跟新参数
collect.setParames = function() {
collect.updatePageInfo()
//解析 配置项
if(typeof _XT != "undefined") {
if(_XT.userConfig.dcpChannelCode){
collect.deviceUrl =collect.deviceUrl.split('/?')[0] +'/?dcpChannelCode='+_XT.userConfig.dcpChannelCode
collect.eventUrl = collect.eventUrl.split('/?')[0] +'/?dcpChannelCode='+_XT.userConfig.dcpChannelCode
collect.isuploadUrl = collect.isuploadUrl.split('/?')[0] +'/?dcpChannelCode='+_XT.userConfig.dcpChannelCode
}
for(var i in _XT) {
switch(_XT[i][0]) {
case 'Target':
collect.target = _XT[i].slice(1);
break;
case 'deviceUrl':
if(_XT.userConfig){
collect.deviceUrl = _XT[i][1].split('/?')[0] +'/?dcpChannelCode='+_XT.userConfig.dcpChannelCode
}else {
collect.deviceUrl = _XT[i][1];
}
break;
case 'eventUrl':
if(_XT.userConfig){
collect.eventUrl = _XT[i][1].split('/?')[0] +'/?dcpChannelCode='+_XT.userConfig.dcpChannelCode
}else {
collect.eventUrl = _XT[i][1];
}
break;
case 'isuploadUrl':
if(_XT.userConfig){
collect.eventUrl = _XT[i][1].split('/?')[0] +'/?dcpChannelCode='+_XT.userConfig.dcpChannelCode
}else {
collect.eventUrl = _XT[i][1];
}
break;
default:
break;
}
}
if(_XT.syserror && _XT.syserror.length) {
this.parmas.syserror = _XT.syserror;
_XT.syserror = [];
} else {
delete this.parmas.syserror;
}
} else {
throw "必须定义全局配置变量 _XT,配置指定的请求Url。示例: var _XT = []";
}
};
//获取事件参数
collect.getParames = function() {
return this.parmas;
};
//获取设备信息
collect.getDevice = function() {
return this.device;
};
//请求,保存数据
collect.send = function(obj) {
obj.PageSessionID = sessionStorage.getItem('PageSessionID')
if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
}
else {
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open('POST',collect.eventUrl)
xhr.setRequestHeader('X-Device-Id', collect.deviceId)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.setRequestHeader('X-Source-Url', encodeURI(collect.sourceUrl))
xhr.setRequestHeader('X-Current-Url', encodeURI(collect.currentUrl))
xhr.setRequestHeader('X-User-Id',sessionStorage.getItem('userId')||' ') //混合应用,可能会变
xhr.withCredentials = false;
xhr.send(JSON.stringify(obj))
};
//请求,存储设备信息
collect.sendDevice = function(obj, url) {
if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
}
else {
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open('POST',url)
xhr.setRequestHeader('X-Device-Id', collect.deviceId)
xhr.withCredentials = false
xhr.send(JSON.stringify(obj))
};
//请求,判断是否采集信息
collect.isupload = function(url) {
if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
}
else {
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function() {//服务器返回值的处理函数,此处使用匿名函数进行实现
if(xhr.readyState == 4){
if( xhr.status == 403){
// 考虑存储好 userId ,用户登录好的情况,如果已经进行事件采集,则取消事件监听
collect.openCollect = false
helper.remove(window,'popstate', collect.onPopStateHandler)
helper.remove(window,'beforeunload', collect.beforeUnloadHandler)
collect.onPushStateHandler = function () {
return false
}
}else {
if(!collect.loadEventSend){
collect.loadEventSend = 'collected'
collect.openCollect = true
collect.setDevice();
if(!collect.dcpDeviceType){
collect.setIframe()
}else {
collect.saveEvent(JSON.stringify(collect.beforeload))
setTimeout(function () {
collect.saveEvent(JSON.stringify(collect.loaded))
},1000)
}
collect.event()
}
}
}
};
var obj = {
device:3
}
var url = url +'&'+ helper.changeJSON2Query(obj)+ '&random=' +Math.random();
xhr.open('GET',url)
xhr.setRequestHeader('X-User-Id', sessionStorage.getItem('userId')?sessionStorage.getItem('userId'):' ')
xhr.withCredentials = false;
xhr.send()
};
//点击事件的回调方法
collect.clickHandler =function (e) {
var that = collect
that.parmas && that.parmas.PageElement && delete that.parmas.PageElement
var $target = e.target || e.srcElement;
var currtagName = $target.nodeName.toLowerCase();
if(currtagName == "body" || currtagName == "html" || currtagName == "") {
return 0;
}
if(that.openCollect == true && (that.target && helper.indexOf(that.target, currtagName) > -1 || $target.getAttribute('collect'))) {
that.parmas.PageElement = '{nodeName:'+$target.nodeName +
',title:' + $target.title + ',text:' +$target.innerHTML + '}';
that.parmas.Event = 'click'
that.setParames();
that.parmas.CurrentTime = new Date().getTime()
that.saveEventInfo()
}
}
//beforeunload 事件的回调方法
collect.beforeUnloadHandler = function(){
collect.parmas.Event = 'unload'
if(collect.parmas && collect.parmas.CurrentTime && (new Date().getTime()-collect.parmas.CurrentTime) > 100){
collect.parmas.CurrentTime = new Date().getTime()
collect.setParames();
collect.saveEventInfo()
}
}
//onpopstate 事件回调函数
collect.onPopStateHandler = function () {
if(collect.loadEventSend){
if(collect.parmas && collect.parmas.CurrentTime && (new Date().getTime()-collect.parmas.CurrentTime) > 100){
if(typeof Promise == 'function'){
var runAsync = function(){
var p = new Promise(function(resolve, reject){
collect.parmas.Event = 'unload'
collect.parmas.CurrentTime = new Date().getTime()
collect.current = collect.origin
collect.setParames();
collect.saveEventInfo()
resolve()
});
return p;
}
runAsync().then(function () {
collect.parmas.Event = 'insideload'
sessionStorage.setItem('PageSessionID',helper.uuid())
collect.current = document.URL
collect.setParames();
collect.saveEventInfo()
collect.origin = document.URL
})
}else {
//TODO 全局清理 unload 自定义事件传入的参数,在发其他请求的时候不发
collect.parmas.Event = 'unload'
collect.parmas.CurrentTime = new Date().getTime()
collect.current = collect.origin
collect.setParames();
collect.saveEventInfo()
setTimeout(function () {
collect.parmas.Event = 'insideload'
sessionStorage.setItem('PageSessionID',helper.uuid())
collect.current = document.URL
collect.setParames();
collect.saveEventInfo()
collect.origin = document.URL
},1000)
}
}
}
}
//onpushstate 事件回调
collect.onPushStateHandler = function (_state) {
if(collect.loadEventSend){
if(collect.parmas && collect.parmas.CurrentTime && (new Date().getTime()-collect.parmas.CurrentTime) > 100){
collect.parmas.Event = 'unload'
collect.parmas.CurrentTime = new Date().getTime()
collect.setParames();
collect.saveEventInfo()
setTimeout(function () {
collect.parmas.Event = 'insideload'
sessionStorage.setItem('PageSessionID',helper.uuid())
collect.current = helper.normalize(_state.url)
collect.setParames();
collect.saveEventInfo()
collect.origin = document.URL
},1000)
}
}
}
//系统级事件初始化
collect.event = function() {
var clickEvent = ['click']
for(var j=0;j<clickEvent.length;j++){
helper.on(document.body, clickEvent[j], collect.clickHandler);
}
//监听页面,用户退出离开的时候触发事件
helper.on(window,'beforeunload', collect.beforeUnloadHandler)
//当页面应用,路由切换触发事件
helper.on(window,'popstate', collect.onPopStateHandler)
}
//存储加载完成和开始加载数据信息
collect.getBeforeload=function() {
collect.parmas.Event = 'beforeload'
collect.parmas.CurrentTime = new Date().getTime()
collect.setParames();
collect.beforeload = helper.copyObj(collect.getParames())
}
//存储加载完成,获取设备类型,记录加载完成信息
collect.onload = function() {
if(document.cookie){
var cookieArr = document.cookie.split('; ')
for(var i=0;i<cookieArr.length;i++){
if(cookieArr[i].indexOf('dcp_device_type')>-1){
collect.dcpDeviceType = cookieArr[i].split('=')[1]
}
}
}
collect.isloaded = true
collect.parmas.Event = 'loaded'
collect.parmas.CurrentTime = new Date().getTime()
collect.setParames();
collect.loaded = helper.copyObj(collect.getParames())
collect.origin = document.URL
collect.isupload(collect.isuploadUrl)
}
//web 应用,通过嵌入 iframe 进行跨域 cookie 通讯,设置设备id,
collect.setIframe = function () {
var that = this
var iframe = document.createElement('iframe')
iframe.id = "frame",
iframe.src = '//collectiframe.xxx.com'
iframe.style.display='none'
document.body.appendChild(iframe)
iframe.onload = function () {
iframe.contentWindow.postMessage('loaded','*');
}
//监听message事件
helper.on(window,"message",function(event){
console.log('监听message事n setIframe',event)
that.deviceId = that.deviceId ? that.deviceId : event.data
if(event.data){
that.sendDevice(that.getDevice(), that.deviceUrl);
setTimeout(function () {
that.send(that.beforeload)
that.send(that.loaded)
},1000)
}
})
}
// app 与 h5 混合应用,直接将数信息发给 app
collect.saveEvent = function (jsonString) {
collect.dcpDeviceType && setTimeout(function () {
if(collect.dcpDeviceType=='android'){
android.saveEvent(jsonString)
} else {
window.webkit && window.webkit.messageHandlers ? window.webkit.messageHandlers.nativeBridge.postMessage(jsonString) : window.postBridgeMessage(jsonString)
}
},1000)
}
//采集自定义事件类型
collect.dispatch = function(eType,element,extraInfo){
var that = collect;
element && helper.remove(element,'click',that.clickHandler)
if(that.openCollect){
if(element){
var $target = element
that.parmas.PageElement = '{nodeName:'+$target.nodeName +
',title:' + $target.title + ',text:' +$target.innerHTML + '}';
}
that.parmas.Event = eType
that.parmas.CurrentTime = new Date().getTime()
that.setParames();
that.parmas.ExtraInfo = extraInfo
that.saveEventInfo()
}
}
//将参数 userId 存入sessionStorage
collect.storeUserId = function (_userId) {
sessionStorage.setItem('userId',_userId)
// 调用 isupload
collect.isupload(collect.isuploadUrl)
}
//采集H5信息,如果是混合应用,将采集到的信息发送给 app 端
collect.saveEventInfo = function () {
var that = this
var _obj = that.getParames()
if(collect.dcpDeviceType){
that.saveEvent(JSON.stringify(_obj))
}else {
that.send(_obj)
}
}
collect.init = function() {
sessionStorage.setItem('PageSessionID',helper.uuid())
var that = this;
that.getBeforeload() //获取开始加载的采集信息
that.setParames();
that.setDevice();
(function(history){
var replaceState = history.replaceState;
if(replaceState){
history.replaceState = function(state, param) {
var url = arguments[2];
if (typeof collect.onPushStateHandler == "function") {
collect.onPushStateHandler({state: state, param: param, url: url});
}
return replaceState.apply(history, arguments);
};
}
})(window.history);
(function(history){
var pushState = history.pushState;
if(pushState){
history.pushState = function(state, param) {
var url = arguments[2];
if (typeof collect.onPushStateHandler == "function") {
collect.onPushStateHandler({state: state, param: param, url: url});
}
return pushState.apply(history, arguments);
};
}
})(window.history);
if ((document.readyState=='complete' || document.readyState=='interactive')&&!collect.isloaded) {
collect.onload()
} else {
helper.on(document,'DOMContentLoaded',collect.onload)
}
delete that.parmas.syserror
};
collect.getBeforeload()
collect.init();
return {
dispatch:collect.dispatch,
storeUserId:collect.storeUserId,
}
})(window);