|
67 | 67 | //Force to lower case if its a string |
68 | 68 | eventMethod = eventMethod.toLowerCase ? eventMethod.toLowerCase() : eventMethod; |
69 | 69 |
|
70 | | - //Use the Beacon API if eventMethod is set null, true, or 'beacon'. |
71 | | - var enableBeacon = (eventMethod === null || eventMethod === true || eventMethod === "beacon" || eventMethod === "true") ? true : false; |
| 70 | + // Use the Beacon API if eventMethod is set null, true, or 'beacon'. |
| 71 | + var isBeaconRequested = (eventMethod === null) || (eventMethod === true) || (eventMethod === "beacon") || (eventMethod === "true"); |
72 | 72 | // Fall back to POST or GET for browsers which don't support Beacon API |
73 | | - useBeacon = enableBeacon && navigator && navigator.sendBeacon; |
| 73 | + var isBeaconAvailable = Boolean(isBeaconRequested && navigator && navigator.sendBeacon); |
| 74 | + var useBeacon = (isBeaconAvailable && isBeaconRequested); |
74 | 75 |
|
75 | | - //Use POST if specified, or beacon is unavailable. |
76 | | - var usePost = (eventMethod === "post" || (enableBeacon && !useBeacon)) ? true : false; |
77 | | - // Fall back to GET for browsers which don't support CORS XMLHttpRequests (e.g. IE <= 9) |
78 | | - usePost = usePost && window.XMLHttpRequest && ('withCredentials' in new XMLHttpRequest()); |
| 76 | + // Use GET if specified |
| 77 | + var isGetRequested = (eventMethod === "get"); |
79 | 78 |
|
80 | | - var path = useBeacon || usePost ? postPath : '/i'; |
| 79 | + // Use POST if specified |
| 80 | + var isPostRequested = (eventMethod === "post"); |
| 81 | + // usePost acts like a catch all for POST methods - Beacon or XHR |
| 82 | + var usePost = (isPostRequested || useBeacon) && !isGetRequested; |
| 83 | + // Don't use POST for browsers which don't support CORS XMLHttpRequests (e.g. IE <= 9) |
| 84 | + usePost = usePost && Boolean(window.XMLHttpRequest && ('withCredentials' in new XMLHttpRequest())); |
| 85 | + |
| 86 | + // Resolve all options and capabilities and decide path |
| 87 | + var path = usePost ? postPath : '/i'; |
81 | 88 |
|
82 | 89 | bufferSize = (localStorageAccessible() && useLocalStorage && usePost && bufferSize) || 1; |
83 | 90 |
|
|
234 | 241 |
|
235 | 242 | var nextRequest = outQueue[0]; |
236 | 243 |
|
237 | | - if (usePost || useBeacon) { |
| 244 | + if (usePost) { |
238 | 245 |
|
239 | 246 | var xhr = initializeXMLHttpRequest(configCollectorUrl); |
240 | 247 |
|
|
285 | 292 | var beaconStatus; |
286 | 293 |
|
287 | 294 | if (useBeacon) { |
288 | | - beaconStatus = navigator.sendBeacon(configCollectorUrl, encloseInPayloadDataEnvelope(attachStmToEvent(batch))); |
| 295 | + const headers = { type: 'application/json' }; |
| 296 | + const blob = new Blob([encloseInPayloadDataEnvelope(attachStmToEvent(batch))], headers); |
| 297 | + beaconStatus = navigator.sendBeacon(configCollectorUrl, blob); |
289 | 298 | } |
290 | 299 | if (!useBeacon || !beaconStatus) { |
291 | 300 | xhr.send(encloseInPayloadDataEnvelope(attachStmToEvent(batch))); |
292 | 301 | } |
293 | 302 | } |
294 | 303 |
|
295 | 304 | } else { |
296 | | - |
297 | 305 | var image = new Image(1, 1); |
298 | 306 |
|
299 | 307 | image.onload = function () { |
|
0 commit comments