Repository URL to install this package:
|
Version:
1.0.0-alpha.7 ▾
|
import { initAmplitude } from '../amplitude';
import { trackingClickHandler } from './trackingClickHandler';
const initTracking = async ({ amplitudeApiKey }, initAll = true) => {
// Guard against SSR
if (typeof document === 'undefined') {
return;
}
// Guard against DNT (Do Not Track) - FF returns 'unspecified' when disabled
// Note: FF does not support JS retrieving of per-site DNT disabling (shield icon), unless by checking for a honeypot
// See https://stackoverflow.com/a/35070265/462849
const dnt = navigator.doNotTrack || window.doNotTrack;
if (dnt && !['0', 'unspecified'].includes(dnt)) {
return;
}
if (initAll) {
try {
await initAmplitude(amplitudeApiKey);
} catch (e) {
console.warn('Error on Init:');
console.error(e);
}
}
// We don't need to manually remove the listener as it is only added once,
// when the Gatsby browser runtime first starts, and gets removed on full page reload
document.body.addEventListener('click', trackingClickHandler);
};
export { initTracking };