Skip to content

Commit 3b35938

Browse files
committed
Merge pull request segment-integrations#5 from segment-integrations/revenue-fallback
update properties on trackEvent and revenue fallback
2 parents 40d3cce + 50c4254 commit 3b35938

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

lib/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,22 @@ Optimizely.prototype.initialize = function() {
5353
};
5454

5555
/**
56-
* Track.
56+
* Track. trackEvent can only send one property, revenue
57+
* We fall back to total if it's on the call and revenue is not
5758
*
5859
* https://www.optimizely.com/docs/api#track-event
60+
* http://developers.optimizely.com/javascript/reference/#api-function-calls
5961
*
6062
* @api public
6163
* @param {Track} track
6264
*/
6365

6466
Optimizely.prototype.track = function(track) {
6567
var props = track.properties();
66-
if (props.revenue) props.revenue *= 100;
67-
push('trackEvent', track.event(), props);
68+
var payload = {};
69+
var revenue = props.revenue || props.total || props.value;
70+
if (revenue) payload.revenue = revenue *= 100;
71+
push('trackEvent', track.event(), payload);
6872
};
6973

7074
/**

test/index.test.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,9 @@ describe('Optimizely', function() {
161161
analytics.called(window.optimizely.push, ['trackEvent', 'event', {}]);
162162
});
163163

164-
it('should send an event and properties', function() {
164+
it('shouldn\'t send properties it can\'t process', function() {
165165
analytics.track('event', { property: true });
166-
analytics.called(window.optimizely.push, ['trackEvent', 'event', {
167-
property: true
168-
}]);
166+
analytics.called(window.optimizely.push, ['trackEvent', 'event', {}]);
169167
});
170168

171169
it('should change revenue to cents', function() {
@@ -174,6 +172,20 @@ describe('Optimizely', function() {
174172
revenue: 999
175173
}]);
176174
});
175+
176+
it('should fallback to total if revenue isn\'t on the call', function() {
177+
analytics.track('event', { total: 9.99 });
178+
analytics.called(window.optimizely.push, ['trackEvent', 'event', {
179+
revenue: 999
180+
}]);
181+
});
182+
183+
it('should fallback to value if revenue and total aren\'t on the call', function() {
184+
analytics.track('event', { value: 9.99 });
185+
analytics.called(window.optimizely.push, ['trackEvent', 'event', {
186+
revenue: 999
187+
}]);
188+
});
177189
});
178190

179191
describe('#page', function() {
@@ -183,27 +195,12 @@ describe('Optimizely', function() {
183195

184196
it('should send an event for a named page', function() {
185197
analytics.page('Home');
186-
analytics.called(window.optimizely.push, ['trackEvent', 'Viewed Home Page', {
187-
name: 'Home',
188-
path: window.location.pathname,
189-
referrer: document.referrer,
190-
title: document.title,
191-
search: window.location.search,
192-
url: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname + window.location.search
193-
}]);
198+
analytics.called(window.optimizely.push, ['trackEvent', 'Viewed Home Page', {}]);
194199
});
195200

196201
it('should send an event for a named and categorized page', function() {
197202
analytics.page('Blog', 'New Integration');
198-
analytics.called(window.optimizely.push, ['trackEvent', 'Viewed Blog New Integration Page', {
199-
category: 'Blog',
200-
name: 'New Integration',
201-
path: window.location.pathname,
202-
referrer: document.referrer,
203-
title: document.title,
204-
search: window.location.search,
205-
url: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname + window.location.search
206-
}]);
203+
analytics.called(window.optimizely.push, ['trackEvent', 'Viewed Blog New Integration Page', {}]);
207204
});
208205
});
209206
});

0 commit comments

Comments
 (0)