@internetarchive/analytics-manager
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -32,8 +32,14 @@ export class AnalyticsManager { | ||
const url = this.generateTrackingUrl(values).toString(); | ||
if (!this.requireImagePing && | ||
typeof window.navigator !== 'undefined' && | ||
typeof window.navigator.sendBeacon !== 'undefined') { | ||
window.navigator.sendBeacon(url); | ||
if (this.requireImagePing) { | ||
this.sendPingViaImage(url); | ||
return; | ||
} | ||
else { | ||
// `navigator` has to be bound to ensure it does not error in some browsers | ||
// https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch | ||
const send = navigator.sendBeacon && navigator.sendBeacon.bind(navigator); | ||
try { | ||
// if `send` is `undefined` it'll throw and fall back to the image ping | ||
send(url); | ||
} | ||
catch (err) { | ||
this.sendPingViaImage(url); | ||
@@ -40,0 +46,0 @@ } |
{ | ||
"name": "@internetarchive/analytics-manager", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A manager for Internet Archive analytics.", | ||
@@ -5,0 +5,0 @@ "author": "Internet Archive", |
@@ -123,11 +123,17 @@ export type AnalyticsEventConfig = { | ||
const url = this.generateTrackingUrl(values).toString(); | ||
if ( | ||
!this.requireImagePing && | ||
typeof window.navigator !== 'undefined' && | ||
typeof window.navigator.sendBeacon !== 'undefined' | ||
) { | ||
window.navigator.sendBeacon(url); | ||
} else { | ||
if (this.requireImagePing) { | ||
this.sendPingViaImage(url); | ||
return; | ||
} | ||
// `navigator` has to be bound to ensure it does not error in some browsers | ||
// https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch | ||
const send = navigator.sendBeacon && navigator.sendBeacon.bind(navigator); | ||
try { | ||
// if `send` is `undefined` it'll throw and fall back to the image ping | ||
send!(url); | ||
} catch (err) { | ||
this.sendPingViaImage(url); | ||
} | ||
} | ||
@@ -134,0 +140,0 @@ |
Sorry, the diff of this file is not supported yet
126621
1417