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 utils = require('../helpers/utils.js');
var dispatcher = require('../services/amplitude/dispatcher.js');
var listener = require('./listener.js');

/**
 * Runs declarative tracking calls which are not based on clicks
 *
 * @private
 * @return {undefined}
 */

var runDeclarativeTracking = function runDeclarativeTracking(options) {
  var identifyTrackers = _rollupPluginBabelHelpers.toConsumableArray(document.querySelectorAll('[data-identify]'));

  identifyTrackers.forEach(function (node) {
    return listener.handleDeclarativeTracking(options, {
      target: node
    });
  });
};
/**
 * Initializes @doodle/tracking and its dependencies (currently only amplitude-js needs it)
 * Also by default enables declarative tracking (automatically handle tracking data attributes)
 *
 * 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 Server Side Rendering

            if (!(typeof document === 'undefined')) {
              _context.next = 5;
              break;
            }

            return _context.abrupt("return", false);

          case 5:
            if (!utils.hasDntEnabled()) {
              _context.next = 7;
              break;
            }

            return _context.abrupt("return", false);

          case 7:
            // Enable automated click handling behavior for declarative tracking
            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.handleDeclarativeTracking.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:
            // Perform tracking for declarative tracking which is not click based (for now, only identify calls)
            runDeclarativeTracking(options);
            return _context.abrupt("return", true);

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

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

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