Skip to content

Commit f308fe9

Browse files
authored
[DEST-757][CC-4858] Add product list viewed event to Monetate (#142)
* Added Product List Viewed event type * Add tests
1 parent f1043f7 commit f308fe9

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

integrations/monetate/HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
3.0.0 / 2019-06-24
3+
==================
4+
5+
* support for product list viewed events as `addProducts`
6+
* change product view Monetate event to `addProductDetails`
7+
28
2.2.0 / 2016-09-06
39
==================
410

integrations/monetate/lib/index.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Monetate.prototype.initialize = function() {
4040
if (this.options.retail) {
4141
this.options.events = {
4242
orderCompleted: 'addPurchaseRows',
43-
productViewed: 'addProducts',
43+
productViewed: 'addProductDetails',
44+
productListViewed: 'addProducts',
4445
productAdded: 'addCartRows'
4546
};
4647
}
@@ -71,6 +72,25 @@ Monetate.prototype.page = function(page) {
7172
push('setPageType', page.category() || page.name() || 'unknown');
7273
};
7374

75+
/**
76+
* Product list viewed.
77+
*
78+
* @param {Track} track
79+
*/
80+
81+
Monetate.prototype.productListViewed = function(track) {
82+
var products = track.products();
83+
var items = [];
84+
85+
each(products, function(product) {
86+
var track = new Track({ properties: product });
87+
var p = toProducts(track);
88+
items.push(p);
89+
});
90+
91+
push(this.options.events.productListViewed, items);
92+
};
93+
7494
/**
7595
* Product viewed.
7696
*
@@ -105,14 +125,29 @@ Monetate.prototype.orderCompleted = function(track) {
105125

106126
each(products, function(product) {
107127
var track = new Track({ properties: product });
108-
product = toProduct(track);
109-
product.conversionId = orderId;
110-
items.push(product);
128+
var p = toProduct(track);
129+
p.conversionId = orderId;
130+
items.push(p);
111131
});
112132

113133
push(this.options.events.orderCompleted, items);
114134
};
115135

136+
/**
137+
* Reformat a product list view into a Monetate-compatible format.
138+
*
139+
* @api private
140+
* @param {Track} track
141+
* @return {Object}
142+
*/
143+
144+
function toProducts(track) {
145+
return {
146+
itemId: track.productId() || track.id(),
147+
sku: track.sku()
148+
};
149+
}
150+
116151
/**
117152
* Reformat a product into to a Monetate-compatible format.
118153
*

integrations/monetate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-monetate",
33
"description": "The Monetate analytics.js integration.",
4-
"version": "2.2.0",
4+
"version": "3.0.0",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/monetate/test/index.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,39 @@ describe('Monetate', function() {
113113
price: 299,
114114
quantity: 8
115115
});
116-
analytics.called(window.monetateQ.push, ['addProducts', ['bb161be4']]);
116+
analytics.called(window.monetateQ.push, [
117+
'addProductDetails',
118+
['bb161be4']
119+
]);
120+
});
121+
122+
it('should track product list viewed', function() {
123+
analytics.track('product list viewed', {
124+
products: [
125+
{
126+
product_id: 'bb161be4',
127+
sku: 'fc0b3bb',
128+
name: 'sony pulse',
129+
price: 299,
130+
quantity: 8
131+
},
132+
{
133+
product_id: 'bb181be4',
134+
sku: 'fc1b3bb',
135+
name: 'sony play',
136+
price: 299,
137+
quantity: 1
138+
}
139+
]
140+
});
141+
var expected = [
142+
'addProducts',
143+
[
144+
{ itemId: 'bb161be4', sku: 'fc0b3bb' },
145+
{ itemId: 'bb181be4', sku: 'fc1b3bb' }
146+
]
147+
];
148+
analytics.called(window.monetateQ.push, expected);
117149
});
118150

119151
it('should track product added', function() {

0 commit comments

Comments
 (0)