Repository URL to install this package:
|
Version:
1.4.1 ▾
|
import { asyncToGenerator as _asyncToGenerator } from '../../_virtual/_rollupPluginBabelHelpers.js';
import { initAmplitude } from '../services/amplitude/dispatcher.js';
import { handleTrackingClicks } from './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}
*/
var hasDntEnabled = function hasDntEnabled() {
var 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 {TrackingApiOptions} options An object that holds configuration options for the API client
* @return {Promise<boolean>} true for successful initialization, and false for exits on guard conditions
*/
var initTracking = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var _ref2,
_ref2$env,
amplitudeApiKey,
_ref2$initAll,
initAll,
_ref2$autoTracking,
autoTracking,
_args = arguments;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_ref2 = _args.length > 0 && _args[0] !== undefined ? _args[0] : {}, _ref2$env = _ref2.env;
_ref2$env = _ref2$env === void 0 ? {} : _ref2$env;
amplitudeApiKey = _ref2$env.amplitudeApiKey, _ref2$initAll = _ref2.initAll, initAll = _ref2$initAll === void 0 ? true : _ref2$initAll, _ref2$autoTracking = _ref2.autoTracking, autoTracking = _ref2$autoTracking === void 0 ? true : _ref2$autoTracking;
if (!(typeof document === 'undefined')) {
_context.next = 5;
break;
}
return _context.abrupt("return", false);
case 5:
if (!hasDntEnabled()) {
_context.next = 7;
break;
}
return _context.abrupt("return", false);
case 7:
// 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', handleTrackingClicks);
} // Opt-in trackers initialization behavior
if (!initAll) {
_context.next = 20;
break;
}
_context.prev = 9;
if (amplitudeApiKey) {
_context.next = 12;
break;
}
throw new TypeError('Please provide an Amplitude key (e.g. as an AMPLITUDE_API_KEY env var) so @doodle/tracking can initialize its client');
case 12:
_context.next = 14;
return initAmplitude(amplitudeApiKey);
case 14:
_context.next = 20;
break;
case 16:
_context.prev = 16;
_context.t0 = _context["catch"](9);
console.error('Error on Init:');
throw _context.t0;
case 20:
return _context.abrupt("return", true);
case 21:
case "end":
return _context.stop();
}
}
}, _callee, null, [[9, 16]]);
}));
return function initTracking() {
return _ref.apply(this, arguments);
};
}();
export { hasDntEnabled, initTracking };
//# sourceMappingURL=init.js.map