@datarangers/sdk-javascript 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,478 +1 @@
1
- 'use strict';
2
-
3
- /******************************************************************************
4
- Copyright (c) Microsoft Corporation.
5
-
6
- Permission to use, copy, modify, and/or distribute this software for any
7
- purpose with or without fee is hereby granted.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
- PERFORMANCE OF THIS SOFTWARE.
16
- ***************************************************************************** */
17
-
18
- var __assign = function() {
19
- __assign = Object.assign || function __assign(t) {
20
- for (var s, i = 1, n = arguments.length; i < n; i++) {
21
- s = arguments[i];
22
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
-
29
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
30
- var e = new Error(message);
31
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
32
- };
33
-
34
- // Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
35
- // @ts-nocheck
36
- // @ts-nocheck
37
- var isObject = function isObject(obj) {
38
- return obj != null && Object.prototype.toString.call(obj) == '[object Object]';
39
- };
40
- (function () {
41
- var lastEventId = +Date.now() + Number(("" + Math.random()).slice(2, 8));
42
- return function () {
43
- lastEventId += 1;
44
- return lastEventId;
45
- };
46
- })();
47
- var isSupVisChange = function isSupVisChange() {
48
- var flag = 0;
49
- ['hidden', 'msHidden', 'webkitHidden'].forEach(function (hidden) {
50
- if (document[hidden] !== undefined) {
51
- flag = 1;
52
- }
53
- });
54
- return flag;
55
- };
56
-
57
- // Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
58
- var Alive = /** @class */function () {
59
- function Alive(collect, config) {
60
- this.maxDuration = 12 * 60 * 60 * 1000;
61
- this.aliveDTime = 60 * 1000;
62
- this.options = {
63
- aliveName: 'predefine_page_alive',
64
- params: {}
65
- };
66
- this.collect = collect;
67
- this.config = config;
68
- this.pageStartTime = Date.now();
69
- this.sessionStartTime = this.pageStartTime;
70
- this.timerHandler = null;
71
- if (isObject(config.enable_stay_duration)) {
72
- this.options = Object.assign(this.options, config.enable_stay_duration);
73
- }
74
- }
75
- Alive.prototype.setParams = function (url_path, title, url) {
76
- this.set_path = url_path;
77
- this.set_url = url;
78
- this.set_title = title;
79
- };
80
- Alive.prototype.resetParams = function (url_path, title, url) {
81
- this.url_path = url_path;
82
- this.url = url;
83
- this.title = title;
84
- };
85
- Alive.prototype.enable = function (url_path, title, url) {
86
- this.url_path = url_path || this.url_path;
87
- this.url = url || this.url;
88
- this.title = title || this.title;
89
- this.disableCallback = this.enablePageAlive();
90
- if (this.options.params instanceof Function) {
91
- this.customParmas = this.options.params();
92
- } else {
93
- this.customParmas = this.options.params;
94
- }
95
- };
96
- Alive.prototype.disable = function () {
97
- this.disableCallback();
98
- this.pageStartTime = Date.now();
99
- };
100
- Alive.prototype.sendEvent = function (leave, limited) {
101
- if (limited === void 0) {
102
- limited = false;
103
- }
104
- var duration = limited ? this.aliveDTime : Date.now() - this.sessionStartTime;
105
- if (duration < 0 || duration > this.aliveDTime || Date.now() - this.pageStartTime > this.maxDuration) {
106
- return;
107
- }
108
- this.collect.beconEvent(this.options.aliveName, __assign({
109
- url_path: this.getParams('url_path'),
110
- title: this.getParams('title'),
111
- url: this.getParams('url'),
112
- duration: duration,
113
- is_support_visibility_change: isSupVisChange(),
114
- startTime: this.sessionStartTime,
115
- hidden: document.visibilityState,
116
- leave: leave
117
- }, this.customParmas));
118
- this.sessionStartTime = Date.now();
119
- this.resetParams(location.pathname, document.title, location.href);
120
- };
121
- Alive.prototype.getParams = function (type) {
122
- switch (type) {
123
- case 'url_path':
124
- return this.set_path || this.url_path || location.pathname;
125
- case 'title':
126
- return this.set_title || this.title || document.title || location.pathname;
127
- case 'url':
128
- return this.set_url || this.url || location.href;
129
- }
130
- };
131
- Alive.prototype.setUpTimer = function () {
132
- var _this = this;
133
- if (this.timerHandler) clearInterval(this.timerHandler);
134
- return setInterval(function () {
135
- if (Date.now() - _this.sessionStartTime > _this.aliveDTime) {
136
- _this.sendEvent(false, true);
137
- }
138
- }, 1000);
139
- };
140
- Alive.prototype.visibilitychange = function () {
141
- if (document.visibilityState === 'hidden') {
142
- if (this.timerHandler) {
143
- clearInterval(this.timerHandler);
144
- this.sendEvent(false);
145
- }
146
- } else if (document.visibilityState === 'visible') {
147
- // 重置时间
148
- this.sessionStartTime = Date.now();
149
- this.timerHandler = this.setUpTimer();
150
- }
151
- };
152
- Alive.prototype.beforeunload = function () {
153
- if (!document.hidden) {
154
- this.sendEvent(true);
155
- }
156
- };
157
- Alive.prototype.enablePageAlive = function () {
158
- var _this = this;
159
- this.timerHandler = this.setUpTimer();
160
- var change = this.visibilitychange.bind(this);
161
- var before = this.beforeunload.bind(this);
162
- document.addEventListener('visibilitychange', change);
163
- window.addEventListener('pagehide', before);
164
- return function () {
165
- _this.beforeunload();
166
- document.removeEventListener('visibilitychange', change);
167
- window.removeEventListener('beforeunload', before);
168
- window.removeEventListener('pagehide', before);
169
- };
170
- };
171
- return Alive;
172
- }();
173
-
174
- // Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
175
- var Close = /** @class */function () {
176
- function Close(collect, config) {
177
- var _this = this;
178
- this.maxDuration = 12 * 60 * 60 * 1000;
179
- this.aliveDTime = 60 * 1000;
180
- this.options = {
181
- closeName: 'predefine_page_close',
182
- params: {}
183
- };
184
- this.visibilitychange = function () {
185
- if (document.visibilityState === 'hidden') {
186
- _this.activeEndTime = Date.now();
187
- } else if (document.visibilityState === 'visible') {
188
- if (_this.activeEndTime) {
189
- _this.totalTime += _this.activeEndTime - _this.activeStartTime;
190
- _this.activeTimes += 1;
191
- }
192
- _this.activeEndTime = undefined;
193
- _this.activeStartTime = Date.now();
194
- }
195
- };
196
- this.beforeunload = function () {
197
- _this.totalTime += (_this.activeEndTime || Date.now()) - _this.activeStartTime;
198
- if (_this.config.autotrack) {
199
- var durationKey = '_tea_cache_duration';
200
- try {
201
- var session = window.sessionStorage;
202
- session.setItem(durationKey, JSON.stringify({
203
- duration: _this.totalTime,
204
- page_title: document.title || location.pathname
205
- }));
206
- } catch (e) {}
207
- }
208
- _this.sendEventPageClose();
209
- };
210
- this.collect = collect;
211
- this.config = config;
212
- this.maxDuration = config.maxDuration || 24 * 60 * 60 * 1000;
213
- this.pageStartTime = Date.now();
214
- if (isObject(config.enable_stay_duration)) {
215
- this.options = Object.assign(this.options, config.enable_stay_duration);
216
- }
217
- this.resetData();
218
- }
219
- Close.prototype.setParams = function (url_path, title, url) {
220
- this.set_path = url_path;
221
- this.set_url = url;
222
- this.set_title = title;
223
- };
224
- Close.prototype.resetParams = function (url_path, title, url) {
225
- this.url_path = url_path;
226
- this.url = url;
227
- this.title = title;
228
- };
229
- Close.prototype.enable = function (url_path, title, url) {
230
- this.url_path = url_path || this.url_path;
231
- this.url = url || this.url;
232
- this.title = title || this.title;
233
- this.disableCallback = this.enablePageClose();
234
- };
235
- Close.prototype.disable = function () {
236
- this.disableCallback();
237
- };
238
- Close.prototype.resetData = function () {
239
- this.activeStartTime = this.activeStartTime === undefined ? this.pageStartTime : Date.now();
240
- this.activeEndTime = undefined;
241
- this.activeTimes = 1;
242
- this.totalTime = 0;
243
- if (this.options.params instanceof Function) {
244
- this.customParmas = this.options.params();
245
- } else {
246
- this.customParmas = this.options.params;
247
- }
248
- this.resetParams(location.pathname, document.title, location.href);
249
- };
250
- Close.prototype.sendEventPageClose = function () {
251
- var total_duration = Date.now() - this.pageStartTime;
252
- if (this.totalTime < 0 || total_duration < 0) return;
253
- // 超过24小时的时长没有统计意义
254
- if (this.totalTime >= this.maxDuration) return;
255
- this.collect.beconEvent(this.options.closeName, __assign({
256
- url_path: this.getParams('url_path'),
257
- title: this.getParams('title'),
258
- url: this.getParams('url'),
259
- active_times: this.activeTimes,
260
- duration: this.totalTime,
261
- total_duration: total_duration,
262
- is_support_visibility_change: isSupVisChange()
263
- }, this.customParmas));
264
- this.pageStartTime = Date.now();
265
- this.resetData();
266
- };
267
- Close.prototype.getParams = function (type) {
268
- switch (type) {
269
- case 'url_path':
270
- return this.set_path || this.url_path || location.pathname;
271
- case 'title':
272
- return this.set_title || this.title || document.title || location.pathname;
273
- case 'url':
274
- return this.set_url || this.url || location.href;
275
- }
276
- };
277
- Close.prototype.enablePageClose = function () {
278
- var _this = this;
279
- var change = this.visibilitychange.bind(this);
280
- var before = this.beforeunload.bind(this);
281
- document.addEventListener('visibilitychange', change);
282
- window.addEventListener('pagehide', before);
283
- return function () {
284
- _this.beforeunload();
285
- document.removeEventListener('visibilitychange', change);
286
- window.removeEventListener('beforeunload', before);
287
- window.removeEventListener('pagehide', before);
288
- };
289
- };
290
- return Close;
291
- }();
292
-
293
- // Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
294
- var Types;
295
- (function (Types) {
296
- Types["Init"] = "init";
297
- Types["Config"] = "config";
298
- Types["Start"] = "start";
299
- Types["Ready"] = "ready";
300
- Types["TokenComplete"] = "token-complete";
301
- Types["TokenStorage"] = "token-storage";
302
- Types["TokenFetch"] = "token-fetch";
303
- Types["TokenError"] = "token-error";
304
- Types["ConfigUuid"] = "config-uuid";
305
- Types["ConfigWebId"] = "config-webid";
306
- Types["ConfigDomain"] = "config-domain";
307
- Types["CustomWebId"] = "custom-webid";
308
- Types["AnonymousId"] = "anonymous-id";
309
- Types["TokenChange"] = "token-change";
310
- Types["TokenReset"] = "token-reset";
311
- Types["ConfigTransform"] = "config-transform";
312
- Types["EnvTransform"] = "env-transform";
313
- Types["SessionReset"] = "session-reset";
314
- Types["SessionResetTime"] = "session-reset-time";
315
- Types["Event"] = "event";
316
- Types["Events"] = "events";
317
- Types["EventNow"] = "event-now";
318
- Types["CleanEvents"] = "clean-events";
319
- Types["BeconEvent"] = "becon-event";
320
- Types["SubmitBefore"] = "submit-before";
321
- Types["SubmitScuess"] = "submit-scuess";
322
- Types["SubmitSuccess"] = "submit-success";
323
- Types["SubmitAfter"] = "submit-after";
324
- Types["SubmitError"] = "submit-error";
325
- Types["SubmitVerifyH"] = "submit-verify-h5";
326
- Types["Stay"] = "stay";
327
- Types["ResetStay"] = "reset-stay";
328
- Types["StayReady"] = "stay-ready";
329
- Types["SetStay"] = "set-stay";
330
- Types["RouteChange"] = "route-change";
331
- Types["RouteReady"] = "route-ready";
332
- Types["Ab"] = "ab";
333
- Types["AbVar"] = "ab-var";
334
- Types["AbAllVars"] = "ab-all-vars";
335
- Types["AbConfig"] = "ab-config";
336
- Types["AbExternalVersion"] = "ab-external-version";
337
- Types["AbVersionChangeOn"] = "ab-version-change-on";
338
- Types["AbVersionChangeOff"] = "ab-version-change-off";
339
- Types["AbOpenLayer"] = "ab-open-layer";
340
- Types["AbCloseLayer"] = "ab-close-layer";
341
- Types["AbReady"] = "ab-ready";
342
- Types["AbComplete"] = "ab-complete";
343
- Types["AbTimeout"] = "ab-timeout";
344
- Types["Profile"] = "profile";
345
- Types["ProfileSet"] = "profile-set";
346
- Types["ProfileSetOnce"] = "profile-set-once";
347
- Types["ProfileUnset"] = "profile-unset";
348
- Types["ProfileIncrement"] = "profile-increment";
349
- Types["ProfileAppend"] = "profile-append";
350
- Types["ProfileClear"] = "profile-clear";
351
- Types["TrackDuration"] = "track-duration";
352
- Types["TrackDurationStart"] = "track-duration-start";
353
- Types["TrackDurationEnd"] = "track-duration-end";
354
- Types["TrackDurationPause"] = "track-duration-pause";
355
- Types["TrackDurationResume"] = "tracl-duration-resume";
356
- Types["Autotrack"] = "autotrack";
357
- Types["AutotrackReady"] = "autotrack-ready";
358
- Types["CustomHeader"] = "custom-request-header";
359
- Types["TracerReady"] = "tracer-ready";
360
- })(Types || (Types = {}));
361
- var DebuggerMesssge;
362
- (function (DebuggerMesssge) {
363
- DebuggerMesssge["DEBUGGER_MESSAGE"] = "debugger-message";
364
- DebuggerMesssge["DEBUGGER_MESSAGE_SDK"] = "debugger-message-sdk";
365
- DebuggerMesssge["DEBUGGER_MESSAGE_FETCH"] = "debugger-message-fetch";
366
- DebuggerMesssge["DEBUGGER_MESSAGE_FETCH_RESULT"] = "debugger-message-fetch-result";
367
- DebuggerMesssge["DEBUGGER_MESSAGE_EVENT"] = "debugger-message-event";
368
- DebuggerMesssge["DEVTOOL_WEB_READY"] = "devtool-web-ready";
369
- })(DebuggerMesssge || (DebuggerMesssge = {}));
370
-
371
- // Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
372
- var Stay = /** @class */function () {
373
- function Stay() {}
374
- Stay.prototype.apply = function (collect, config) {
375
- var _this = this;
376
- this.collect = collect;
377
- this.config = config;
378
- if (!this.config.enable_stay_duration) return;
379
- this.title = document.title || location.pathname;
380
- this.url = location.href;
381
- this.url_path = location.pathname;
382
- this.pageAlive = new Alive(collect, config);
383
- this.pageClose = new Close(collect, config);
384
- var Types = this.collect.Types;
385
- this.collect.on(Types.ResetStay, function (_a) {
386
- var url_path = _a.url_path,
387
- title = _a.title,
388
- url = _a.url;
389
- _this.resetStayDuration(url_path, title, url);
390
- });
391
- this.collect.on(Types.RouteChange, function (info) {
392
- if (info.init) return;
393
- if (config.disable_route_report) return;
394
- _this.resetStayDuration();
395
- });
396
- this.collect.on(Types.SetStay, function (_a) {
397
- var url_path = _a.url_path,
398
- title = _a.title,
399
- url = _a.url;
400
- _this.setStayParmas(url_path, title, url);
401
- });
402
- this.enable(this.url_path, this.title, this.url);
403
- this.ready(Types.Stay);
404
- this.collect.emit(Types.StayReady);
405
- };
406
- Stay.prototype.ready = function (name) {
407
- var _this = this;
408
- this.collect.set(name);
409
- if (this.collect.hook._hooksCache.hasOwnProperty(name)) {
410
- var emits = this.collect.hook._hooksCache[name];
411
- if (!Object.keys(emits).length) return;
412
- var _loop_1 = function _loop_1(key) {
413
- if (emits[key].length) {
414
- emits[key].forEach(function (item) {
415
- _this.collect.hook.emit(key, item);
416
- });
417
- }
418
- };
419
- for (var key in emits) {
420
- _loop_1(key);
421
- }
422
- }
423
- };
424
- Stay.prototype.enable = function (url_path, title, url) {
425
- this.pageAlive.enable(url_path, title, url);
426
- this.pageClose.enable(url_path, title, url);
427
- };
428
- Stay.prototype.disable = function () {
429
- this.pageAlive.disable();
430
- this.pageClose.disable();
431
- };
432
- Stay.prototype.setStayParmas = function (url_path, title, url) {
433
- if (url_path === void 0) {
434
- url_path = '';
435
- }
436
- if (title === void 0) {
437
- title = '';
438
- }
439
- if (url === void 0) {
440
- url = '';
441
- }
442
- // 专门用来设置stay的参数
443
- this.pageAlive.setParams(url_path, title, url);
444
- this.pageClose.setParams(url_path, title, url);
445
- this.collect.emit(DebuggerMesssge.DEBUGGER_MESSAGE, {
446
- type: DebuggerMesssge.DEBUGGER_MESSAGE_SDK,
447
- info: 'SDK 执行 resetStayParams',
448
- level: 'info',
449
- time: Date.now(),
450
- data: {
451
- url_path: url_path,
452
- title: title,
453
- url: url
454
- }
455
- });
456
- };
457
- Stay.prototype.reset = function (url_path, title, url) {
458
- this.disable();
459
- this.enable(url_path, title, url);
460
- };
461
- Stay.prototype.resetStayDuration = function (url_path, title, url) {
462
- this.reset(url_path, title, url);
463
- this.collect.emit(DebuggerMesssge.DEBUGGER_MESSAGE, {
464
- type: DebuggerMesssge.DEBUGGER_MESSAGE_SDK,
465
- info: 'SDK 执行 resetStayDuration',
466
- level: 'info',
467
- time: Date.now(),
468
- data: {
469
- url_path: url_path,
470
- title: title,
471
- url: url
472
- }
473
- });
474
- };
475
- return Stay;
476
- }();
477
-
478
- module.exports = Stay;
1
+ "use strict";function t(t,i,e){return(i=function(t){var i=function(t,i){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var s=e.call(t,i||"default");if("object"!=typeof s)return s;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===i?String:Number)(t)}(t,"string");return"symbol"==typeof i?i:i+""}(i))in t?Object.defineProperty(t,i,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[i]=e,t}var i=function(t){return null!=t&&"[object Object]"==Object.prototype.toString.call(t)};Date.now(),Number((""+Math.random()).slice(2,8));var e=function(){var t=0;return["hidden","msHidden","webkitHidden"].forEach(function(i){void 0!==document[i]&&(t=1)}),t},s=function(){function s(e,s){t(this,"collect",void 0),t(this,"config",void 0),t(this,"pageStartTime",void 0),t(this,"maxDuration",432e5),t(this,"sessionStartTime",void 0),t(this,"timerHandler",void 0),t(this,"url_path",void 0),t(this,"url",void 0),t(this,"title",void 0),t(this,"set_path",void 0),t(this,"set_url",void 0),t(this,"set_title",void 0),t(this,"aliveDTime",6e4),t(this,"aliveName",void 0),t(this,"disableCallback",void 0),t(this,"customParmas",void 0),t(this,"options",{aliveName:"predefine_page_alive",params:{}}),this.collect=e,this.config=s,this.pageStartTime=Date.now(),this.sessionStartTime=this.pageStartTime,this.timerHandler=null,i(s.enable_stay_duration)&&(this.options=Object.assign(this.options,s.enable_stay_duration))}var a=s.prototype;return a.setParams=function(t,i,e){this.set_path=t,this.set_url=e,this.set_title=i},a.resetParams=function(t,i,e){this.url_path=t,this.url=e,this.title=i},a.enable=function(t,i,e){this.url_path=t||this.url_path,this.url=e||this.url,this.title=i||this.title,this.disableCallback=this.enablePageAlive(),this.options.params instanceof Function?this.customParmas=this.options.params():this.customParmas=this.options.params},a.disable=function(){this.disableCallback(),this.pageStartTime=Date.now()},a.sendEvent=function(t,i){void 0===i&&(i=!1);var s=i?this.aliveDTime:Date.now()-this.sessionStartTime;s<0||s>this.aliveDTime||Date.now()-this.pageStartTime>this.maxDuration||(this.collect.beconEvent(this.options.aliveName,Object.assign({url_path:this.getParams("url_path"),title:this.getParams("title"),url:this.getParams("url"),duration:s,is_support_visibility_change:e(),startTime:this.sessionStartTime,hidden:document.visibilityState,leave:t},this.customParmas)),this.sessionStartTime=Date.now(),this.resetParams(location.pathname,document.title,location.href))},a.getParams=function(t){switch(t){case"url_path":return this.set_path||this.url_path||location.pathname;case"title":return this.set_title||this.title||document.title||location.pathname;case"url":return this.set_url||this.url||location.href}},a.setUpTimer=function(){var t=this;return this.timerHandler&&clearInterval(this.timerHandler),setInterval(function(){Date.now()-t.sessionStartTime>t.aliveDTime&&t.sendEvent(!1,!0)},1e3)},a.visibilitychange=function(){"hidden"===document.visibilityState?this.timerHandler&&(clearInterval(this.timerHandler),this.sendEvent(!1)):"visible"===document.visibilityState&&(this.sessionStartTime=Date.now(),this.timerHandler=this.setUpTimer())},a.beforeunload=function(){document.hidden||this.sendEvent(!0)},a.enablePageAlive=function(){var t=this;this.timerHandler=this.setUpTimer();var i=this.visibilitychange.bind(this),e=this.beforeunload.bind(this);return document.addEventListener("visibilitychange",i),window.addEventListener("pagehide",e),function(){t.beforeunload(),document.removeEventListener("visibilitychange",i),window.removeEventListener("beforeunload",e),window.removeEventListener("pagehide",e)}},s}(),a=function(){function s(e,s){var a=this;t(this,"collect",void 0),t(this,"config",void 0),t(this,"pageStartTime",void 0),t(this,"maxDuration",432e5),t(this,"sessionStartTime",void 0),t(this,"timerHandler",void 0),t(this,"url_path",void 0),t(this,"url",void 0),t(this,"title",void 0),t(this,"set_path",void 0),t(this,"set_url",void 0),t(this,"set_title",void 0),t(this,"aliveDTime",6e4),t(this,"aliveName",void 0),t(this,"options",{closeName:"predefine_page_close",params:{}}),t(this,"activeStartTime",void 0),t(this,"activeEndTime",void 0),t(this,"activeTimes",void 0),t(this,"totalTime",void 0),t(this,"disableCallback",void 0),t(this,"customParmas",void 0),t(this,"visibilitychange",function(){"hidden"===document.visibilityState?a.activeEndTime=Date.now():"visible"===document.visibilityState&&(a.activeEndTime&&(a.totalTime+=a.activeEndTime-a.activeStartTime,a.activeTimes+=1),a.activeEndTime=void 0,a.activeStartTime=Date.now())}),t(this,"beforeunload",function(){if(a.totalTime+=(a.activeEndTime||Date.now())-a.activeStartTime,a.config.autotrack){try{window.sessionStorage.setItem("_tea_cache_duration",JSON.stringify({duration:a.totalTime,page_title:document.title||location.pathname}))}catch(t){}}a.sendEventPageClose()}),this.collect=e,this.config=s,this.maxDuration=s.maxDuration||864e5,this.pageStartTime=Date.now(),i(s.enable_stay_duration)&&(this.options=Object.assign(this.options,s.enable_stay_duration)),this.resetData()}var a=s.prototype;return a.setParams=function(t,i,e){this.set_path=t,this.set_url=e,this.set_title=i},a.resetParams=function(t,i,e){this.url_path=t,this.url=e,this.title=i},a.enable=function(t,i,e){this.url_path=t||this.url_path,this.url=e||this.url,this.title=i||this.title,this.disableCallback=this.enablePageClose()},a.disable=function(){this.disableCallback()},a.resetData=function(){this.activeStartTime=void 0===this.activeStartTime?this.pageStartTime:Date.now(),this.activeEndTime=void 0,this.activeTimes=1,this.totalTime=0,this.options.params instanceof Function?this.customParmas=this.options.params():this.customParmas=this.options.params,this.resetParams(location.pathname,document.title,location.href)},a.sendEventPageClose=function(){var t=Date.now()-this.pageStartTime;this.totalTime<0||t<0||this.totalTime>=this.maxDuration||(this.collect.beconEvent(this.options.closeName,Object.assign({url_path:this.getParams("url_path"),title:this.getParams("title"),url:this.getParams("url"),active_times:this.activeTimes,duration:this.totalTime,total_duration:t,is_support_visibility_change:e()},this.customParmas)),this.pageStartTime=Date.now(),this.resetData())},a.getParams=function(t){switch(t){case"url_path":return this.set_path||this.url_path||location.pathname;case"title":return this.set_title||this.title||document.title||location.pathname;case"url":return this.set_url||this.url||location.href}},a.enablePageClose=function(){var t=this,i=this.visibilitychange.bind(this),e=this.beforeunload.bind(this);return document.addEventListener("visibilitychange",i),window.addEventListener("pagehide",e),function(){t.beforeunload(),document.removeEventListener("visibilitychange",i),window.removeEventListener("beforeunload",e),window.removeEventListener("pagehide",e)}},s}(),n=function(t){return t.DEBUGGER_MESSAGE="debugger-message",t.DEBUGGER_MESSAGE_SDK="debugger-message-sdk",t.DEBUGGER_MESSAGE_FETCH="debugger-message-fetch",t.DEBUGGER_MESSAGE_FETCH_RESULT="debugger-message-fetch-result",t.DEBUGGER_MESSAGE_EVENT="debugger-message-event",t.DEVTOOL_WEB_READY="devtool-web-ready",t}({}),o=function(){function i(){t(this,"collect",void 0),t(this,"config",void 0),t(this,"title",void 0),t(this,"url",void 0),t(this,"url_path",void 0),t(this,"pageAlive",void 0),t(this,"pageClose",void 0)}var e=i.prototype;return e.apply=function(t,i){var e=this;if(this.collect=t,this.config=i,this.config.enable_stay_duration){this.title=document.title||location.pathname,this.url=location.href,this.url_path=location.pathname,this.pageAlive=new s(t,i),this.pageClose=new a(t,i);var n=this.collect.Types;this.collect.on(n.ResetStay,function(t){var i=t.url_path,s=t.title,a=t.url;e.resetStayDuration(i,s,a)}),this.collect.on(n.RouteChange,function(t){t.init||i.disable_route_report||e.resetStayDuration()}),this.collect.on(n.SetStay,function(t){var i=t.url_path,s=t.title,a=t.url;e.setStayParmas(i,s,a)}),this.enable(this.url_path,this.title,this.url),this.ready(n.Stay),this.collect.emit(n.StayReady)}},e.ready=function(t){var i=this;if(this.collect.set(t),this.collect.hook._hooksCache.hasOwnProperty(t)){var e=this.collect.hook._hooksCache[t];if(!Object.keys(e).length)return;var s=function(t){e[t].length&&e[t].forEach(function(e){i.collect.hook.emit(t,e)})};for(var a in e)s(a)}},e.enable=function(t,i,e){this.pageAlive.enable(t,i,e),this.pageClose.enable(t,i,e)},e.disable=function(){this.pageAlive.disable(),this.pageClose.disable()},e.setStayParmas=function(t,i,e){void 0===t&&(t=""),void 0===i&&(i=""),void 0===e&&(e=""),this.pageAlive.setParams(t,i,e),this.pageClose.setParams(t,i,e),this.collect.emit(n.DEBUGGER_MESSAGE,{type:n.DEBUGGER_MESSAGE_SDK,info:"SDK 执行 resetStayParams",level:"info",time:Date.now(),data:{url_path:t,title:i,url:e}})},e.reset=function(t,i,e){this.disable(),this.enable(t,i,e)},e.resetStayDuration=function(t,i,e){this.reset(t,i,e),this.collect.emit(n.DEBUGGER_MESSAGE,{type:n.DEBUGGER_MESSAGE_SDK,info:"SDK 执行 resetStayDuration",level:"info",time:Date.now(),data:{url_path:t,title:i,url:e}})},i}();module.exports=o;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datarangers/sdk-javascript",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "scripts": {
5
5
  "nclean": "rimraf es lib core",
6
6
  "build": "export NODE_ENV=production && npm run nclean && npx rollup -c rollup.config.js",