@qbix/q.js 1.0.8 → 1.1.0
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/dist/Metrics.js +28 -4
- package/dist/Metrics.min.js +8 -7
- package/package.json +1 -1
package/dist/Metrics.js
CHANGED
|
@@ -204,20 +204,42 @@ function _bindVisibility() {
|
|
|
204
204
|
document.addEventListener('active', handleVisEvent, false);
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
// ── Unload / bfcache ──
|
|
207
|
+
// ── Unload / bfcache / background-foreground ──
|
|
208
208
|
|
|
209
209
|
Metrics._unloadBound = false;
|
|
210
|
+
Metrics._bgTime = null; // when backgrounded
|
|
211
|
+
Metrics._bgCooldown = false; // suppress rapid duplicate background events
|
|
212
|
+
Metrics._bgCooldownMs = 1000; // ignore repeated bg events within 1s
|
|
210
213
|
|
|
211
214
|
function _bindUnload() {
|
|
212
215
|
if (Metrics._unloadBound) return;
|
|
213
216
|
Metrics._unloadBound = true;
|
|
214
217
|
|
|
215
|
-
// Visibility-based exit (most reliable)
|
|
216
218
|
Metrics.onVisibilityChange(function (isVisible) {
|
|
217
219
|
if (!isVisible) {
|
|
220
|
+
// Backgrounded — send immediately (timers don't fire in bg tabs)
|
|
221
|
+
// but suppress if we just sent one within the cooldown
|
|
222
|
+
if (!Metrics._bgCooldown) {
|
|
223
|
+
Metrics._bgCooldown = true;
|
|
224
|
+
var elapsed = Math.round((Date.now() - Metrics._startTime) / 1000);
|
|
225
|
+
Metrics.send('background', { elapsed: elapsed });
|
|
226
|
+
}
|
|
227
|
+
Metrics._bgTime = Date.now();
|
|
218
228
|
_sendUnload();
|
|
219
229
|
} else {
|
|
220
|
-
|
|
230
|
+
// Foregrounded — reset cooldown so next background can fire
|
|
231
|
+
Metrics._bgCooldown = false;
|
|
232
|
+
Metrics._unloaded = false;
|
|
233
|
+
if (Metrics._bgTime) {
|
|
234
|
+
var awaySeconds = Math.round((Date.now() - Metrics._bgTime) / 1000);
|
|
235
|
+
Metrics._bgTime = null;
|
|
236
|
+
// Only report return if they were away > 1 second
|
|
237
|
+
// (rapid alt-tab = noise, background event already sent but
|
|
238
|
+
// no point sending a foreground for a sub-second switch)
|
|
239
|
+
if (awaySeconds > 1) {
|
|
240
|
+
Metrics.send('foreground', { away: awaySeconds });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
221
243
|
}
|
|
222
244
|
}, 'Metrics.unload');
|
|
223
245
|
|
|
@@ -230,15 +252,17 @@ function _bindUnload() {
|
|
|
230
252
|
window.addEventListener('pageshow', function (e) {
|
|
231
253
|
if (e.persisted) {
|
|
232
254
|
Metrics._unloaded = false;
|
|
255
|
+
Metrics._bgCooldown = false;
|
|
256
|
+
Metrics.send('foreground', { bfcache: true });
|
|
233
257
|
}
|
|
234
258
|
});
|
|
235
259
|
}
|
|
236
260
|
|
|
237
261
|
function _sendUnload() {
|
|
238
262
|
if (Metrics._unloaded) return;
|
|
239
|
-
Metrics._unloaded = true;
|
|
240
263
|
var elapsed = Math.round((Date.now() - Metrics._startTime) / 1000);
|
|
241
264
|
Metrics.send('unload:' + elapsed + 's');
|
|
265
|
+
Metrics._unloaded = true; // set AFTER send, not before
|
|
242
266
|
}
|
|
243
267
|
|
|
244
268
|
/**
|
package/dist/Metrics.min.js
CHANGED
|
@@ -4,13 +4,14 @@ $jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var c=$jscomp.global.
|
|
|
4
4
|
$jscomp.iteratorPrototype=function(c){$jscomp.initSymbolIterator();c={next:c};c[$jscomp.global.Symbol.iterator]=function(){return this};return c};$jscomp.iteratorFromArray=function(c,h){$jscomp.initSymbolIterator();c instanceof String&&(c+="");var d=0,a={next:function(){if(d<c.length){var b=d++;return{value:h(b,c[b]),done:!1}}a.next=function(){return{done:!0,value:void 0}};return a.next()}};a[Symbol.iterator]=function(){return a};return a};
|
|
5
5
|
$jscomp.polyfill=function(c,h,d,a){if(h){d=$jscomp.global;c=c.split(".");for(a=0;a<c.length-1;a++){var b=c[a];b in d||(d[b]={});d=d[b]}c=c[c.length-1];a=d[c];h=h(a);h!=a&&null!=h&&$jscomp.defineProperty(d,c,{configurable:!0,writable:!0,value:h})}};$jscomp.polyfill("Array.prototype.keys",function(c){return c?c:function(){return $jscomp.iteratorFromArray(this,function(c){return c})}},"es6","es3");
|
|
6
6
|
(function(c){function h(){function a(a){a=!("pause"===a.type||"resign"===a.type||("resume"===a.type||"active"===a.type?0:"hidden"===document.visibilityState));if(a!==b._visible){b._visible=a;for(var g=0;g<b._visibilityCallbacks.length;g++)try{b._visibilityCallbacks[g].fn(a)}catch(m){}}}if(!b._visibilityBound){b._visibilityBound=!0;for(var d=null,c=["","moz","ms","webkit","o"],h=0;h<c.length;h++){var k=c[h];if((k?k+"Hidden":"hidden")in document){d=k?k+"visibilitychange":"visibilitychange";break}}d&&
|
|
7
|
-
document.addEventListener(d,a,!1);document.addEventListener("pause",a,!1);document.addEventListener("resume",a,!1);document.addEventListener("resign",a,!1);document.addEventListener("active",a,!1)}}function d(){b._unloadBound||(b._unloadBound=!0,b.onVisibilityChange(function(g){g?b.
|
|
8
|
-
Math.round((Date.now()-b._startTime)/1E3);b.send("unload:"+a+"s")}}var b=c.Metrics||{};c.Metrics=b;b._sessionKey="metrics_sid";b._visitorKey="metrics_vid";b._sid=null;b._vid=null;b.getSessionId=function(){if(b._sid)return b._sid;
|
|
9
|
-
|
|
10
|
-
Date.now().toString(36)+Math.random().toString(36).slice(2);return b._vid=a};b._defaultEndpoint="https://invites.to/metrics";b._endpoint=null;b._page=null;b._extra=null;b._unloaded=!1;b._startTime=Date.now();b.send=function(a,d){if(b._endpoint
|
|
11
|
-
try{navigator.sendBeacon?navigator.sendBeacon(b._endpoint,new Blob([d],{type:"text/plain"})):fetch(b._endpoint,{method:"POST",headers:{"Content-Type":"text/plain"},keepalive:!0,body:d})}catch(n){}}};b._visible=!0;b._visibilityCallbacks=[];b._visibilityBound
|
|
12
|
-
key:d||null})};b._unloadBound=!1;b.init=function(a){a=a||{};b._endpoint=a.endpoint||b._defaultEndpoint;a.page&&(b._page=a.page);a.sessionKey&&(b._sessionKey=a.sessionKey);a.sessionId&&(b._sid=a.sessionId);a.extra&&
|
|
13
|
-
try{a.tz=Intl.DateTimeFormat().resolvedOptions().timeZone}catch(n){}try{navigator.connection&&navigator.connection.effectiveType&&(a.conn=navigator.connection.effectiveType)}catch(n){}try{var c=performance.getEntriesByType("navigation");
|
|
7
|
+
document.addEventListener(d,a,!1);document.addEventListener("pause",a,!1);document.addEventListener("resume",a,!1);document.addEventListener("resign",a,!1);document.addEventListener("active",a,!1)}}function d(){b._unloadBound||(b._unloadBound=!0,b.onVisibilityChange(function(g){g?(b._bgCooldown=!1,b._unloaded=!1,b._bgTime&&(g=Math.round((Date.now()-b._bgTime)/1E3),b._bgTime=null,1<g&&b.send("foreground",{away:g}))):(b._bgCooldown||(b._bgCooldown=!0,g=Math.round((Date.now()-b._startTime)/1E3),b.send("background",
|
|
8
|
+
{elapsed:g})),b._bgTime=Date.now(),a())},"Metrics.unload"),window.addEventListener("pagehide",function(){a()}),window.addEventListener("pageshow",function(a){a.persisted&&(b._unloaded=!1,b._bgCooldown=!1,b.send("foreground",{bfcache:!0}))}))}function a(){if(!b._unloaded){var a=Math.round((Date.now()-b._startTime)/1E3);b.send("unload:"+a+"s");b._unloaded=!0}}var b=c.Metrics||{};c.Metrics=b;b._sessionKey="metrics_sid";b._visitorKey="metrics_vid";b._sid=null;b._vid=null;b.getSessionId=function(){if(b._sid)return b._sid;
|
|
9
|
+
try{var a=sessionStorage.getItem(b._sessionKey);a||(a=Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2),sessionStorage.setItem(b._sessionKey,a));b._sid=a}catch(p){b._sid=Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2)}return b._sid};b.getVisitorId=function(){if(b._vid)return b._vid;var a=null;try{return a=localStorage.getItem(b._visitorKey),a||(a=Math.random().toString(36).slice(2)+Date.now().toString(36)+Math.random().toString(36).slice(2),localStorage.setItem(b._visitorKey,
|
|
10
|
+
a)),b._vid=a}catch(p){}try{return a=sessionStorage.getItem(b._visitorKey),a||(a=Math.random().toString(36).slice(2)+Date.now().toString(36)+Math.random().toString(36).slice(2),sessionStorage.setItem(b._visitorKey,a)),b._vid=a}catch(p){}a=Math.random().toString(36).slice(2)+Date.now().toString(36)+Math.random().toString(36).slice(2);return b._vid=a};b._defaultEndpoint="https://invites.to/metrics";b._endpoint=null;b._page=null;b._extra=null;b._unloaded=!1;b._startTime=Date.now();b.send=function(a,d){if(b._endpoint&&
|
|
11
|
+
!b._unloaded){a={session:b.getSessionId(),visitor:b.getVisitorId(),origin:location.origin,url:location.pathname+location.search+location.hash,page:b._page||document.title,label:a,t:Date.now()};b._extra&&(a.extra=b._extra);d&&(a.data=d);d=JSON.stringify(a);try{navigator.sendBeacon?navigator.sendBeacon(b._endpoint,new Blob([d],{type:"text/plain"})):fetch(b._endpoint,{method:"POST",headers:{"Content-Type":"text/plain"},keepalive:!0,body:d})}catch(n){}}};b._visible=!0;b._visibilityCallbacks=[];b._visibilityBound=
|
|
12
|
+
!1;b.isVisible=function(){return b._visible};b.onVisibilityChange=function(a,d){if(d)for(var c=0;c<b._visibilityCallbacks.length;c++)if(b._visibilityCallbacks[c].key===d){b._visibilityCallbacks[c].fn=a;return}b._visibilityCallbacks.push({fn:a,key:d||null})};b._unloadBound=!1;b._bgTime=null;b._bgCooldown=!1;b._bgCooldownMs=1E3;b.init=function(a){a=a||{};b._endpoint=a.endpoint||b._defaultEndpoint;a.page&&(b._page=a.page);a.sessionKey&&(b._sessionKey=a.sessionKey);a.sessionId&&(b._sid=a.sessionId);a.extra&&
|
|
13
|
+
(b._extra=a.extra);h();!1!==a.trackUnload&&d();a={referrer:document.referrer||"",screen:screen.width+"x"+screen.height,viewport:window.innerWidth+"x"+window.innerHeight,dpr:window.devicePixelRatio||1,lang:navigator.language||"",touch:"ontouchstart"in window||0<navigator.maxTouchPoints};try{a.tz=Intl.DateTimeFormat().resolvedOptions().timeZone}catch(n){}try{navigator.connection&&navigator.connection.effectiveType&&(a.conn=navigator.connection.effectiveType)}catch(n){}try{var c=performance.getEntriesByType("navigation");
|
|
14
|
+
c&&c[0]&&(a.navType=c[0].type)}catch(n){}b.send("loaded",a);return b}})("undefined"!==typeof window?window:this);
|
|
14
15
|
"undefined"!==typeof Q&&function(c){function h(a){var b=location.hash||"#";a=b.queryField("v",a);a!==b&&history.replaceState(history.state,document.title,a)}var d=c.Metrics=c.plugins.Metrics=window.Metrics;d.setState=function(a,b){var g=c.info.url;d.setState.pending[g]=c.setTimeout(function(){d.setState.pending[g]&&(clearTimeout(d.setState.pending[g]),delete d.setState.pending[g]);c.req("Metrics/update",[],null,{method:"POST",fields:{navigatorUrl:location.href,url:c.info.url,state:a,extra:JSON.stringify(b)},
|
|
15
16
|
keepalive:!0})},5E3)};d.setState.pending={};c.extend.dontCopy["Q.Users.User"]=!0;c.text.Metrics={};c.onReady.add(function(){c.onVisibilityChange&&c.onVisibilityChange.set&&c.onVisibilityChange.set(function(a){if(d._visible!==a){d._visible=a;for(var b=0;b<d._visibilityCallbacks.length;b++)try{d._visibilityCallbacks[b].fn(a)}catch(k){}}},"Metrics");var a=c.getObject("Metrics.navigationTracker",c.plugins)||c.getObject("Metrics.navigationTracker",c);a&&d.NavigationTracker&&(a.page=a.page||c.info.url||
|
|
16
17
|
document.title,d.NavigationTracker.init(a));(a=c.getObject("Metrics.mediaTracker",c.plugins)||c.getObject("Metrics.mediaTracker",c))&&d.MediaTracker&&d.MediaTracker.init(a);var b=d.NavigationTracker;if(b){var g=null;c.Tool.onActivate("Q/tabs").set(function(){this.state.onCurrent.set(function(a,d){clearTimeout(g);g=setTimeout(function(){d&&b.state.initialized&&b.opened("tab:"+d)},300)},"Metrics.NavigationTracker")},"Metrics.NavigationTracker");c.Tool.onActivate("Q/columns").set(function(){this.state.onActivate.set(function(a,
|