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 / esm / src / core / init.js
Size: Mime:
import { initAmplitude } from '../services/amplitude/index.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}
 */

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 {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
 */


const initTracking = async ({
  env: {
    amplitudeApiKey
  } = {},
  initAll = true,
  autoTracking = true
} = {}) => {
  // 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', 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 initAmplitude(amplitudeApiKey);
    } catch (e) {
      console.error('Error on Init:');
      throw e;
    }
  }

  return true;
};

export { hasDntEnabled, initTracking };
//# sourceMappingURL=init.js.map