Skip to content

Commit 1b57069

Browse files
authored
[Amplitude] Update SDK to 4.4.0 (#68)
* [Amplitude] Update SDK to 4.4.0 * make snippet one line * make snippet one line (take 2) * fix lockfile * up version number
1 parent 36f524f commit 1b57069

File tree

3 files changed

+332
-174
lines changed

3 files changed

+332
-174
lines changed

integrations/amplitude/lib/index.js

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ var umd = typeof window.define === 'function' && window.define.amd;
2222
* Source.
2323
*/
2424

25-
var src = '//d24n15hnbwhuhn.cloudfront.net/libs/amplitude-4.1.1-min.gz.js';
25+
var src = 'https://cdn.amplitude.com/libs/amplitude-4.4.0-min.gz.js';
2626

2727
/**
2828
* Expose `Amplitude` integration.
2929
*/
3030

31-
var Amplitude = module.exports = integration('Amplitude')
31+
var Amplitude = (module.exports = integration('Amplitude')
3232
.global('amplitude')
3333
.option('apiKey', '')
3434
.option('trackAllPages', false)
@@ -47,7 +47,7 @@ var Amplitude = module.exports = integration('Amplitude')
4747
.option('mapQueryParams', {})
4848
.option('trackRevenuePerProduct', false)
4949
.option('preferAnonymousIdForDeviceId', false)
50-
.tag('<script src="' + src + '">');
50+
.tag('<script src="' + src + '">'));
5151

5252
/**
5353
* Initialize.
@@ -58,20 +58,23 @@ var Amplitude = module.exports = integration('Amplitude')
5858
*/
5959

6060
Amplitude.prototype.initialize = function() {
61+
// amplitude snippet (lines loading amplitude cdn-served script are removed as that is already achieved via Segment tag and load methods)
6162
/* eslint-disable */
62-
(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};function r(e,t){e.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}var i=function(){this._q=[];return this};var s=["add","append","clearAll","prepend","set","setOnce","unset"];for(var o=0;o<s.length;o++){r(i,s[o])}n.Identify=i;var a=function(){this._q=[];return this};var u=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"];for(var c=0;c<u.length;c++){r(a,u[c])}n.Revenue=a;var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId"];function p(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}for(var n=0;n<l.length;n++){t(l[n])}}p(n);n.getInstance=function(e){e=(!e||e.length===0?"$default_instance":e).toLowerCase();if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};p(n._iq[e])}return n._iq[e]};e.amplitude=n})(window,document);
63+
(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};;function s(e,t){e.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}var o=function(){this._q=[];return this};var a=["add","append","clearAll","prepend","set","setOnce","unset"];for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[];return this};var l=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"];for(var p=0;p<l.length;p++){s(c,l[p])}n.Revenue=c;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"];function v(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){e=(!e||e.length===0?"$default_instance":e).toLowerCase();if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};v(n._iq[e])}return n._iq[e]};e.amplitude=n})(window,document);
6364
/* eslint-enable */
6465

6566
this.setDomain(window.location.href);
66-
window.amplitude.init(this.options.apiKey, null, {
67+
68+
window.amplitude.getInstance().init(this.options.apiKey, null, {
6769
includeUtm: this.options.trackUtmProperties,
6870
includeReferrer: this.options.trackReferrer,
6971
batchEvents: this.options.batchEvents,
7072
eventUploadThreshold: this.options.eventUploadThreshold,
7173
eventUploadPeriodMillis: this.options.eventUploadPeriodMillis,
7274
forceHttps: this.options.forceHttps,
7375
includeGclid: this.options.trackGclid,
74-
saveParamsReferrerOncePerSession: this.options.saveParamsReferrerOncePerSession,
76+
saveParamsReferrerOncePerSession: this.options
77+
.saveParamsReferrerOncePerSession,
7578
deviceIdFromUrlParam: this.options.deviceIdFromUrlParam
7679
});
7780

@@ -91,10 +94,12 @@ Amplitude.prototype.initialize = function() {
9194
}
9295

9396
this.load(function() {
94-
when(loaded, function() {
97+
if (window.amplitude.runQueuedFunctions) {
9598
window.amplitude.runQueuedFunctions();
9699
ready();
97-
});
100+
} else {
101+
console.log('[Amplitude] Error: could not load SDK');
102+
}
98103
});
99104
};
100105

@@ -106,7 +111,7 @@ Amplitude.prototype.initialize = function() {
106111
*/
107112

108113
Amplitude.prototype.loaded = function() {
109-
return !!(window.amplitude && window.amplitude.options);
114+
return !!(window.amplitude && window.amplitude.getInstance().options);
110115
};
111116

112117
/**
@@ -151,7 +156,7 @@ Amplitude.prototype.identify = function(identify) {
151156

152157
var id = identify.userId();
153158
var traits = identify.traits();
154-
if (id) window.amplitude.setUserId(id);
159+
if (id) window.amplitude.getInstance().setUserId(id);
155160
if (traits) {
156161
// map query params from context url if opted in
157162
var mapQueryParams = this.options.mapQueryParams;
@@ -164,14 +169,15 @@ Amplitude.prototype.identify = function(identify) {
164169
}, mapQueryParams);
165170
}
166171

167-
window.amplitude.setUserProperties(traits);
172+
window.amplitude.getInstance().setUserProperties(traits);
168173
}
169174

170175
// Set user groups: https://amplitude.zendesk.com/hc/en-us/articles/115001361248#setting-user-groups
171176
var groups = identify.options(this.name).groups;
172177
if (groups && is.object(groups)) {
173178
for (var group in groups) {
174-
if (groups.hasOwnProperty(group)) window.amplitude.setGroup(group, groups[group]);
179+
if (groups.hasOwnProperty(group))
180+
window.amplitude.getInstance().setGroup(group, groups[group]);
175181
}
176182
}
177183
};
@@ -197,28 +203,32 @@ function logEvent(track, dontSetRevenue) {
197203
if (!is.empty(mapQueryParams)) {
198204
var params = {};
199205
var type;
200-
// since we accept any arbitrary property name and we dont have conditional UI components
201-
// in the app where we can limit users to only add a single mapping, so excuse the temporary jank
206+
// since we accept any arbitrary property name and we dont have conditional UI components
207+
// in the app where we can limit users to only add a single mapping, so excuse the temporary jank
202208
each(function(value, key) {
203209
// add query params to either `user_properties` or `event_properties`
204210
type = value;
205-
type === 'user_properties' ? params[key] = query : props[key] = query;
211+
type === 'user_properties' ? (params[key] = query) : (props[key] = query);
206212
}, mapQueryParams);
207213

208-
if (type === 'user_properties') window.amplitude.setUserProperties(params);
214+
if (type === 'user_properties')
215+
window.amplitude.getInstance().setUserProperties(params);
209216
}
210217

211218
// track the event
212219
if (options.groups) {
213-
window.amplitude.logEventWithGroups(event, props, options.groups);
220+
window.amplitude
221+
.getInstance()
222+
.logEventWithGroups(event, props, options.groups);
214223
} else {
215-
window.amplitude.logEvent(event, props);
224+
window.amplitude.getInstance().logEvent(event, props);
216225
}
217226

218227
// Ideally, user's will track revenue using an Order Completed event.
219228
// However, we have previously setRevenue for any event given it had a revenue property.
220229
// We need to keep this behavior around for backwards compatibility.
221-
if (track.revenue() && !dontSetRevenue) this.setRevenue(mapRevenueAttributes(track));
230+
if (track.revenue() && !dontSetRevenue)
231+
this.setRevenue(mapRevenueAttributes(track));
222232
}
223233

224234
Amplitude.prototype.orderCompleted = function(track) {
@@ -242,20 +252,23 @@ Amplitude.prototype.orderCompleted = function(track) {
242252
logEvent.call(this, new Track(clonedTrack), trackRevenuePerProduct);
243253

244254
// Loop through products array.
245-
each(function(product) {
246-
var price = product.price;
247-
var quantity = product.quantity;
248-
clonedTrack.properties = product;
249-
clonedTrack.event = 'Product Purchased';
250-
// Price and quantity are both required by Amplitude:
251-
// https://amplitude.zendesk.com/hc/en-us/articles/115001361248#tracking-revenue
252-
// Price could potentially be 0 so handle that edge case.
253-
if (trackRevenuePerProduct && price != null && quantity) this.setRevenue(mapRevenueAttributes(new Track(clonedTrack)));
254-
logEvent.call(this, new Track(clonedTrack), trackRevenuePerProduct);
255-
}.bind(this), products);
255+
each(
256+
function(product) {
257+
var price = product.price;
258+
var quantity = product.quantity;
259+
clonedTrack.properties = product;
260+
clonedTrack.event = 'Product Purchased';
261+
// Price and quantity are both required by Amplitude:
262+
// https://amplitude.zendesk.com/hc/en-us/articles/115001361248#tracking-revenue
263+
// Price could potentially be 0 so handle that edge case.
264+
if (trackRevenuePerProduct && price != null && quantity)
265+
this.setRevenue(mapRevenueAttributes(new Track(clonedTrack)));
266+
logEvent.call(this, new Track(clonedTrack), trackRevenuePerProduct);
267+
}.bind(this),
268+
products
269+
);
256270
};
257271

258-
259272
/**
260273
* Group.
261274
*
@@ -269,11 +282,11 @@ Amplitude.prototype.group = function(group) {
269282
var groupType = group.traits()[this.options.groupTypeTrait];
270283
var groupValue = group.traits()[this.options.groupValueTrait];
271284
if (groupType && groupValue) {
272-
window.amplitude.setGroup(groupType, groupValue);
285+
window.amplitude.getInstance().setGroup(groupType, groupValue);
273286
} else {
274287
var groupId = group.groupId();
275288
if (groupId) {
276-
window.amplitude.setGroup('[Segment] Group', groupId);
289+
window.amplitude.getInstance().setGroup('[Segment] Group', groupId);
277290
}
278291
}
279292
};
@@ -314,7 +327,7 @@ Amplitude.prototype.setDeviceIdFromAnonymousId = function(facade) {
314327
*/
315328

316329
Amplitude.prototype.setDeviceId = function(deviceId) {
317-
if (deviceId) window.amplitude.setDeviceId(deviceId);
330+
if (deviceId) window.amplitude.getInstance().setDeviceId(deviceId);
318331
};
319332

320333
Amplitude.prototype.setRevenue = function(properties) {
@@ -335,17 +348,19 @@ Amplitude.prototype.setRevenue = function(properties) {
335348
}
336349

337350
var ampRevenue = new window.amplitude.Revenue()
338-
.setPrice(price)
339-
.setQuantity(quantity)
340-
.setEventProperties(eventProps);
351+
.setPrice(price)
352+
.setQuantity(quantity)
353+
.setEventProperties(eventProps);
341354

342355
if (revenueType) ampRevenue.setRevenueType(revenueType);
343356

344357
if (productId) ampRevenue.setProductId(productId);
345358

346-
window.amplitude.logRevenueV2(ampRevenue);
359+
window.amplitude.getInstance().logRevenueV2(ampRevenue);
347360
} else {
348-
window.amplitude.logRevenue(revenue || price * quantity, quantity, productId);
361+
window.amplitude
362+
.getInstance()
363+
.logRevenue(revenue || price * quantity, quantity, productId);
349364
}
350365
};
351366

@@ -360,7 +375,9 @@ function mapRevenueAttributes(track) {
360375
return {
361376
price: track.price(),
362377
productId: track.productId(),
363-
revenueType: track.proxy('properties.revenueType') || mapRevenueType[track.event().toLowerCase()],
378+
revenueType:
379+
track.proxy('properties.revenueType') ||
380+
mapRevenueType[track.event().toLowerCase()],
364381
quantity: track.quantity(),
365382
eventProps: track.properties(),
366383
revenue: track.revenue()

integrations/amplitude/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-amplitude",
33
"description": "The Amplitude analytics.js integration.",
4-
"version": "2.8.0",
4+
"version": "2.9.0",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

0 commit comments

Comments
 (0)