Skip to content

Commit

Permalink
Merge pull request #532 from MindscapeHQ/ph/limit-ping
Browse files Browse the repository at this point in the history
Limit Ping to be less frequent.
  • Loading branch information
phillip-haydon authored Nov 25, 2024
2 parents e0349ce + 0abe2f5 commit 5cedcbb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "raygun4js",
"version": "3.1.0",
"version": "3.1.2",
"homepage": "http://raygun.com",
"authors": [
"Mindscape <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion raygun4js.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>raygun4js</id>
<version>3.1.0</version>
<version>3.1.2</version>
<title>Raygun4js</title>
<authors>Raygun Limited</authors>
<owners>Raygun Limited</owners>
Expand Down
75 changes: 52 additions & 23 deletions src/raygun.loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@
crashReportingEnabled = false,
captureUnhandledRejections;

var hasSessionStorage = false;
try {
hasSessionStorage = !!window.sessionStorage;
} catch (e) {
// sessionStorage not available
}

var metadata = {
ping : {
sessionStorageItem : 'raygun4js-successful-ping',
sendPing : true,
pingIntervalId : -1,
failedPings : 0
},
};
Expand Down Expand Up @@ -230,41 +237,64 @@
};

function ping() {
if(metadata.ping.failedPings > 2) {
clearInterval(metadata.ping.pingIntervalId);
}

if(!Raygun.Options || !Raygun.Options._raygunApiKey || !Raygun.Options._raygunApiUrl){
metadata.ping.failedPings++;
return;
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}}'
};

// 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: {
'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 (hasSessionStorage) {
// Record successful ping in local storage
sessionStorage.setItem(metadata.ping.sessionStorageItem, JSON.stringify(data));
}
}).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() {
Expand Down Expand Up @@ -325,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;
};
Expand Down

0 comments on commit 5cedcbb

Please sign in to comment.