mobify-analytics-sdk
Version:
A unified way to setup Mobify analytics
222 lines (200 loc) • 6.14 kB
JavaScript
// The main schame defintion for our Analytics SDK
// see http://json-schema.org/
var captureItem = {
'type': 'object',
'properties': {
'url': {'type': 'string'},
's': {
'anyOf': [
{'type': 'string'},
{
'type': 'array',
'items': {'type': 'string'}
}
]
},
'l': {'type': 'string'}
},
'required': ['s'],
'additionalProperties': false
};
var urlRegexExtractors = {
'anyOf': [
{ 'type': 'string' },
captureItem,
{
'type': 'array',
'items': captureItem
},
{
'type': 'array',
'items': { 'type': 'string' }
}
]
};
var getSpecification = function(allRequired, additionalDimensions) {
var config = {
'type': 'object',
'properties': {
'action': { 'type': 'string' },
'selectors': urlRegexExtractors,
'lifeCycle': {
'type': 'string',
'enum': [
'ASAP', 'DCL', 'untilSeen', 'monitor'
]
},
// 'dimensions': null, // filled in below
},
'additionalProperties': false
};
if (allRequired) {
config.required = ['action', 'selectors'];
}
if (additionalDimensions) {
var dimConfig = {
'type': 'object',
'properties': {},
'additionalProperties': false
};
additionalDimensions.forEach(function(dimensionName) {
dimConfig.properties[dimensionName] = urlRegexExtractors;
});
config.properties.dimensions = dimConfig;
}
var disableableConfig = {
'anyOf': [
config,
{
'type': 'object',
'properties': {
'disable': {'type': 'boolean' }
},
'additionalProperties': false
},
]
};
return disableableConfig;
};
var getCaptureSchema = function(additionalDimensions) {
var config = {
'type': 'object',
'properties': {
'default': getSpecification(true, additionalDimensions),
'components': {
'type': 'object',
'properties': {
'PWA': getSpecification(false, additionalDimensions),
'Adaptive': getSpecification(false, additionalDimensions),
'Astro': getSpecification(false, additionalDimensions)
},
'additionalProperties': false
}
},
'required': ['default'],
'additionalProperties': false
};
return config;
};
var getSingleValueDimensionCapture = function() {
return {
'type': 'object',
'properties': {
'default': urlRegexExtractors,
'components': {
'type': 'object',
'properties': {
'PWA': urlRegexExtractors,
'Adaptive': urlRegexExtractors,
'Astro': urlRegexExtractors
},
'additionalProperties': false
}
},
'required': ['default'],
'additionalProperties': false
};
};
var getMultiValueDimensionCapture = function(multipleDimensions) {
var config = {
'type': 'object',
'properties': {},
'additionalProperties': false
};
multipleDimensions.forEach(function(dimValue) {
config.properties[dimValue] = getSingleValueDimensionCapture();
});
return config;
};
/**
* @name Analytics SDK config
*
* @description
* What is a config file?
A config file is a declarative structure which allows you to specify how you would like to instrument a site.
We divide our capturing into 2 parts:
1. `Events` - these are action events that are captured (e.g. click on something)
2. `Dimensions` - these are dimensions that are sent along with events (e.g. category of the page)
Below we will further explain each of the section.
* ## Valid events
*
* Currently we have following events available:
*
* ### Add to cart
*
* When client add items to cart
*
* * Event key: `addToCart`
* * Additional properties: `cart_items`
*
* ### Search
*
* When client search
*
* * Event key: `search`
* * Additional properties: `search`
*
*
* ## Valid dimensions
*
* These are meta properties that can be included in events
*
* * `funnel_step`
* * `product_category`
*
*/
exports.SDKschema = {
'title': 'Analytics SDK config',
'type': 'object',
'properties': {
'events': {
'type': 'object',
'properties': {
// Note that we use snake_case here since this data is passed as
// dimension to Sandy which uses camel case for key
'addToCart': getCaptureSchema(['cart_items']),
'removeFromCart': getCaptureSchema(['cart_items']),
'search': getCaptureSchema(['search']),
'formInteraction': getCaptureSchema(['container_name', 'content']),
},
'additionalProperties': false
},
'dimensions': {
'type': 'object',
'properties': {
'funnel_step': getMultiValueDimensionCapture([
// These are steps that are suggested by Insights team (@kdr)
'home', 'search', 'plp', 'pdp', 'cart', 'checkout',
'category', 'confirmation', 'storeLocator', 'account'
]),
'product_id': getSingleValueDimensionCapture(),
'product_name': getSingleValueDimensionCapture(),
'product_stock': getSingleValueDimensionCapture(),
'product_category': getSingleValueDimensionCapture(),
'product_price': getSingleValueDimensionCapture()
},
'additionalProperties': false
},
},
'additionalProperties': false
};