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 / api.js
Size: Mime:
'use strict';

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var utils = require('../helpers/utils.js');
var handlers = require('./handlers.js');
var init = require('./init.js');

var API = {
  /**
   * Factory method for initializing @doodle/tracking library, returning an instance of an API
   *
   * @public
   * @async
   * @param {TrackingApiOptions} options - The configuration options for API client
   * @return {API} - An initialized instance of API class
   */
  init: function init$1(options) {
    var _this = this;

    // Avoid initializing twice
    if (this._isInitialized) {
      return this;
    }

    this.options = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, options), {}, {
      services: utils.sampleServices(options.services, Math.random())
    }); // Guard for Do Not Track

    this._hasDntEnabled = utils.hasDntEnabled();

    if (!this._hasDntEnabled) {
      init.initTracking(this.options).then(function (isInitialized) {
        _this._isInitialized = isInitialized;
      });
    } else {
      this._isInitialized = Promise.resolve(true);
    }

    this.track = this.track.bind(this);
    this.identify = this.identify.bind(this);
    this.page = this.page.bind(this);
    return this;
  },

  /**
   * Method to be used for instance when logging out without reloading the page
   *
   * @public
   * @return {boolean} - Indicates the success of the operation
   */
  reset: function reset() {
    delete this.options._userId;
    return true;
  },

  /**
   * Client method for tracking
   *
   * It may be called either with a trackingIntent or with a trackingEl, which carries tracking data attributes
   * In case both are passed: trackingIntent is preferred
   *
   * @public
   * @async
   * @param {Object} payload - Either a trackingIntent or a trackingEl must be part of the payload
   * @param {TrackingIntent} [payload.trackingIntent] - The tracking intent passed to the API client
   * @param {HTMLElement} [payload.trackingEl] - An HTML element with tracking data attributes
   * @param {MouseEvent} [payload.event] - A click event to be cancelled in case of same tab links
   * @return {Promise<boolean>}
   */
  track: function track(_ref) {
    var trackingIntent = _ref.trackingIntent,
        trackingEl = _ref.trackingEl,
        event = _ref.event;

    if (this._hasDntEnabled) {
      return;
    }

    return handlers.handleTrack({
      trackingIntent: trackingIntent,
      trackingEl: trackingEl,
      event: event
    }, this.options);
  },

  /**
   * Client method for user identification
   *
   * @public
   * @async
   * @param {Object} [payload] - Either a trackingIntent or a trackingEl must be part of the payload
   * @param {TrackingIntent} [payload.trackingIntent] - The tracking intent passed to the API client
   * @param {HTMLElement} [payload.trackingEl] - An HTML element with tracking data attributes
   * @return {Promise<boolean>}
   */
  identify: function identify(_ref2) {
    var trackingIntent = _ref2.trackingIntent,
        trackingEl = _ref2.trackingEl;

    if (this._hasDntEnabled) {
      return;
    }

    return handlers.handleIdentify({
      trackingIntent: trackingIntent,
      trackingEl: trackingEl
    }, this.options);
  },

  /**
   * Client method for page identification
   *
   * @public
   * @async
   * @param {Object} [payload] - Either a trackingIntent or a trackingEl must be part of the payload
   * @param {TrackingIntent} [payload.trackingIntent] - The tracking intent passed to the API client
   * @param {HTMLElement} [payload.trackingEl] - An HTML element with tracking data attributes
   * @return {Promise<boolean>}
   */
  page: function page(_ref3) {
    var trackingIntent = _ref3.trackingIntent,
        trackingEl = _ref3.trackingEl;

    if (this._hasDntEnabled) {
      return;
    }

    return handlers.handlePage({
      trackingIntent: trackingIntent,
      trackingEl: trackingEl
    }, this.options);
  }
};

module.exports = API;
//# sourceMappingURL=api.js.map