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 / esm / src / services / avo / mapper.js
Size: Mime:
import { objectSpread2 as _objectSpread2, defineProperty as _defineProperty, objectWithoutProperties as _objectWithoutProperties } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { AVO_EVENT_NAME_SYMBOL, AVO_IDENTIFY_ACTION_PROPERTY, AVO_PROXY_EVENT_LOG_PAGE_VIEW_NAME, AVO_LOG_EVENT_ACTION_PROPERTY, AVO_USER_PROPERTY_PREFIX, AVO_PROXY_EVENT_IDENTIFY_USER } from './constants.js';
import { normalizeAvoName } from './helpers.js';

var _excluded = ["path", "referrer", "title", "url"];

var normalizeAvoEventProperties = function normalizeAvoEventProperties() {
  var properties = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return Object.keys(properties).reduce(function (obj, trackingProperty) {
    var camelCasedProperty = normalizeAvoName(trackingProperty);
    obj[camelCasedProperty] = properties[trackingProperty];
    return obj;
  }, {});
};
/**
 * Normalizes a Avo user property.
 * This is necessary, because Avo cannot have an event and user property with the same name and generic names
 * like email and userId might be used on both - event and user properties.
 * @param {string} property - The property to normalize.
 */


var normalizeAvoUserProperty = function normalizeAvoUserProperty(property) {
  var firstLetterCapitalized = property.charAt(0).toUpperCase() + property.slice(1);
  return AVO_USER_PROPERTY_PREFIX + firstLetterCapitalized;
};
/**
 * Normalizes Avo user properties.
 * @returns {object} - The normalized properties.
 */


var normalizeAvoUserProperties = function normalizeAvoUserProperties() {
  var properties = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return Object.keys(properties).reduce(function (obj, property) {
    var normalizedUserProperty = normalizeAvoUserProperty(property);
    obj[normalizedUserProperty] = properties[property];
    return obj;
  }, {});
};
/**
 * Formats a trackingIntent object to be compatible with Avo API
 *
 * @param {TrackingDefinitionData} trackingData - An object produced by getTrackingDefinition function
 * @return {Object}
 */


var mapTrackingDataToAvo = function mapTrackingDataToAvo(_ref) {
  var track = _ref.track,
      page = _ref.page,
      identify = _ref.identify;

  if (track) {
    var _objectSpread2$1;

    var properties = track.properties,
        type = track.type,
        _track$userId = track.userId,
        userId = _track$userId === void 0 ? '' : _track$userId;
    var normalizedProperties = normalizeAvoEventProperties(properties);
    return _objectSpread2(_objectSpread2({
      type: type,
      userId: userId
    }, normalizedProperties), {}, (_objectSpread2$1 = {}, _defineProperty(_objectSpread2$1, AVO_EVENT_NAME_SYMBOL, track.event), _defineProperty(_objectSpread2$1, AVO_IDENTIFY_ACTION_PROPERTY, userId), _objectSpread2$1));
  }

  if (page) {
    var _objectSpread3;

    var name = page.name,
        _page$properties = page.properties,
        path = _page$properties.path,
        referrer = _page$properties.referrer,
        title = _page$properties.title,
        url = _page$properties.url,
        _properties = _objectWithoutProperties(_page$properties, _excluded);

    var _normalizedProperties = normalizeAvoEventProperties(_properties);

    return _objectSpread2(_objectSpread2({
      name: name,
      path: path,
      referrer: referrer,
      title: title,
      url: url
    }, _normalizedProperties), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, AVO_EVENT_NAME_SYMBOL, AVO_PROXY_EVENT_LOG_PAGE_VIEW_NAME), _defineProperty(_objectSpread3, AVO_LOG_EVENT_ACTION_PROPERTY, name), _objectSpread3));
  }

  if (identify) {
    var _objectSpread5;

    var _userId = identify.userId,
        _properties2 = identify.properties;
    /**
     * Because Avo does not support naming event and user properties the same (eg, cannot re-use "email" as a user property if
     * it is already defined in an event), we need to prefix the property names to make them unique.
     */

    var normalizedUserIdProperty = normalizeAvoUserProperty('userId');

    var _normalizedProperties2 = normalizeAvoUserProperties(_properties2);

    return _objectSpread2(_objectSpread2(_defineProperty({}, normalizedUserIdProperty, _userId), _normalizedProperties2), {}, (_objectSpread5 = {}, _defineProperty(_objectSpread5, AVO_EVENT_NAME_SYMBOL, AVO_PROXY_EVENT_IDENTIFY_USER), _defineProperty(_objectSpread5, AVO_IDENTIFY_ACTION_PROPERTY, _userId), _objectSpread5));
  }

  throw new TypeError('Please check a valid tracking intent was provided');
};

export { mapTrackingDataToAvo, normalizeAvoEventProperties, normalizeAvoUserProperties };
//# sourceMappingURL=mapper.js.map