Skip to content

Commit

Permalink
detect whether window had focus only once and assume it had focus if …
Browse files Browse the repository at this point in the history
…used inside an iframe
  • Loading branch information
tsteur committed Jan 18, 2016
1 parent b2e67c9 commit b88b165
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 48 deletions.
32 changes: 28 additions & 4 deletions js/piwik.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
************************************************************/
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand Down
Loading

0 comments on commit b88b165

Please sign in to comment.