@qbix/q.js 1.0.8 → 1.1.1

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 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
- Metrics._unloaded = false; // returned to page
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
  /**
@@ -1,16 +1,25 @@
1
+ var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(k,t,a){k!=Array.prototype&&k!=Object.prototype&&(k[t]=a.value)};$jscomp.getGlobal=function(k){return"undefined"!=typeof window&&window===k?k:"undefined"!=typeof global&&null!=global?global:k};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";
2
+ if (typeof window !== "undefined") {
3
+ $jscomp.global = window;
4
+ } else if (typeof self !== "undefined") {
5
+ $jscomp.global = self;
6
+ } else if (typeof global !== "undefined") {
7
+ $jscomp.global = global;
8
+ }
1
9
  var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(c,h,d){c!=Array.prototype&&c!=Object.prototype&&(c[h]=d.value)};$jscomp.getGlobal=function(c){return"undefined"!=typeof window&&window===c?c:"undefined"!=typeof global&&null!=global?global:c};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";
2
10
  $jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.Symbol=function(){var c=0;return function(h){return $jscomp.SYMBOL_PREFIX+(h||"")+c++}}();
3
11
  $jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var c=$jscomp.global.Symbol.iterator;c||(c=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));"function"!=typeof Array.prototype[c]&&$jscomp.defineProperty(Array.prototype,c,{configurable:!0,writable:!0,value:function(){return $jscomp.arrayIterator(this)}});$jscomp.initSymbolIterator=function(){}};$jscomp.arrayIterator=function(c){var h=0;return $jscomp.iteratorPrototype(function(){return h<c.length?{done:!1,value:c[h++]}:{done:!0}})};
4
12
  $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
13
  $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
14
  (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._unloaded=!1:a()},"Metrics.unload"),window.addEventListener("pagehide",function(){a()}),window.addEventListener("pageshow",function(a){a.persisted&&(b._unloaded=!1)}))}function a(){if(!b._unloaded){b._unloaded=!0;var a=
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;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};
9
- 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,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)+
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&&!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);
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=!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,
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&&(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};
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");c&&c[0]&&(a.navType=c[0].type)}catch(n){}b.send("loaded",a);return b}})("undefined"!==typeof window?window:this);
15
+ 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",
16
+ {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;
17
+ 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,
18
+ 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&&
19
+ !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=
20
+ !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&&
21
+ (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");
22
+ c&&c[0]&&(a.navType=c[0].type)}catch(n){}b.send("loaded",a);return b}})("undefined"!==typeof window?window:this);
14
23
  "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
24
  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
25
  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,
@@ -84,4 +93,5 @@ c);a._lastTimeUpdate=c;a.lastPosition=c},1E3)});b.addEventListener(Twitch.Player
84
93
  var d=genId(c);state.tracked[d]||function(){if(root.Twitch&&root.Twitch.Player)h();else{var a=document.createElement("script");a.src="https://player.twitch.tv/js/embed/v1.js";a.onload=function(){h()};document.head.appendChild(a)}}()}
85
94
  function trackMuseAi(c){var h=genId(c);if(!state.tracked[h]){var d=createTracker(h,"museai",c,0);state.tracked[h]=d;window.addEventListener("message",function(a){if(a.data&&a.source===c.contentWindow){try{var b="string"===typeof a.data?JSON.parse(a.data):a.data}catch(g){return}"play"===b.event?(d.playing=!0,d.lastPosition=b.currentTime||0,d._lastTimeUpdate=d.lastPosition,d.duration=b.duration||d.duration,sendEvent(d,"media-play"),startCheckpoints(d)):"pause"===b.event?d.playing&&(d.playing=!1,a=b.currentTime||
86
95
  0,d.watched.add(d._lastTimeUpdate,a),d.lastPosition=a,stopCheckpoints(d),sendEvent(d,"media-pause")):"ended"===b.event?(d.playing=!1,d.watched.add(d._lastTimeUpdate,d.duration),d.lastPosition=d.duration,stopCheckpoints(d),sendEvent(d,"media-ended")):"timeupdate"===b.event&&(a=b.currentTime||0,d.duration=b.duration||d.duration,d.playing&&a>d._lastTimeUpdate&&d.watched.add(d._lastTimeUpdate,a),d._lastTimeUpdate=a,d.lastPosition=a)}});try{c.contentWindow.postMessage({method:"addEventListener",value:"play"},
87
- "*")}catch(a){}try{c.contentWindow.postMessage({method:"addEventListener",value:"pause"},"*")}catch(a){}try{c.contentWindow.postMessage({method:"addEventListener",value:"ended"},"*")}catch(a){}try{c.contentWindow.postMessage({method:"addEventListener",value:"timeupdate"},"*")}catch(a){}}};
96
+ "*")}catch(a){}try{c.contentWindow.postMessage({method:"addEventListener",value:"pause"},"*")}catch(a){}try{c.contentWindow.postMessage({method:"addEventListener",value:"ended"},"*")}catch(a){}try{c.contentWindow.postMessage({method:"addEventListener",value:"timeupdate"},"*")}catch(a){}}};
97
+ export default window.Q;