|
2 | 2 | * @prettier |
3 | 3 | */ |
4 | 4 |
|
5 | | -(function(window, Raygun) { |
| 5 | +(function (window, Raygun) { |
6 | 6 | if (!window['RaygunObject'] || !window[window['RaygunObject']]) { |
7 | 7 | return; |
8 | 8 | } |
|
19 | 19 | crashReportingEnabled = false, |
20 | 20 | captureUnhandledRejections; |
21 | 21 |
|
22 | | - var hasSessionStorage = false; |
23 | | - try { |
24 | | - hasSessionStorage = !!window.sessionStorage; |
25 | | - } catch (e) { |
26 | | - // sessionStorage not available |
27 | | - } |
| 22 | + var hasSessionStorage = false; |
| 23 | + try { |
| 24 | + hasSessionStorage = !!window.sessionStorage; |
| 25 | + } catch (e) { |
| 26 | + // sessionStorage not available |
| 27 | + } |
28 | 28 |
|
29 | | - var metadata = { |
30 | | - ping : { |
31 | | - sessionStorageItem : 'raygun4js-successful-ping', |
32 | | - sendPing : true, |
33 | | - failedPings : 0 |
34 | | - }, |
35 | | - }; |
36 | 29 | var snippetOnErrorSignature = ['function (b,c,d,f,g){', '||(g=new Error(b)),a[e].q=a[e].q||[]']; |
37 | 30 |
|
38 | 31 | errorQueue = window[window['RaygunObject']].q; |
39 | 32 | var rg = Raygun; |
40 | 33 |
|
41 | | - var delayedExecutionFunctions = ['trackEvent', 'send', 'recordBreadcrumb','captureMissingRequests']; |
| 34 | + var delayedExecutionFunctions = [ |
| 35 | + 'trackEvent', |
| 36 | + 'send', |
| 37 | + 'recordBreadcrumb', |
| 38 | + 'captureMissingRequests', |
| 39 | + ]; |
42 | 40 |
|
43 | | - var parseSnippetOptions = function() { |
| 41 | + var parseSnippetOptions = function () { |
44 | 42 | snippetOptions = window[window['RaygunObject']].o; |
45 | 43 |
|
46 | | - for (var i in snippetOptions) { |
47 | | - if (snippetOptions.hasOwnProperty(i)) { |
48 | | - var pair = snippetOptions[i]; |
| 44 | + for (var i in snippetOptions) { |
| 45 | + if (snippetOptions.hasOwnProperty(i)) { |
| 46 | + var pair = snippetOptions[i]; |
49 | 47 | if (pair) { |
50 | 48 | if (delayedExecutionFunctions.indexOf(pair[0]) === -1) { |
51 | 49 | // Config pair, can execute immediately |
|
59 | 57 | } |
60 | 58 | }; |
61 | 59 |
|
62 | | - var executor = function(pair) { |
| 60 | + var executor = function (pair) { |
63 | 61 | var key = pair[0]; |
64 | 62 | var value = pair[1]; |
65 | 63 |
|
66 | 64 | if (key) { |
67 | 65 | switch (key) { |
68 | | - case 'sendPing': |
69 | | - metadata.ping.sendPing = value; |
70 | | - break; |
71 | 66 | // React Native only |
72 | 67 | case 'boot': |
73 | 68 | onLoadHandler(); |
|
175 | 170 | case 'trackEvent': |
176 | 171 | if (value.type && value.path) { |
177 | 172 | rg.trackEvent(value.type, { path: value.path }); |
178 | | - } else if(value.type && value.name && value.duration) { |
179 | | - rg.trackEvent(value.type, { name: value.name, duration: value.duration, offset: value.offset || 0 }); |
| 173 | + } else if (value.type && value.name && value.duration) { |
| 174 | + rg.trackEvent(value.type, { |
| 175 | + name: value.name, |
| 176 | + duration: value.duration, |
| 177 | + offset: value.offset || 0, |
| 178 | + }); |
180 | 179 | } else if (value.type && value.timings) { |
181 | 180 | rg.trackEvent(value.type, { timings: value.timings }); |
182 | 181 | } |
|
226 | 225 | case 'clientIp': |
227 | 226 | rg.setClientIp(value); |
228 | 227 | break; |
229 | | - case 'captureMissingRequests': |
| 228 | + case 'captureMissingRequests': |
230 | 229 | rg.captureMissingRequests(value); |
231 | 230 | break; |
232 | 231 | case 'captureUnhandledRejections': |
|
236 | 235 | } |
237 | 236 | }; |
238 | 237 |
|
239 | | - function ping() { |
240 | | - if(!Raygun.Options || !Raygun.Options._raygunApiKey || !Raygun.Options._raygunApiUrl){ |
241 | | - return; |
242 | | - } |
243 | | - |
244 | | - var url = Raygun.Options._raygunApiUrl + "/ping?apiKey=" + encodeURIComponent(Raygun.Options._raygunApiKey); |
245 | | - var data = { |
246 | | - crashReportingEnabled: crashReportingEnabled ? true : false, |
247 | | - realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false, |
248 | | - providerName: "raygun4js", |
249 | | - providerVersion: '{{VERSION}}' |
250 | | - }; |
251 | | - |
252 | | - // Check if we've already pinged with the same data |
253 | | - if (hasSessionStorage) { |
254 | | - var storedData = sessionStorage.getItem(metadata.ping.sessionStorageItem); |
255 | | - if (storedData && storedData === JSON.stringify(data)) { |
256 | | - return; |
257 | | - } |
258 | | - } |
259 | | - |
260 | | - fetch(url, { |
261 | | - method: 'POST', |
262 | | - headers: { |
263 | | - 'Content-Type': 'application/json' |
264 | | - }, |
265 | | - body: JSON.stringify(data) |
266 | | - }).then(function(response) { |
267 | | - if (response.ok) { |
268 | | - if (hasSessionStorage) { |
269 | | - // Record successful ping in local storage |
270 | | - sessionStorage.setItem(metadata.ping.sessionStorageItem, JSON.stringify(data)); |
271 | | - } |
272 | | - metadata.ping.failedPings = 0; |
273 | | - } else { |
274 | | - retryPing(metadata.ping.failedPings); |
275 | | - metadata.ping.failedPings++; |
276 | | - } |
277 | | - }).catch(function() { |
278 | | - retryPing(metadata.ping.failedPings); |
279 | | - metadata.ping.failedPings++; |
280 | | - }); |
281 | | - } |
282 | | - |
283 | | - var retryPing = function(failedPings) { |
284 | | - if (failedPings > 5) { |
285 | | - // Stop retrying after 5 failed attempts |
286 | | - return; |
287 | | - } |
288 | | - |
289 | | - // Generates a delay of 10/20/40/80/120 seconds |
290 | | - var backoffDelay = Math.min( |
291 | | - 10 * Math.pow(2, metadata.ping.failedPings), |
292 | | - 120 // 2 minutes |
293 | | - ) * 1000; |
294 | | - |
295 | | - // Retry after backoff delay |
296 | | - setTimeout(ping, backoffDelay); |
297 | | - }; |
298 | | - |
299 | | - var installGlobalExecutor = function() { |
300 | | - window[window['RaygunObject']] = function() { |
| 238 | + var installGlobalExecutor = function () { |
| 239 | + window[window['RaygunObject']] = function () { |
301 | 240 | return executor(arguments); |
302 | 241 | }; |
303 | 242 | window['RaygunInitialized'] = true; |
304 | 243 | globalExecutorInstalled = true; |
305 | 244 | }; |
306 | 245 |
|
307 | | - var onLoadHandler = function() { |
| 246 | + var onLoadHandler = function () { |
308 | 247 | parseSnippetOptions(); |
309 | 248 |
|
310 | 249 | if (noConflict) { |
311 | 250 | rg = Raygun.noConflict(); |
312 | 251 | } |
313 | | - |
| 252 | + |
314 | 253 | if (apiKey) { |
315 | 254 | if (!options) { |
316 | 255 | options = {}; |
|
353 | 292 | installGlobalExecutor(); |
354 | 293 | } |
355 | 294 |
|
356 | | - if(metadata.ping.sendPing) { |
357 | | - ping(); //call immediately |
358 | | - } |
359 | 295 | window[window['RaygunObject']].q = errorQueue; |
360 | 296 | }; |
361 | 297 |
|
362 | 298 | if (!Raygun.Utilities.isReactNative()) { |
363 | | - var supportsNavigationTiming = !!window.PerformanceObserver && !!window.PerformanceObserver.supportedEntryTypes && window.PerformanceObserver.supportedEntryTypes.includes('navigation'); |
| 299 | + var supportsNavigationTiming = |
| 300 | + !!window.PerformanceObserver && |
| 301 | + !!window.PerformanceObserver.supportedEntryTypes && |
| 302 | + window.PerformanceObserver.supportedEntryTypes.includes('navigation'); |
364 | 303 | if (document.readyState === 'complete') { |
365 | | - onLoadHandler(); |
| 304 | + onLoadHandler(); |
366 | 305 | } else if (supportsNavigationTiming) { |
367 | | - //The other 'load' events are called before the PerformanceNavigationTiming is completed resulting in `loadEventEnd` never being set which is needed to calculate the duration of the timing. This observer triggers after the timing is complete. |
368 | | - var observer = new window.PerformanceObserver(function () { |
369 | | - onLoadHandler(); |
370 | | - }); |
| 306 | + //The other 'load' events are called before the PerformanceNavigationTiming is completed resulting in `loadEventEnd` never being set which is needed to calculate the duration of the timing. This observer triggers after the timing is complete. |
| 307 | + var observer = new window.PerformanceObserver(function () { |
| 308 | + onLoadHandler(); |
| 309 | + }); |
371 | 310 |
|
372 | | - observer.observe({ entryTypes: ['navigation'] }); |
| 311 | + observer.observe({ entryTypes: ['navigation'] }); |
373 | 312 | } else if (window.addEventListener) { |
374 | 313 | window.addEventListener('load', onLoadHandler); |
375 | 314 | } else { |
|
0 commit comments