diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2016-01-18 04:23:20 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2016-01-18 04:23:20 +0300 |
commit | b88b165f066fe462f06b4960202a4c126d149dae (patch) | |
tree | a60f12d4c05f71bc99b6f1fd97deaaa23d20236b /js/piwik.js | |
parent | b2e67c9bc62250881f28ed474f95184028aa314e (diff) |
detect whether window had focus only once and assume it had focus if used inside an iframe
Diffstat (limited to 'js/piwik.js')
-rw-r--r-- | js/piwik.js | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/js/piwik.js b/js/piwik.js index f72117bceb..64998cd0cd 100644 --- a/js/piwik.js +++ b/js/piwik.js @@ -957,7 +957,7 @@ if (typeof JSON2 !== 'object' && typeof window.JSON === 'object' && window.JSON. /*global unescape */ /*global ActiveXObject */ /*members encodeURIComponent, decodeURIComponent, getElementsByTagName, - shift, unshift, piwikAsyncInit, + shift, unshift, piwikAsyncInit, frameElement, self, hasFocus, createElement, appendChild, characterSet, charset, all, addEventListener, attachEvent, removeEventListener, detachEvent, disableCookies, cookie, domain, readyState, documentElement, doScroll, title, text, @@ -2727,6 +2727,18 @@ if (typeof Piwik !== 'object') { ); } + function isInsideAnIframe () { + if (isDefined(windowAlias.frameElement)) { + return (windowAlias.frameElement && String(windowAlias.frameElement.nodeName).toLowerCase() === 'iframe'); + } + + try { + return windowAlias.self !== windowAlias.top; + } catch (e) { + return true; + } + } + /************************************************************ * End Page Overlay ************************************************************/ @@ -2913,6 +2925,11 @@ if (typeof Piwik !== 'object') { // Guard against installing the activity tracker more than once per Tracker instance heartBeatSetUp = false, + // bool used to detect whether this browser window had focus at least once. So far we cannot really + // detect this 100% correct for an iframe so whenever Piwik is loaded inside an iframe we presume + // the window had focus at least once. + hadWindowFocusAtLeastOnce = isInsideAnIframe(), + // Timestamp of last tracker request sent to Piwik lastTrackerRequestTime = null, @@ -3229,9 +3246,14 @@ if (typeof Piwik !== 'object') { heartBeatTimeout = setTimeout(function heartBeat() { heartBeatTimeout = null; - if (documentAlias.hasFocus && !documentAlias.hasFocus()) { - // only send a ping if the tab actually has focus. For example do not send a ping if - // window was opened via "right click => open in new window" and never had focus see #9504 + if (!hadWindowFocusAtLeastOnce) { + // if browser does not support .hasFocus (eg IE5), we assume that the window has focus. + hadWindowFocusAtLeastOnce = (!documentAlias.hasFocus || documentAlias.hasFocus()); + } + + if (!hadWindowFocusAtLeastOnce) { + // only send a ping if the tab actually had focus at least once. For example do not send a ping + // if window was opened via "right click => open in new window" and never had focus see #9504 heartBeatUp(configHeartBeatDelay); return; } @@ -3261,6 +3283,8 @@ if (typeof Piwik !== 'object') { } function heartBeatOnFocus() { + hadWindowFocusAtLeastOnce = true; + // since it's possible for a user to come back to a tab after several hours or more, we try to send // a ping if the page is active. (after the ping is sent, the heart beat timeout will be set) if (heartBeatPingIfActivityAlias()) { |