@mrporter/inseason-analytics
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -6,4 +6,8 @@ 'use strict'; | ||
}); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
exports.trackEvent = trackEvent; | ||
exports.trackPage = trackPage; | ||
exports.createPageObject = createPageObject; | ||
@@ -14,6 +18,16 @@ var _importAnalytics = require('./importAnalytics'); | ||
var _readDataAttribute = require('./readDataAttribute'); | ||
var _readDataAttributes = require('./readDataAttributes'); | ||
var _readDataAttribute2 = _interopRequireDefault(_readDataAttribute); | ||
var _readDataAttributes2 = _interopRequireDefault(_readDataAttributes); | ||
var _waitForUserProp = require('./waitForUserProp'); | ||
var _waitForUserProp2 = _interopRequireDefault(_waitForUserProp); | ||
var _DataObjectUtils = require('./DataObjectUtils'); | ||
var _querystring = require('querystring'); | ||
var _querystring2 = _interopRequireDefault(_querystring); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -26,2 +40,3 @@ | ||
var analytics = void 0; | ||
if (typeof window !== 'undefined') { | ||
@@ -32,4 +47,18 @@ if (!window.digitalData) { | ||
analytics = (0, _readDataAttribute2.default)('business').then(function (business) { | ||
return (0, _importAnalytics2.default)('http://s3-eu-west-1.amazonaws.com/mrp-web-analytics-dev/' + business + 'analytics-v2.js'); | ||
analytics = (0, _readDataAttributes2.default)().then(function (attributes) { | ||
var business = attributes.business; | ||
if (!business) { | ||
throw new Error('Expected "data-business" attribute in "body" element'); | ||
} | ||
(0, _DataObjectUtils.setPage)(createPageObject(attributes)); | ||
return Promise.all([(0, _importAnalytics2.default)('http://s3-eu-west-1.amazonaws.com/mrp-web-analytics-dev/' + business + 'analytics-v2.js'), (0, _waitForUserProp2.default)()]).then(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 1), | ||
webAnalytics = _ref2[0]; | ||
return webAnalytics; | ||
}); | ||
}).catch(printWarning); | ||
@@ -39,14 +68,16 @@ } | ||
function trackEvent(eventData) { | ||
if (!window.digitalData.event) { | ||
window.digitalData.event = []; | ||
} | ||
window.digitalData.event.push(eventData); | ||
return analytics.then(function (_ref3) { | ||
var trackEvent = _ref3.trackEvent; | ||
if (!production) { | ||
console.info('Analytics: Event', eventData); | ||
} | ||
if (!window.digitalData.event) { | ||
window.digitalData.event = []; | ||
} | ||
return analytics.then(function (_ref) { | ||
var trackEvent = _ref.trackEvent; | ||
return trackEvent(eventData); | ||
window.digitalData.event.push(eventData); | ||
if (!production) { | ||
console.info('Analytics: Event', eventData); | ||
} | ||
trackEvent(eventData); | ||
}).catch(printWarning); | ||
@@ -56,12 +87,13 @@ } | ||
function trackPage() { | ||
var _window$digitalData = window.digitalData, | ||
event = _window$digitalData.event, | ||
data = _objectWithoutProperties(_window$digitalData, ['event']); | ||
return analytics.then(function (_ref4) { | ||
var trackPage = _ref4.trackPage; | ||
if (!production) { | ||
console.info('Analytics: Page View', data); | ||
} | ||
var _window$digitalData = window.digitalData, | ||
event = _window$digitalData.event, | ||
data = _objectWithoutProperties(_window$digitalData, ['event']); | ||
return analytics.then(function (_ref2) { | ||
var trackPage = _ref2.trackPage; | ||
if (!production) { | ||
console.info('Analytics: Page View', data); | ||
} | ||
return trackPage(data); | ||
@@ -71,2 +103,29 @@ }).catch(printWarning); | ||
function createPageObject(_ref5) { | ||
var business = _ref5.business, | ||
language = _ref5.language, | ||
country = _ref5.country, | ||
region = _ref5.region, | ||
currency = _ref5.currency; | ||
var search = window.location.search; | ||
var query = _querystring2.default.parse(search.substr(1)); | ||
return { | ||
pageInfo: { | ||
sysEnv: 'website', | ||
language: language, | ||
geoRegion: country && country.toLowerCase(), | ||
referringURL: document.referrer | ||
}, | ||
attributes: { | ||
businessName: business, | ||
region: region, | ||
currencyCode: currency && currency.toLowerCase(), | ||
externalCampaign: query['cm_mmc'], | ||
internalCampaign: query['cm_sp'] | ||
} | ||
}; | ||
} | ||
function printWarning(error) { | ||
@@ -73,0 +132,0 @@ if (!production) { |
@@ -29,2 +29,6 @@ 'use strict'; | ||
window.digitalData.user = setObject(user, newProps); | ||
var userSetEvent = document.createEvent('Event'); | ||
userSetEvent.initEvent('__analytics__user__set__', true, true); | ||
document.dispatchEvent(userSetEvent); | ||
} | ||
@@ -31,0 +35,0 @@ |
@@ -9,11 +9,14 @@ 'use strict'; | ||
Object.keys(_Analytics).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _Analytics[key]; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'trackEvent', { | ||
enumerable: true, | ||
get: function get() { | ||
return _Analytics.trackEvent; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'trackPage', { | ||
enumerable: true, | ||
get: function get() { | ||
return _Analytics.trackPage; | ||
} | ||
}); | ||
@@ -20,0 +23,0 @@ var _DataObjectUtils = require('./DataObjectUtils'); |
{ | ||
"name": "@mrporter/inseason-analytics", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "In Season Analytics Module", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# In Season Analytics Module | ||
This module aims to provide a single point of interaction for developers implementing analytics in front end applications. | ||
This module aims to provide a single point of interaction for developers implementing analytics in front end applications. There is no need to also include the Web Analytics script. | ||
@@ -13,7 +13,36 @@ ## Setup | ||
When you import this module, it checks behind the scenes that the Web Analytics library has been loaded in, and `NAP.WebAn` is available on the page. If not, it fetches the script. | ||
## TL; DR | ||
To set up the analytics in your application: | ||
- Set the page object according to the spec for your page using `setPage`. This module has already set up some parameters. You need to add: | ||
``` | ||
setPage({ | ||
pageInfo: { | ||
pageName, | ||
environment, | ||
destinationURL, | ||
pageType, | ||
primaryCategory, | ||
subCategory1, | ||
subCategory2 | ||
}, | ||
attributes: { | ||
featuresList // check the page level spec for this, probably not needed | ||
} | ||
}); | ||
``` | ||
- Set any other objects according to the spec for your page, e.g. use `setProduct` ([spec](#setproductnewprops)) on the product page, `setTransaction` on the order confirmation page, `setWishlist` on the wishlist page. The `user` object has been set by site furniture, if you need to modify it at any point you can use `setUser`. | ||
- Call `trackPage` after these have been set. | ||
- Use `trackEvent` to track any events on your page according to the spec. | ||
- If you're building a one page app and need to track new pages, you can use the provided functions to make any changes to the digital data object, and then call trackPage again. | ||
## Event and Page Tracking | ||
There are two tracking functions provided. These functions can be used safely before the the Web Analytics library is available, as calls are stored in a queue and passed to the Web Analytics functions when they become available. | ||
There are two tracking functions provided. Various steps have been taken to avoid race conditions being an issue (e.g. between your app and site furniture), so just import these and use as necessary: | ||
@@ -67,7 +96,7 @@ ### trackEvent(eventData) | ||
Site Furniture sets some of the `user` and `page` data where it is consistent. The following methods merge more data into the digital data object, without replacing the values set by site furniture (unless you explicitly overwrite a value). In each case, `newProps` is an object containing the data that you wish to add to the digital data object. | ||
This module sets some of the `page` data where it is consistent, and Site Furniture sets the `user` data. The following methods merge more data into the digital data object, without replacing these initial values (unless you explicitly overwrite a value). In each case, `newProps` is an object containing the data that you wish to add to the digital data object. | ||
### setPage(newProps) | ||
The specification for the `page` object can be found [here](https://confluence.nap/pages/viewpage.action?pageId=18617632). | ||
Site Furniture will have set the following properties: | ||
This module will have set the following properties: | ||
@@ -77,3 +106,2 @@ - `pageInfo.sysEnv` | ||
- `pageInfo.geoRegion` | ||
- `pageInfo.destinationURL` | ||
- `pageInfo.referringURL` | ||
@@ -93,2 +121,3 @@ - `attributes.businessName` | ||
environment, | ||
destinationURL, | ||
pageType, | ||
@@ -167,3 +196,3 @@ primaryCategory, | ||
The following three objects contain an array called `item`, which are all of the items within that cart, order confirmation page, or wishlist repsectively. Passing a new item array into this object using these methods will overwrite a previous array, rather than merging with it. | ||
The following three objects contain an array called `item`, which are all of the items within that cart, order confirmation page, or wishlist respectively. Passing a new item array into this object using these methods will overwrite a previous array, rather than merging with it. | ||
@@ -170,0 +199,0 @@ ### setCart(newProps) |
17862
10
252
204