Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/tracking / dist / cjs / src / core / init.js
Size: Mime:
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var index = require('../services/amplitude/index.js');
var handlers = require('./handlers.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
 *
 * @param {Object} initOptions An object that holds configuration options for @doodle/tracking
 * @param {string} initOptions.amplitudeApiKey The Amplitude key, usually retrieved from an env var
 * @param {boolean} [initOptions.initAll] Initialize all trackers. For now only Amplitude
 * @param {boolean} [initOptions.clickHandler] 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,
  clickHandler = true
} = {}) => {
  // Guard against SSR
  if (typeof document === 'undefined') {
    return false;
  } // Guard against DNT


  if (hasDntEnabled()) {
    return false;
  } // Opt-in click handling behavior


  if (clickHandler) {
    // We don't need to manually remove the listener as it is added only once per full page load
    document.body.addEventListener('click', handlers.handleTrackingClicks);
  } // Opt-in trackers initialization behavior


  if (initAll) {
    try {
      if (!amplitudeApiKey) {
        throw new Error('Please provide an Amplitude key so @doodle/tracking may initialize Amplitude');
      }

      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