Repository URL to install this package:
|
Version:
2.0.2 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var constants = require('../constants.js');
var utils = require('../helpers/utils.js');
var _excluded = ["options"];
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, in case of declarative tracking
* @return {{href: string, isSameTabLink: boolean}} - A representation of the element's click behavior
*/
var getTrackingBehavior = function getTrackingBehavior(el) {
var href = el && el.getAttribute('href');
var target = el && el.getAttribute('target');
var trackingBehavior = {
href: href,
isSameTabLink: Boolean(href ? target !== '_blank' : true)
};
return trackingBehavior;
};
/**
* Enriches a user provided tracking intent data with additional information
*
* @param {Object.<string, TrackingIntentTrack|TrackingIntentIdentify|TrackingIntentPage>} trackingIntentData - An object that holds the data of a tracking intent
* @param {TrackingApiOptions} [options] - The API client's options
* @return {TrackingIntentTrack|TrackingIntentIdentify|TrackingIntentPage} - An enriched tracking intent data
*/
var getEnrichedTrackingData = function getEnrichedTrackingData(trackingIntentData, options) {
if (trackingIntentData.identify && trackingIntentData.identify.userId && !options._userId) {
options._userId = trackingIntentData.identify.userId;
}
var track = trackingIntentData.track,
identify = trackingIntentData.identify,
page = trackingIntentData.page;
var enrichedTrackingData = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, track && {
track: _rollupPluginBabelHelpers.objectSpread2({
userId: options._userId
}, track)
}), identify && {
identify: _rollupPluginBabelHelpers.objectSpread2({
userId: options._userId
}, identify)
}), page && {
page: _rollupPluginBabelHelpers.objectSpread2({
userId: options._userId
}, page)
});
return enrichedTrackingData;
};
/**
* Enriches a user provided tracking intent options with additional information
*
* @param {TrackingIntentOptions} trackingIntentOptions - An object that holds the options of a tracking intent
* @param {?HTMLElement} trackingEl - An HTML element with tracking data attributes
* @param {TrackingApiOptions} [options] - The API client's options
* @param {string[]} [consents] - The currently active consents.
* @return {TrackingDefinitionOptions} - An enriched tracking intent options
*/
var getEnrichedTrackingOptions = function getEnrichedTrackingOptions(trackingIntentOptions, trackingEl) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var consents = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
// 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,
avo = options.avo,
avoInspector = options.avoInspector,
amplitude = options.amplitude;
var enrichedTrackingOptions = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({
clientId: clientId,
avo: avo,
avoInspector: avoInspector,
amplitude: amplitude,
env: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, doodleEnv && {
doodleEnv: doodleEnv
}), nodeEnv && {
nodeEnv: nodeEnv
}), svcDataLayerApi && {
svcDataLayerApi: svcDataLayerApi
})
}, trackingIntentOptions), {}, {
// Define services by overwriting defaults with custom that may be provided in the tracking intent
services: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, defaultServices), trackingIntentOptions.services),
consents: consents
}, getTrackingBehavior(trackingEl));
return enrichedTrackingOptions;
};
/**
* Enriches a trackingIntent with defaults (e.g. destination services and env vars as well as currently active consents) 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
* @param {string[]} [consents] - The currently active consents.
* @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 consents = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
var hasTrackingIntent = !utils.isEmptyObject(trackingIntent);
if (!(hasTrackingIntent || trackingEl)) {
throw new TypeError('Please provide either a trackingIntent or a trackingEl');
} // 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 (if any) and tracking data
var _foundTrackingIntent$ = foundTrackingIntent.options,
intentTrackingOptions = _foundTrackingIntent$ === void 0 ? {} : _foundTrackingIntent$,
intentTrackingData = _rollupPluginBabelHelpers.objectWithoutProperties(foundTrackingIntent, _excluded);
var trackingDefinition = {
trackingData: getEnrichedTrackingData(intentTrackingData, options),
trackingOptions: getEnrichedTrackingOptions(intentTrackingOptions, trackingEl, options, consents)
};
return trackingDefinition;
};
/**
* Returns an object with tracking-related data attributes to be spread over a JSX element
*
* @param {TrackingIntent} trackingIntent An object that holds the values that should be tracked
* @return {Object} An object with data attributes as keys
*/
var getTrackingDataAttrs = function getTrackingDataAttrs(_ref2) {
var track = _ref2.track,
identify = _ref2.identify,
page = _ref2.page,
_ref2$options = _ref2.options;
_ref2$options = _ref2$options === void 0 ? {} : _ref2$options;
var services = _ref2$options.services,
setOnce = _ref2$options.setOnce,
autoTracking = _ref2$options.autoTracking;
var trackingOptions = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, !utils.isUndefined(services) && {
services: services
}), !utils.isUndefined(setOnce) && {
setOnce: setOnce
}), !utils.isUndefined(autoTracking) && {
autoTracking: autoTracking
});
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, !utils.isEmptyObject(trackingOptions) && {
'data-tracking-options': JSON.stringify(trackingOptions)
}), track && {
'data-track': JSON.stringify(track)
}), identify && {
'data-identify': JSON.stringify(identify)
}), page && {
'data-page': JSON.stringify(page)
});
};
exports.getEnrichedTrackingData = getEnrichedTrackingData;
exports.getEnrichedTrackingOptions = getEnrichedTrackingOptions;
exports.getTrackingBehavior = getTrackingBehavior;
exports.getTrackingDataAttrs = getTrackingDataAttrs;
exports.getTrackingDefinition = getTrackingDefinition;
exports.getTrackingIntentFromDOM = getTrackingIntentFromDOM;
//# sourceMappingURL=getters.js.map