diff options
Diffstat (limited to 'js/piwik.js')
-rw-r--r-- | js/piwik.js | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/js/piwik.js b/js/piwik.js index 50ebfea82e..4bc799d6d6 100644 --- a/js/piwik.js +++ b/js/piwik.js @@ -3553,6 +3553,15 @@ if (typeof window.Piwik !== 'object') { image.src = configTrackerUrl + (configTrackerUrl.indexOf('?') < 0 ? '?' : '&') + request; } + function shouldForcePost(request) + { + if (configRequestMethod === 'POST') { + return true; + } + // we force long single request urls and bulk requests over post + return request && (request.length > 2000 || request.indexOf('{"requests"') === 0); + } + function supportsSendBeacon() { return 'object' === typeof navigatorAlias @@ -3560,7 +3569,7 @@ if (typeof window.Piwik !== 'object') { && 'function' === typeof Blob; } - function sendPostRequestViaSendBeacon(request, callback) + function sendPostRequestViaSendBeacon(request, callback, fallbackToGet) { var isSupported = supportsSendBeacon(); @@ -3576,7 +3585,7 @@ if (typeof window.Piwik !== 'object') { try { var blob = new Blob([request], headers); - if (request.length <= 2000) { + if (fallbackToGet && !shouldForcePost(request)) { blob = new Blob([], headers); url = url + (url.indexOf('?') < 0 ? '?' : '&') + request; } @@ -3604,7 +3613,7 @@ if (typeof window.Piwik !== 'object') { fallbackToGet = true; } - if (isPageUnloading && sendPostRequestViaSendBeacon(request, callback)) { + if (isPageUnloading && sendPostRequestViaSendBeacon(request, callback, fallbackToGet)) { return; } @@ -3619,7 +3628,7 @@ if (typeof window.Piwik !== 'object') { // same request a second time. To avoid this, we delay the actual execution of this POST request just // by 50ms which gives it usually enough time to detect the unload event in most cases. - if (isPageUnloading && sendPostRequestViaSendBeacon(request, callback)) { + if (isPageUnloading && sendPostRequestViaSendBeacon(request, callback, fallbackToGet)) { return; } var sentViaBeacon; @@ -3639,7 +3648,7 @@ if (typeof window.Piwik !== 'object') { // fallback on error xhr.onreadystatechange = function () { if (this.readyState === 4 && !(this.status >= 200 && this.status < 300)) { - var sentViaBeacon = isPageUnloading && sendPostRequestViaSendBeacon(request, callback); + var sentViaBeacon = isPageUnloading && sendPostRequestViaSendBeacon(request, callback, fallbackToGet); if (!sentViaBeacon && fallbackToGet) { getImage(request, callback); @@ -3660,7 +3669,7 @@ if (typeof window.Piwik !== 'object') { xhr.send(request); } catch (e) { - sentViaBeacon = isPageUnloading && sendPostRequestViaSendBeacon(request, callback); + sentViaBeacon = isPageUnloading && sendPostRequestViaSendBeacon(request, callback, fallbackToGet); if (!sentViaBeacon && fallbackToGet) { getImage(request, callback); } else if (typeof callback === 'function') { @@ -3841,12 +3850,12 @@ if (typeof window.Piwik !== 'object') { makeSureThereIsAGapAfterFirstTrackingRequestToPreventMultipleVisitorCreation(function () { - if (configAlwaysUseSendBeacon && sendPostRequestViaSendBeacon(request, callback)) { + if (configAlwaysUseSendBeacon && sendPostRequestViaSendBeacon(request, callback, true)) { setExpireDateTime(100); return; } - if (configRequestMethod === 'POST' || String(request).length > 2000) { + if (shouldForcePost(request)) { sendXmlHttpRequest(request, callback); } else { getImage(request, callback); @@ -3906,7 +3915,13 @@ if (typeof window.Piwik !== 'object') { var i = 0, bulk; for (i; i < chunks.length; i++) { bulk = '{"requests":["?' + chunks[i].join('","?') + '"]}'; - sendXmlHttpRequest(bulk, null, false); + if (configAlwaysUseSendBeacon && sendPostRequestViaSendBeacon(bulk, null, false)) { + // makes sure to load the next page faster by not waiting as long + // we apply this once we know send beacon works + setExpireDateTime(100); + } else { + sendXmlHttpRequest(bulk, null, false); + } } setExpireDateTime(delay); |