From 1a42ea59afdd7656db8c9d8c107e64231aa5c9f1 Mon Sep 17 00:00:00 2001 From: phillip-haydon Date: Wed, 20 Nov 2024 09:32:57 +1300 Subject: [PATCH 1/4] only check for 1 successful ping and store in local storage --- src/raygun.loader.js | 69 ++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/src/raygun.loader.js b/src/raygun.loader.js index ee16319a..f3495327 100644 --- a/src/raygun.loader.js +++ b/src/raygun.loader.js @@ -19,6 +19,13 @@ crashReportingEnabled = false, captureUnhandledRejections; + var hasLocalStorage = false; + try { + hasLocalStorage = !!window.localStorage; + } catch (e) { + // localStorage not available + } + var metadata = { ping : { sendPing : true, @@ -230,41 +237,61 @@ }; function ping() { - if(metadata.ping.failedPings > 2) { - clearInterval(metadata.ping.pingIntervalId); + if(!Raygun.Options || !Raygun.Options._raygunApiKey || !Raygun.Options._raygunApiUrl){ + return; } - if(!Raygun.Options || !Raygun.Options._raygunApiKey || !Raygun.Options._raygunApiUrl){ - metadata.ping.failedPings++; - return; + // Don't ping if we've already had a successful ping + if (hasLocalStorage && localStorage.getItem('raygun4js_successful_ping') === 'true') { + return; } var url = Raygun.Options._raygunApiUrl + "/ping?apiKey=" + encodeURIComponent(Raygun.Options._raygunApiKey); var data = { - crashReportingEnabled: crashReportingEnabled ? true : false, - realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false, - providerName: "raygun4js", - providerVersion: '{{VERSION}}' + crashReportingEnabled: crashReportingEnabled ? true : false, + realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false, + providerName: "raygun4js", + providerVersion: '{{VERSION}}' }; fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(data) + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) }).then(function(response) { - if (response.ok) { - metadata.ping.failedPings = 0; - } else { - // Request failed - metadata.ping.failedPings++; + if (response.ok) { + if (hasLocalStorage) { + // Record successful ping in local storage + localStorage.setItem('raygun4js_successful_ping', 'true'); } - }).catch(function() { + metadata.ping.failedPings = 0; + } else { + retryPing(metadata.ping.failedPings); metadata.ping.failedPings++; + } + }).catch(function() { + retryPing(metadata.ping.failedPings); + metadata.ping.failedPings++; }); } + var retryPing = function(failedPings) { + if (failedPings > 5) { + // Stop retrying after 5 failed attempts + return; + } + + // Generates a delay of 10/20/40/80/120 seconds + var backoffDelay = Math.min( + 10 * Math.pow(2, metadata.ping.failedPings), + 120 // 2 minutes + ) * 1000; + + // Retry after backoff delay + setTimeout(ping, backoffDelay); + } var installGlobalExecutor = function() { window[window['RaygunObject']] = function() { @@ -325,7 +352,7 @@ if(metadata.ping.sendPing) { ping(); //call immediately - metadata.ping.pingIntervalId = setInterval(ping, 1000 * 60 * 5); //5 minutes + // metadata.ping.pingIntervalId = setInterval(ping, 1000 * 60 * 5); //5 minutes } window[window['RaygunObject']].q = errorQueue; }; From bcea58554ac93a8e107d1b8de17bc21742fc3b29 Mon Sep 17 00:00:00 2001 From: phillip-haydon Date: Wed, 20 Nov 2024 11:04:11 +1300 Subject: [PATCH 2/4] changed to use sessionStorage and check data for re-ping if data changes --- src/raygun.loader.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/raygun.loader.js b/src/raygun.loader.js index f3495327..5a231256 100644 --- a/src/raygun.loader.js +++ b/src/raygun.loader.js @@ -19,15 +19,16 @@ crashReportingEnabled = false, captureUnhandledRejections; - var hasLocalStorage = false; + var hasSessionStorage = false; try { - hasLocalStorage = !!window.localStorage; + hasSessionStorage = !!window.sessionStorage; } catch (e) { - // localStorage not available + // sessionStorage not available } var metadata = { ping : { + sessionStorageItem : 'raygun4js-successful-ping', sendPing : true, pingIntervalId : -1, failedPings : 0 @@ -241,11 +242,6 @@ return; } - // Don't ping if we've already had a successful ping - if (hasLocalStorage && localStorage.getItem('raygun4js_successful_ping') === 'true') { - return; - } - var url = Raygun.Options._raygunApiUrl + "/ping?apiKey=" + encodeURIComponent(Raygun.Options._raygunApiKey); var data = { crashReportingEnabled: crashReportingEnabled ? true : false, @@ -254,6 +250,14 @@ providerVersion: '{{VERSION}}' }; + // Check if we've already pinged with the same data + if (hasSessionStorage) { + var storedData = sessionStorage.getItem(metadata.ping.sessionStorageItem); + if (storedData && storedData === JSON.stringify(data)) { + return; + } + } + fetch(url, { method: 'POST', headers: { @@ -262,9 +266,9 @@ body: JSON.stringify(data) }).then(function(response) { if (response.ok) { - if (hasLocalStorage) { + if (hasSessionStorage) { // Record successful ping in local storage - localStorage.setItem('raygun4js_successful_ping', 'true'); + sessionStorage.setItem(metadata.ping.sessionStorageItem, JSON.stringify(data)); } metadata.ping.failedPings = 0; } else { @@ -291,7 +295,7 @@ // Retry after backoff delay setTimeout(ping, backoffDelay); - } + }; var installGlobalExecutor = function() { window[window['RaygunObject']] = function() { From 7fd8096abf18ab3633fa723253a1ca76f1da2c52 Mon Sep 17 00:00:00 2001 From: phillip-haydon Date: Wed, 20 Nov 2024 11:09:28 +1300 Subject: [PATCH 3/4] removed commented out code --- src/raygun.loader.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/raygun.loader.js b/src/raygun.loader.js index 5a231256..3d8b57ec 100644 --- a/src/raygun.loader.js +++ b/src/raygun.loader.js @@ -30,7 +30,6 @@ ping : { sessionStorageItem : 'raygun4js-successful-ping', sendPing : true, - pingIntervalId : -1, failedPings : 0 }, }; @@ -356,7 +355,6 @@ if(metadata.ping.sendPing) { ping(); //call immediately - // metadata.ping.pingIntervalId = setInterval(ping, 1000 * 60 * 5); //5 minutes } window[window['RaygunObject']].q = errorQueue; }; From 0abe2f5f1593817b7d2794337ea8c1c285ef57d5 Mon Sep 17 00:00:00 2001 From: phillip-haydon Date: Wed, 20 Nov 2024 12:57:51 +1300 Subject: [PATCH 4/4] updated version --- CHANGELOG.md | 5 +++++ bower.json | 2 +- package.json | 2 +- raygun4js.nuspec | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6c97715..db1bd9d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 --> +## [3.1.2] + +### Changed +- Updated when ping happens so it only happens once, and when successful, it will store in sessionStorage and not ping again. This is to prevent multiple pings from happening on every page load. + ## [3.1.1] ### Fixed diff --git a/bower.json b/bower.json index 84384aa0..d9cc173b 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "raygun4js", - "version": "3.1.0", + "version": "3.1.2", "homepage": "http://raygun.com", "authors": [ "Mindscape " diff --git a/package.json b/package.json index c7eb99d3..3b78eec4 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ ], "title": "Raygun4js", "description": "Raygun.com plugin for JavaScript", - "version": "3.1.1", + "version": "3.1.2", "homepage": "https://github.com/MindscapeHQ/raygun4js", "author": { "name": "MindscapeHQ", diff --git a/raygun4js.nuspec b/raygun4js.nuspec index 5ede8313..c49945ac 100644 --- a/raygun4js.nuspec +++ b/raygun4js.nuspec @@ -2,7 +2,7 @@ raygun4js - 3.1.0 + 3.1.2 Raygun4js Raygun Limited Raygun Limited