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

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

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

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

  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 = _rollupPluginBabelHelpers.objectWithoutProperties(_page$properties, _excluded);

    var _normalizedProperties = normalizeAvoEventProperties(_properties);

    return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({
      name: name,
      path: path,
      referrer: referrer,
      title: title,
      url: url
    }, _normalizedProperties), {}, (_objectSpread3 = {}, _rollupPluginBabelHelpers.defineProperty(_objectSpread3, constants.AVO_EVENT_NAME_SYMBOL, constants.AVO_PROXY_EVENT_LOG_PAGE_VIEW_NAME), _rollupPluginBabelHelpers.defineProperty(_objectSpread3, constants.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 _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.defineProperty({}, normalizedUserIdProperty, _userId), _normalizedProperties2), {}, (_objectSpread5 = {}, _rollupPluginBabelHelpers.defineProperty(_objectSpread5, constants.AVO_EVENT_NAME_SYMBOL, constants.AVO_PROXY_EVENT_IDENTIFY_USER), _rollupPluginBabelHelpers.defineProperty(_objectSpread5, constants.AVO_IDENTIFY_ACTION_PROPERTY, _userId), _objectSpread5));
  }

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

exports.mapTrackingDataToAvo = mapTrackingDataToAvo;
exports.normalizeAvoEventProperties = normalizeAvoEventProperties;
exports.normalizeAvoUserProperties = normalizeAvoUserProperties;
//# sourceMappingURL=mapper.js.map