@datarangers/sdk-javascript 0.1.2 → 0.1.3

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 +1,478 @@
1
- "use strict";var t=function(){return t=Object.assign||function(t){for(var e,i=1,a=arguments.length;i<a;i++)for(var s in e=arguments[i])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},t.apply(this,arguments)},e=function(t){return null!=t&&"[object Object]"==Object.prototype.toString.call(t)};Date.now(),Number((""+Math.random()).slice(2,8));var i,a,s=function(){var t=0;return["hidden","msHidden","webkitHidden"].forEach(function(e){void 0!==document[e]&&(t=1)}),t},n=function(){function i(t,i){this.maxDuration=432e5,this.aliveDTime=6e4,this.options={aliveName:"predefine_page_alive",params:{}},this.collect=t,this.config=i,this.pageStartTime=Date.now(),this.sessionStartTime=this.pageStartTime,this.timerHandler=null,e(i.enable_stay_duration)&&(this.options=Object.assign(this.options,i.enable_stay_duration))}return i.prototype.setParams=function(t,e,i){this.set_path=t,this.set_url=i,this.set_title=e},i.prototype.resetParams=function(t,e,i){this.url_path=t,this.url=i,this.title=e},i.prototype.enable=function(t,e,i){this.url_path=t||this.url_path,this.url=i||this.url,this.title=e||this.title,this.disableCallback=this.enablePageAlive(),this.options.params instanceof Function?this.customParmas=this.options.params():this.customParmas=this.options.params},i.prototype.disable=function(){this.disableCallback(),this.pageStartTime=Date.now()},i.prototype.sendEvent=function(e,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,t({url_path:this.getParams("url_path"),title:this.getParams("title"),url:this.getParams("url"),duration:a,is_support_visibility_change:s(),startTime:this.sessionStartTime,hidden:document.visibilityState,leave:e},this.customParmas)),this.sessionStartTime=Date.now(),this.resetParams(location.pathname,document.title,location.href))},i.prototype.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}},i.prototype.setUpTimer=function(){var t=this;return this.timerHandler&&clearInterval(this.timerHandler),setInterval(function(){Date.now()-t.sessionStartTime>t.aliveDTime&&t.sendEvent(!1,!0)},1e3)},i.prototype.visibilitychange=function(){"hidden"===document.visibilityState?this.timerHandler&&(clearInterval(this.timerHandler),this.sendEvent(!1)):"visible"===document.visibilityState&&(this.sessionStartTime=Date.now(),this.timerHandler=this.setUpTimer())},i.prototype.beforeunload=function(){document.hidden||this.sendEvent(!0)},i.prototype.enablePageAlive=function(){var t=this;this.timerHandler=this.setUpTimer();var e=this.visibilitychange.bind(this),i=this.beforeunload.bind(this);return document.addEventListener("visibilitychange",e),window.addEventListener("pagehide",i),function(){t.beforeunload(),document.removeEventListener("visibilitychange",e),window.removeEventListener("beforeunload",i),window.removeEventListener("pagehide",i)}},i}(),o=function(){function i(t,i){var a=this;this.maxDuration=432e5,this.aliveDTime=6e4,this.options={closeName:"predefine_page_close",params:{}},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())},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=t,this.config=i,this.maxDuration=i.maxDuration||864e5,this.pageStartTime=Date.now(),e(i.enable_stay_duration)&&(this.options=Object.assign(this.options,i.enable_stay_duration)),this.resetData()}return i.prototype.setParams=function(t,e,i){this.set_path=t,this.set_url=i,this.set_title=e},i.prototype.resetParams=function(t,e,i){this.url_path=t,this.url=i,this.title=e},i.prototype.enable=function(t,e,i){this.url_path=t||this.url_path,this.url=i||this.url,this.title=e||this.title,this.disableCallback=this.enablePageClose()},i.prototype.disable=function(){this.disableCallback()},i.prototype.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)},i.prototype.sendEventPageClose=function(){var e=Date.now()-this.pageStartTime;this.totalTime<0||e<0||this.totalTime>=this.maxDuration||(this.collect.beconEvent(this.options.closeName,t({url_path:this.getParams("url_path"),title:this.getParams("title"),url:this.getParams("url"),active_times:this.activeTimes,duration:this.totalTime,total_duration:e,is_support_visibility_change:s()},this.customParmas)),this.pageStartTime=Date.now(),this.resetData())},i.prototype.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}},i.prototype.enablePageClose=function(){var t=this,e=this.visibilitychange.bind(this),i=this.beforeunload.bind(this);return document.addEventListener("visibilitychange",e),window.addEventListener("pagehide",i),function(){t.beforeunload(),document.removeEventListener("visibilitychange",e),window.removeEventListener("beforeunload",i),window.removeEventListener("pagehide",i)}},i}();!function(t){t.Init="init",t.Config="config",t.Start="start",t.Ready="ready",t.TokenComplete="token-complete",t.TokenStorage="token-storage",t.TokenFetch="token-fetch",t.TokenError="token-error",t.ConfigUuid="config-uuid",t.ConfigWebId="config-webid",t.ConfigDomain="config-domain",t.CustomWebId="custom-webid",t.AnonymousId="anonymous-id",t.TokenChange="token-change",t.TokenReset="token-reset",t.ConfigTransform="config-transform",t.EnvTransform="env-transform",t.SessionReset="session-reset",t.SessionResetTime="session-reset-time",t.Event="event",t.Events="events",t.EventNow="event-now",t.CleanEvents="clean-events",t.BeconEvent="becon-event",t.SubmitBefore="submit-before",t.SubmitScuess="submit-scuess",t.SubmitSuccess="submit-success",t.SubmitAfter="submit-after",t.SubmitError="submit-error",t.SubmitVerifyH="submit-verify-h5",t.Stay="stay",t.ResetStay="reset-stay",t.StayReady="stay-ready",t.SetStay="set-stay",t.RouteChange="route-change",t.RouteReady="route-ready",t.Ab="ab",t.AbVar="ab-var",t.AbAllVars="ab-all-vars",t.AbConfig="ab-config",t.AbExternalVersion="ab-external-version",t.AbVersionChangeOn="ab-version-change-on",t.AbVersionChangeOff="ab-version-change-off",t.AbOpenLayer="ab-open-layer",t.AbCloseLayer="ab-close-layer",t.AbReady="ab-ready",t.AbComplete="ab-complete",t.AbTimeout="ab-timeout",t.Profile="profile",t.ProfileSet="profile-set",t.ProfileSetOnce="profile-set-once",t.ProfileUnset="profile-unset",t.ProfileIncrement="profile-increment",t.ProfileAppend="profile-append",t.ProfileClear="profile-clear",t.TrackDuration="track-duration",t.TrackDurationStart="track-duration-start",t.TrackDurationEnd="track-duration-end",t.TrackDurationPause="track-duration-pause",t.TrackDurationResume="tracl-duration-resume",t.Autotrack="autotrack",t.AutotrackReady="autotrack-ready",t.CustomHeader="custom-request-header",t.TracerReady="tracer-ready"}(i||(i={})),function(t){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"}(a||(a={}));var r=function(){function t(){}return t.prototype.apply=function(t,e){var i=this;if(this.collect=t,this.config=e,this.config.enable_stay_duration){this.title=document.title||location.pathname,this.url=location.href,this.url_path=location.pathname,this.pageAlive=new n(t,e),this.pageClose=new o(t,e);var a=this.collect.Types;this.collect.on(a.ResetStay,function(t){var e=t.url_path,a=t.title,s=t.url;i.resetStayDuration(e,a,s)}),this.collect.on(a.RouteChange,function(t){t.init||e.disable_route_report||i.resetStayDuration()}),this.collect.on(a.SetStay,function(t){var e=t.url_path,a=t.title,s=t.url;i.setStayParmas(e,a,s)}),this.enable(this.url_path,this.title,this.url),this.ready(a.Stay),this.collect.emit(a.StayReady)}},t.prototype.ready=function(t){var e=this;if(this.collect.set(t),this.collect.hook._hooksCache.hasOwnProperty(t)){var i=this.collect.hook._hooksCache[t];if(!Object.keys(i).length)return;var a=function(t){i[t].length&&i[t].forEach(function(i){e.collect.hook.emit(t,i)})};for(var s in i)a(s)}},t.prototype.enable=function(t,e,i){this.pageAlive.enable(t,e,i),this.pageClose.enable(t,e,i)},t.prototype.disable=function(){this.pageAlive.disable(),this.pageClose.disable()},t.prototype.setStayParmas=function(t,e,i){void 0===t&&(t=""),void 0===e&&(e=""),void 0===i&&(i=""),this.pageAlive.setParams(t,e,i),this.pageClose.setParams(t,e,i),this.collect.emit(a.DEBUGGER_MESSAGE,{type:a.DEBUGGER_MESSAGE_SDK,info:"SDK 执行 resetStayParams",level:"info",time:Date.now(),data:{url_path:t,title:e,url:i}})},t.prototype.reset=function(t,e,i){this.disable(),this.enable(t,e,i)},t.prototype.resetStayDuration=function(t,e,i){this.reset(t,e,i),this.collect.emit(a.DEBUGGER_MESSAGE,{type:a.DEBUGGER_MESSAGE_SDK,info:"SDK 执行 resetStayDuration",level:"info",time:Date.now(),data:{url_path:t,title:e,url:i}})},t}();module.exports=r;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datarangers/sdk-javascript",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
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",
package/types/types.d.ts CHANGED
@@ -258,6 +258,8 @@ interface Sdk {
258
258
  resetStayDuration(url_path?: string, title?: string, url?: string): void;
259
259
  resetStayParams(url_path?: string, title?: string, url?: string): void;
260
260
 
261
+ destroyInstance(): void;
262
+
261
263
  profileSet(profile: any): void;
262
264
  profileSetOnce(profile: any): void;
263
265
  profileIncrement(profile: any): void;