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 constants = require('../constants.js');

/**
 * @constant DEFAULT_SERVICES
 * @private
 */

const DEFAULT_SERVICES = {
  [constants.SERVICES.AMPLITUDE]: true,
  [constants.SERVICES.DOODLE_DATA_LAYER]: false,
  [constants.SERVICES.GA]: true
};
/**
 * Helper function to parse data tracking attributes into a trackingIntent
 *
 * @param {HTMLElement} el An HTML element to inspect
 * @return {Object}
 */

const getTrackingIntentFromDOM = el => {
  const {
    dataset: {
      trackingOptions,
      track,
      identify,
      page
    }
  } = el;
  const trackingIntent = { ...(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
 */


const getTrackingElBehavior = el => {
  const href = el.getAttribute('href');
  const target = el.getAttribute('target');
  const trackingElBehavior = {
    href,
    isSameTabLink: Boolean(href ? target !== '_blank' : true)
  };
  return trackingElBehavior;
};
/**
 * Function to enhance a trackingIntent with defaults (e.g. destination services) and link behavior information
 *
 * 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
 *
 * Returns an empty object for absent input otherwise a trackingDefinition
 *
 * @param {Object} storage A storage from where to extract the tracking definition
 * @param {TrackingIntent} [storage.trackingIntent] A tracking intent provided by consuming project
 * @param {HTMLElement} [storage.trackingEl] An HTML element with tracking data attributes
 * @param {Object} [defaults] Default options
 * @param {Object} [defaults.services] Default services
 * @return {?TrackingDefinition} The tracking definition needed by handlers to dispatch calls
 */


const getTrackingDefinition = ({
  trackingIntent = {},
  trackingEl = null
} = {}, defaults = {}) => {
  const hasTrackingData = Object.keys(trackingIntent).length > 0;
  const defaultServices = defaults.services || DEFAULT_SERVICES;

  if (hasTrackingData) {
    const {
      options = {},
      ...trackingDataOnly
    } = trackingIntent;
    const services = { ...defaultServices,
      ...options.services
    };
    const trackingDefinition = {
      trackingData: trackingDataOnly,
      trackingOptions: {
        services,
        ...options,
        ...(trackingEl && getTrackingElBehavior(trackingEl))
      }
    };
    return trackingDefinition;
  }

  if (trackingEl) {
    const trackingIntentFromDataset = getTrackingIntentFromDOM(trackingEl);
    const {
      options = {},
      ...trackingDataOnly
    } = trackingIntentFromDataset;
    const services = { ...defaultServices,
      ...options.services
    }; // eslint-disable-next-line no-unused-vars

    const {
      services: _,
      ...optionsWithoutServices
    } = options;
    const trackingDefinition = {
      trackingData: trackingDataOnly,
      trackingOptions: {
        services: { ...options.services,
          ...services
        },
        ...optionsWithoutServices,
        ...getTrackingElBehavior(trackingEl)
      }
    };
    return trackingDefinition;
  }

  return null;
};

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