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 { asyncToGenerator as _asyncToGenerator } from '../../_virtual/_rollupPluginBabelHelpers.js';
import { initAmplitude } from '../services/amplitude/dispatcher.js';
import { handleTrackingClicks } from './listener.js';

/**
 * Initializes @doodle/tracking and its dependencies (currently only amplitude-js needs it)
 * Also by default enables declarative tracking (automatically handle tracking data attributes)
 *
 * @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 = _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:
            // 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.bind(null, options));
            } // Opt-in trackers initialization behavior


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

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

            _context.prev = 8;

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

          case 13:
            _context.next = 19;
            break;

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

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

          case 20:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[8, 15]]);
  }));

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

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