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

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

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

var _DEFAULT_SERVICES;
/**
 * @constant DEFAULT_SERVICES
 * @private
 */

var DEFAULT_SERVICES = (_DEFAULT_SERVICES = {}, _rollupPluginBabelHelpers.defineProperty(_DEFAULT_SERVICES, constants.SERVICES.AMPLITUDE, true), _rollupPluginBabelHelpers.defineProperty(_DEFAULT_SERVICES, constants.SERVICES.DOODLE_DATA_LAYER, false), _rollupPluginBabelHelpers.defineProperty(_DEFAULT_SERVICES, constants.SERVICES.GA, true), _DEFAULT_SERVICES);
/**
 * Helper function to parse data tracking attributes into a trackingIntent
 *
 * @param {HTMLElement} el - An HTML element to inspect
 * @return {Object}
 */

var getTrackingIntentFromDOM = function getTrackingIntentFromDOM(el) {
  var _el$dataset = el.dataset,
      trackingOptions = _el$dataset.trackingOptions,
      track = _el$dataset.track,
      identify = _el$dataset.identify,
      page = _el$dataset.page;

  var trackingIntent = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, trackingOptions && {
    options: JSON.parse(trackingOptions)
  }), track && {
    track: JSON.parse(track)
  }), page && {
    page: JSON.parse(page)
  }), identify && {
    identify: JSON.parse(identify)
  });

  return trackingIntent;
};
/**
 * Helper function that returns an object that knows about the link behavior
 * and return them mapped to a trackingElBehavior object
 *
 * @param {HTMLElement} el - An HTML element to inspect
 * @return {{href: string, isSameTabLink: boolean}} - A representation of the element's click behavior
 */


var getTrackingElBehavior = function getTrackingElBehavior(el) {
  var href = el.getAttribute('href');
  var target = el.getAttribute('target');
  var trackingElBehavior = {
    href: href,
    isSameTabLink: Boolean(href ? target !== '_blank' : true)
  };
  return trackingElBehavior;
};
/**
 * Enriches a trackingIntent with defaults (e.g. destination services and env vars) and, in case of declarative
 * tracking, link behavior information.
 *
 * This function must be called with a source containing either a trackingIntent or a trackingEl (a DOM element with
 * tracking data attributes). In case both are passed, the intent is preferred
 *
 * Returns an empty object for absent input otherwise a trackingDefinition
 *
 * @param {Object} source - A source for the tracking intent, from where the tracking definition is produced
 * @param {?TrackingIntent} [source.trackingIntent] - The tracking intent passed to the API client
 * @param {?HTMLElement} [source.trackingEl] - An HTML element with tracking data attributes
 * @param {TrackingApiOptions} [options] - The API client's options
 * @return {?TrackingDefinition} - The tracking definition needed by handlers to dispatch calls
 */


var getTrackingDefinition = function getTrackingDefinition() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref$trackingIntent = _ref.trackingIntent,
      trackingIntent = _ref$trackingIntent === void 0 ? {} : _ref$trackingIntent,
      _ref$trackingEl = _ref.trackingEl,
      trackingEl = _ref$trackingEl === void 0 ? null : _ref$trackingEl;

  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var hasTrackingIntent = trackingIntent && Object.keys(trackingIntent).length > 0;

  if (!(hasTrackingIntent || trackingEl)) {
    throw new TypeError('Please provide either a trackingIntent or a trackingEl');
  } // Extract defaults from definition passed on API client initialization


  var _options$clientId = options.clientId,
      clientId = _options$clientId === void 0 ? '' : _options$clientId,
      _options$services = options.services,
      defaultServices = _options$services === void 0 ? DEFAULT_SERVICES : _options$services,
      _options$env = options.env;
  _options$env = _options$env === void 0 ? {} : _options$env;
  var doodleEnv = _options$env.doodleEnv,
      nodeEnv = _options$env.nodeEnv,
      svcDataLayerApi = _options$env.svcDataLayerApi; // Prefer trackingIntent if a non-empty one was provided. Otherwise, if a tracking element was provided, find tracking intent there (in the tracking data attributes)

  var foundTrackingIntent = hasTrackingIntent ? trackingIntent : getTrackingIntentFromDOM(trackingEl); // Extract tracking intent's options and data

  var _foundTrackingIntent$ = foundTrackingIntent.options,
      intentTrackingOptions = _foundTrackingIntent$ === void 0 ? {} : _foundTrackingIntent$,
      intentTrackingData = _rollupPluginBabelHelpers.objectWithoutProperties(foundTrackingIntent, ["options"]); // Define services by overwriting defaults with custom that may be provided in the tracking intent


  var services = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, defaultServices), intentTrackingOptions.services);

  var trackingDefinition = {
    trackingData: intentTrackingData,
    trackingOptions: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({
      clientId: clientId,
      env: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, doodleEnv && {
        doodleEnv: doodleEnv
      }), nodeEnv && {
        nodeEnv: nodeEnv
      }), svcDataLayerApi && {
        svcDataLayerApi: svcDataLayerApi
      })
    }, intentTrackingOptions), {}, {
      services: services
    }, trackingEl && getTrackingElBehavior(trackingEl))
  };
  return trackingDefinition;
};

exports.getTrackingDefinition = getTrackingDefinition;
exports.getTrackingElBehavior = getTrackingElBehavior;
exports.getTrackingIntentFromDOM = getTrackingIntentFromDOM;
//# sourceMappingURL=getTrackingDefinition.js.map