Repository URL to install this package:
|
Version:
1.0.1 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var index = require('../services/amplitude/index.js');
var listener = require('./listener.js');
/**
* Informs if DNT (Do Not Track) is enabled (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
*
* @return {boolean}
*/
const hasDntEnabled = () => {
const dnt = navigator.doNotTrack || window.doNotTrack;
return Boolean(dnt && !['0', 'unspecified'].includes(dnt));
};
/**
* Initializes @doodle/tracking and its dependencies (currently only Amplitude)
* Also by default enables the convenience data attributes handling
*
* To be invoked in your app's client side initialization
*
* @private
* @param {Object} options An object that holds configuration options for the API client
* @param {string} options.amplitudeApiKey The Amplitude key, usually retrieved from an env var
* @param {boolean} [options.initAll] Initialize all trackers. For now only Amplitude
* @param {boolean} [options.autoTracking] Controls whether the automated handling of tracking clicks occur
* @return {Promise<boolean>} true for successful initialization, and false for exits on guard conditions
*/
const initTracking = async ({
amplitudeApiKey,
initAll = true,
autoTracking = true
} = {
amplitudeApiKey: ''
}) => {
// Guard against SSR
if (typeof document === 'undefined') {
return false;
} // Guard against DNT
if (hasDntEnabled()) {
return false;
} // Opt-in click handling behavior
if (autoTracking) {
// We don't need to manually remove the listener as it is added only once per full page load
document.body.addEventListener('click', listener.handleTrackingClicks);
} // Opt-in trackers initialization behavior
if (initAll) {
try {
if (!amplitudeApiKey) {
throw new TypeError('Please provide an Amplitude key (e.g. as an AMPLITUDE_API_KEY env var) so @doodle/tracking can initialize its client');
}
await index.initAmplitude(amplitudeApiKey);
} catch (e) {
console.error('Error on Init:');
throw e;
}
}
return true;
};
exports.hasDntEnabled = hasDntEnabled;
exports.initTracking = initTracking;
//# sourceMappingURL=init.js.map