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 _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var dispatcher = require('../services/amplitude/dispatcher.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}
 */

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] - The API client's options
 * @return {Promise<boolean>} - true for successful initialization, and false for exits on guard conditions
 */


var initTracking = /*#__PURE__*/function () {
  var _ref = _rollupPluginBabelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(options) {
    var _options$env, amplitudeApiKey, services, _options$initAll, initAll, _options$autoTracking, autoTracking;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _options$env = options.env;
            _options$env = _options$env === void 0 ? {
              amplitudeApiKey: ''
            } : _options$env;
            amplitudeApiKey = _options$env.amplitudeApiKey, services = options.services, _options$initAll = options.initAll, initAll = _options$initAll === void 0 ? true : _options$initAll, _options$autoTracking = options.autoTracking, autoTracking = _options$autoTracking === void 0 ? true : _options$autoTracking; // Guard against SSR

            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', listener.handleTrackingClicks.bind(null, options));
            } // Opt-in trackers initialization behavior


            if (!initAll) {
              _context.next = 21;
              break;
            }

            if (!services.amplitude) {
              _context.next = 21;
              break;
            }

            _context.prev = 10;

            if (amplitudeApiKey) {
              _context.next = 13;
              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 13:
            _context.next = 15;
            return dispatcher.initAmplitude(amplitudeApiKey);

          case 15:
            _context.next = 21;
            break;

          case 17:
            _context.prev = 17;
            _context.t0 = _context["catch"](10);
            console.error('Error on Init:');
            throw _context.t0;

          case 21:
            return _context.abrupt("return", true);

          case 22:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[10, 17]]);
  }));

  return function initTracking(_x) {
    return _ref.apply(this, arguments);
  };
}();

exports.hasDntEnabled = hasDntEnabled;
exports.initTracking = initTracking;
//# sourceMappingURL=init.js.map