@datarangers/sdk-javascript 0.1.4 → 0.1.6

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