Repository URL to install this package:
|
Version:
1.14.2 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var constants = require('../../constants.js');
var utils = require('../../helpers/utils.js');
/**
* Formats a trackingIntent object to be compatible with Google Analytics tracker API
*
* It assumes there is only one preferred tracking command per tracking intent. If multiple are provided,
* the destination tracking data is formatted observing the following precedence: `track` then `identify`
*
* @param {TrackingDefinitionData} trackingData - An object produced by getTrackingDefinition function
* @return {Object}
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation
* @see https://support.google.com/analytics/answer/1033068#Anatomy
*/
var mapTrackingDataToGoogleAnalytics = function mapTrackingDataToGoogleAnalytics(_ref) {
var track = _ref.track,
identify = _ref.identify;
if (track) {
var eventAction = track.event,
_track$type = track.type,
eventCategory = _track$type === void 0 ? constants.EVENT_TYPE.userInteraction : _track$type,
_track$properties = track.properties,
_track$properties$cat = _track$properties.category,
eventPage = _track$properties$cat === void 0 ? '' : _track$properties$cat,
_track$properties$lab = _track$properties.label,
eventLabel = _track$properties$lab === void 0 ? '' : _track$properties$lab,
_track$properties$nam = _track$properties.name,
eventName = _track$properties$nam === void 0 ? '' : _track$properties$nam,
_track$properties$val = _track$properties.value,
eventValue = _track$properties$val === void 0 ? '' : _track$properties$val; // Coerce to number if possible
if (eventValue !== '' && !utils.isNumeric(eventValue)) {
throw new TypeError('Please check a valid tracking intent was provided. Property "value" must be a number');
}
var destinationData = {
eventAction: eventAction,
eventCategory: eventCategory,
eventLabel: eventLabel,
eventName: eventName,
eventValue: parseFloat(eventValue) || '',
eventPage: eventPage
};
return destinationData;
}
if (identify) {
var _destinationData = {
user: identify.properties
};
return _destinationData;
}
throw new TypeError('Please check a valid tracking intent was provided');
};
exports.mapTrackingDataToGoogleAnalytics = mapTrackingDataToGoogleAnalytics;
//# sourceMappingURL=mapper.js.map