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    
Size: Mime:
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var storage = require('../../helpers/storage.js');

/**
 * Loads the optional AmplitudeJs dependency.
 * @returns {object|null} - The AmplitudeJs module or null if not available.
 */

var loadAmplitudeJs = function loadAmplitudeJs() {
  try {
    // eslint-disable-next-line global-require
    return require('amplitude-js');
  } catch (e) {
    return null;
  }
};

var amplitudeJs = loadAmplitudeJs(); // https://developers.amplitude.com/docs/advanced-settings#cookies-created-by-the-sdk
// https://github.com/amplitude/Amplitude-JavaScript/blob/6c8c9618975f683e5a566f2adf65d203cff72772/src/constants.js#L22
// https://github.com/amplitude/Amplitude-JavaScript/blob/4aaa26f45919c5587bcc3fb21510ca8afbc41e4b/test/amplitude-client.js#L18
// https://github.com/amplitude/Amplitude-JavaScript/blob/26e78136cad9cd393c2b0a91607ad47129d01c50/src/constants.js#L17-L19
// https://github.com/amplitude/Amplitude-JavaScript/blob/26e78136cad9cd393c2b0a91607ad47129d01c50/test/amplitude-client.js#L167
// https://github.com/amplitude/Amplitude-JavaScript/blob/26e78136cad9cd393c2b0a91607ad47129d01c50/src/options.js#L18

var COOKIE_NAME_REGEXES = [/amp_[0-9a-zA-Z]{6}/, /amplitude_id_.*/]; // https://github.com/amplitude/Amplitude-JavaScript/blob/4aaa26f45919c5587bcc3fb21510ca8afbc41e4b/src/cookiestorage.js#L24

var LOCAL_STORAGE_ITEM_NAME_REGEXES = [/amp_cookiestore_[0-9a-zA-Z]{6}/, /amplitude_.*/];
/**
 * Initializes Amplitude.
 *
 * @async
 * @private
 * @param {TrackingApiOptions} options - The API client's options
 * @param {Object} [amplitudeOptions] Library initialization options
 * @return {Promise.<undefined|Object>}
 * @see https://github.com/amplitude/Amplitude-JavaScript/blob/18d0dab/src/options.js - options for version 5.11.0
 */

var initAmplitude = /*#__PURE__*/function () {
  var _ref = _rollupPluginBabelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(options) {
    var amplitudeOptions,
        _options$env,
        env,
        _env$amplitudeApiKey,
        amplitudeApiKey,
        amplitudeClient,
        initPromise,
        _args = arguments;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            amplitudeOptions = _args.length > 1 && _args[1] !== undefined ? _args[1] : {
              includeGclid: true,
              includeReferrer: true,
              includeUtm: true,
              saveParamsReferrerOncePerSession: false,

              /**
               * Disabling cookies will cause localStorage to be used by Amplitude SDK.
               * This has the side effect, that users cannot be tracked across subdomains (eg doodle.com -> ethz.doodle.com), but that is
               * anyway better for GDPR compliance.
               * See: https://developers.amplitude.com/docs/advanced-settings#disabling-cookies
               */
              disableCookies: true
            };
            _options$env = options.env, env = _options$env === void 0 ? {} : _options$env;
            _env$amplitudeApiKey = env.amplitudeApiKey, amplitudeApiKey = _env$amplitudeApiKey === void 0 ? '' : _env$amplitudeApiKey;

            if (amplitudeJs) {
              _context.next = 7;
              break;
            }

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

          case 7:
            if (amplitudeApiKey) {
              _context.next = 9;
              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 9:
            options.amplitude = amplitudeJs;
            amplitudeClient = amplitudeJs.getInstance();

            initPromise = function initPromise() {
              for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
                args[_key] = arguments[_key];
              }

              return new Promise(function (resolve, reject) {
                try {
                  amplitudeClient.init.apply(amplitudeClient, args.concat([resolve]));
                  amplitudeClient.setOptOut(false);
                  resolve(true);
                } catch (e) {
                  reject(e);
                }
              });
            }; // passing null here since we don't pass the user ID with the init


            return _context.abrupt("return", initPromise(amplitudeApiKey, null, amplitudeOptions));

          case 13:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function initAmplitude(_x) {
    return _ref.apply(this, arguments);
  };
}();
/**
 * Removes the Amplitude cookies from the browser.
 * @param {string|null} cookieDomain - Optionally, the cookie domain to use when removing cookies.
 */


var removeAmplitudeCookies = function removeAmplitudeCookies() {
  var cookieDomain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  storage.removeCookies(COOKIE_NAME_REGEXES, cookieDomain);
};
/**
 * Removes the Amplitude items from local storage.
 */


var removeAmplitudeLocalStorageItems = function removeAmplitudeLocalStorageItems() {
  storage.removeLocalStorageItems(LOCAL_STORAGE_ITEM_NAME_REGEXES);
};
/**
 * Removes Amplitude.
 *
 * @async
 * @private
 * @param {TrackingApiOptions} options - The API client's options
 * @return {Promise.<boolean>}
 */
// eslint-disable-next-line require-await


var removeAmplitude = /*#__PURE__*/function () {
  var _ref2 = _rollupPluginBabelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(options) {
    var _options$env2, env, amplitudeClient;

    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _options$env2 = options.env, env = _options$env2 === void 0 ? {} : _options$env2; // amplitude-js is present, nothing to remove

            if (options.amplitude) {
              _context2.next = 3;
              break;
            }

            return _context2.abrupt("return", true);

          case 3:
            amplitudeClient = options.amplitude.getInstance(); // Toggling opt-out on the Amplitude client will block the client from sending further events

            amplitudeClient.setOptOut(true);
            amplitudeJs = null;
            options.amplitude = null; // Cleanup existing cookies and local storage items.

            removeAmplitudeCookies(env.cookieDomain);
            removeAmplitudeLocalStorageItems();
            return _context2.abrupt("return", true);

          case 10:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));

  return function removeAmplitude(_x2) {
    return _ref2.apply(this, arguments);
  };
}();

exports.initAmplitude = initAmplitude;
exports.loadAmplitudeJs = loadAmplitudeJs;
exports.removeAmplitude = removeAmplitude;
exports.removeAmplitudeCookies = removeAmplitudeCookies;
exports.removeAmplitudeLocalStorageItems = removeAmplitudeLocalStorageItems;
//# sourceMappingURL=init.js.map