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

var Inspector;

try {
  // avo-inspector is an optional peer dependency, since not all consumers opt in for it
  // eslint-disable-next-line global-require
  Inspector = require('avo-inspector');
} catch (e) {
  Inspector = null;
}
/**
 * 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 (trackingEl) {
    var trackingOptions = trackingEl.dataset.trackingOptions ? JSON.parse(trackingEl.dataset.trackingOptions) : options;
    var autoTracking = trackingOptions.autoTracking !== false; // Avoids tracking if autoTracking is set to false in the tracking intent (when omitted default is true)

    if (!autoTracking) {
      return;
    }

    handlers.handleIdentify({
      trackingEl: trackingEl
    }, options);
  });
};
/**
 * 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 clientId, _options$clientVersio, clientVersion, _options$env, _options$env$amplitud, amplitudeApiKey, _options$env$avoApiKe, avoApiKey, _options$env$doodleEn, doodleEnv, services, _options$initAll, initAll, _options$autoTracking, autoTracking, avo, avoEnv, avoDestinations, _avoEnv;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            clientId = options.clientId, _options$clientVersio = options.clientVersion, clientVersion = _options$clientVersio === void 0 ? '' : _options$clientVersio, _options$env = options.env;
            _options$env = _options$env === void 0 ? {} : _options$env;
            _options$env$amplitud = _options$env.amplitudeApiKey, amplitudeApiKey = _options$env$amplitud === void 0 ? '' : _options$env$amplitud, _options$env$avoApiKe = _options$env.avoApiKey, avoApiKey = _options$env$avoApiKe === void 0 ? '' : _options$env$avoApiKe, _options$env$doodleEn = _options$env.doodleEnv, doodleEnv = _options$env$doodleEn === void 0 ? '' : _options$env$doodleEn, 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, avo = options.avo; // 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 = 45;
              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:
            if (!services.avo) {
              _context.next = 34;
              break;
            }

            _context.prev = 22;

            if (avo) {
              _context.next = 25;
              break;
            }

            throw new TypeError('Please provide the Avo file when enabling Avo as destination service. See https://www.avo.app/docs/implementation/devs-101#a-nameavo-generated-filea-avo-generated-file-containing-avo-functions');

          case 25:
            avoEnv = helpers.mapDoodleEnvToAvoEnv(doodleEnv);
            avoDestinations = destinations.getAvoDestinations(options);
            avo.initAvo.apply(avo, [{
              env: avoEnv
            }, {}, {}].concat(_rollupPluginBabelHelpers.toConsumableArray(avoDestinations)));
            _context.next = 34;
            break;

          case 30:
            _context.prev = 30;
            _context.t1 = _context["catch"](22);
            console.log('Error on Init:');
            throw _context.t1;

          case 34:
            if (!services.avoInspector) {
              _context.next = 45;
              break;
            }

            if (Inspector) {
              _context.next = 39;
              break;
            }

            throw new Error('Please install avo-inspector package on the consuming app of @doodle/tracking');

          case 39:
            if (avoApiKey) {
              _context.next = 43;
              break;
            }

            throw new TypeError('Please provide an Avo Inspector API key so @doodle/tracking can initialize its client');

          case 43:
            _avoEnv = helpers.mapDoodleEnvToAvoEnv(doodleEnv);
            options.avoInspector = new Inspector.AvoInspector({
              appName: clientId,
              version: clientVersion || "".concat(doodleEnv, "-0.0.0"),
              apiKey: avoApiKey,
              env: _avoEnv
            });

          case 45:
            // Perform declarative tracking which is not click based (for now, only identify calls)
            runDeclarativeTracking(options);
            return _context.abrupt("return", true);

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

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

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