@@ -22,13 +22,13 @@ var umd = typeof window.define === 'function' && window.define.amd;
22
22
* Source.
23
23
*/
24
24
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' ;
26
26
27
27
/**
28
28
* Expose `Amplitude` integration.
29
29
*/
30
30
31
- var Amplitude = module . exports = integration ( 'Amplitude' )
31
+ var Amplitude = ( module . exports = integration ( 'Amplitude' )
32
32
. global ( 'amplitude' )
33
33
. option ( 'apiKey' , '' )
34
34
. option ( 'trackAllPages' , false )
@@ -47,7 +47,7 @@ var Amplitude = module.exports = integration('Amplitude')
47
47
. option ( 'mapQueryParams' , { } )
48
48
. option ( 'trackRevenuePerProduct' , false )
49
49
. option ( 'preferAnonymousIdForDeviceId' , false )
50
- . tag ( '<script src="' + src + '">' ) ;
50
+ . tag ( '<script src="' + src + '">' ) ) ;
51
51
52
52
/**
53
53
* Initialize.
@@ -58,20 +58,23 @@ var Amplitude = module.exports = integration('Amplitude')
58
58
*/
59
59
60
60
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)
61
62
/* 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 ) ;
63
64
/* eslint-enable */
64
65
65
66
this . setDomain ( window . location . href ) ;
66
- window . amplitude . init ( this . options . apiKey , null , {
67
+
68
+ window . amplitude . getInstance ( ) . init ( this . options . apiKey , null , {
67
69
includeUtm : this . options . trackUtmProperties ,
68
70
includeReferrer : this . options . trackReferrer ,
69
71
batchEvents : this . options . batchEvents ,
70
72
eventUploadThreshold : this . options . eventUploadThreshold ,
71
73
eventUploadPeriodMillis : this . options . eventUploadPeriodMillis ,
72
74
forceHttps : this . options . forceHttps ,
73
75
includeGclid : this . options . trackGclid ,
74
- saveParamsReferrerOncePerSession : this . options . saveParamsReferrerOncePerSession ,
76
+ saveParamsReferrerOncePerSession : this . options
77
+ . saveParamsReferrerOncePerSession ,
75
78
deviceIdFromUrlParam : this . options . deviceIdFromUrlParam
76
79
} ) ;
77
80
@@ -91,10 +94,12 @@ Amplitude.prototype.initialize = function() {
91
94
}
92
95
93
96
this . load ( function ( ) {
94
- when ( loaded , function ( ) {
97
+ if ( window . amplitude . runQueuedFunctions ) {
95
98
window . amplitude . runQueuedFunctions ( ) ;
96
99
ready ( ) ;
97
- } ) ;
100
+ } else {
101
+ console . log ( '[Amplitude] Error: could not load SDK' ) ;
102
+ }
98
103
} ) ;
99
104
} ;
100
105
@@ -106,7 +111,7 @@ Amplitude.prototype.initialize = function() {
106
111
*/
107
112
108
113
Amplitude . prototype . loaded = function ( ) {
109
- return ! ! ( window . amplitude && window . amplitude . options ) ;
114
+ return ! ! ( window . amplitude && window . amplitude . getInstance ( ) . options ) ;
110
115
} ;
111
116
112
117
/**
@@ -151,7 +156,7 @@ Amplitude.prototype.identify = function(identify) {
151
156
152
157
var id = identify . userId ( ) ;
153
158
var traits = identify . traits ( ) ;
154
- if ( id ) window . amplitude . setUserId ( id ) ;
159
+ if ( id ) window . amplitude . getInstance ( ) . setUserId ( id ) ;
155
160
if ( traits ) {
156
161
// map query params from context url if opted in
157
162
var mapQueryParams = this . options . mapQueryParams ;
@@ -164,14 +169,15 @@ Amplitude.prototype.identify = function(identify) {
164
169
} , mapQueryParams ) ;
165
170
}
166
171
167
- window . amplitude . setUserProperties ( traits ) ;
172
+ window . amplitude . getInstance ( ) . setUserProperties ( traits ) ;
168
173
}
169
174
170
175
// Set user groups: https://amplitude.zendesk.com/hc/en-us/articles/115001361248#setting-user-groups
171
176
var groups = identify . options ( this . name ) . groups ;
172
177
if ( groups && is . object ( groups ) ) {
173
178
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 ] ) ;
175
181
}
176
182
}
177
183
} ;
@@ -197,28 +203,32 @@ function logEvent(track, dontSetRevenue) {
197
203
if ( ! is . empty ( mapQueryParams ) ) {
198
204
var params = { } ;
199
205
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
202
208
each ( function ( value , key ) {
203
209
// add query params to either `user_properties` or `event_properties`
204
210
type = value ;
205
- type === 'user_properties' ? params [ key ] = query : props [ key ] = query ;
211
+ type === 'user_properties' ? ( params [ key ] = query ) : ( props [ key ] = query ) ;
206
212
} , mapQueryParams ) ;
207
213
208
- if ( type === 'user_properties' ) window . amplitude . setUserProperties ( params ) ;
214
+ if ( type === 'user_properties' )
215
+ window . amplitude . getInstance ( ) . setUserProperties ( params ) ;
209
216
}
210
217
211
218
// track the event
212
219
if ( options . groups ) {
213
- window . amplitude . logEventWithGroups ( event , props , options . groups ) ;
220
+ window . amplitude
221
+ . getInstance ( )
222
+ . logEventWithGroups ( event , props , options . groups ) ;
214
223
} else {
215
- window . amplitude . logEvent ( event , props ) ;
224
+ window . amplitude . getInstance ( ) . logEvent ( event , props ) ;
216
225
}
217
226
218
227
// Ideally, user's will track revenue using an Order Completed event.
219
228
// However, we have previously setRevenue for any event given it had a revenue property.
220
229
// 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 ) ) ;
222
232
}
223
233
224
234
Amplitude . prototype . orderCompleted = function ( track ) {
@@ -242,20 +252,23 @@ Amplitude.prototype.orderCompleted = function(track) {
242
252
logEvent . call ( this , new Track ( clonedTrack ) , trackRevenuePerProduct ) ;
243
253
244
254
// 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
+ ) ;
256
270
} ;
257
271
258
-
259
272
/**
260
273
* Group.
261
274
*
@@ -269,11 +282,11 @@ Amplitude.prototype.group = function(group) {
269
282
var groupType = group . traits ( ) [ this . options . groupTypeTrait ] ;
270
283
var groupValue = group . traits ( ) [ this . options . groupValueTrait ] ;
271
284
if ( groupType && groupValue ) {
272
- window . amplitude . setGroup ( groupType , groupValue ) ;
285
+ window . amplitude . getInstance ( ) . setGroup ( groupType , groupValue ) ;
273
286
} else {
274
287
var groupId = group . groupId ( ) ;
275
288
if ( groupId ) {
276
- window . amplitude . setGroup ( '[Segment] Group' , groupId ) ;
289
+ window . amplitude . getInstance ( ) . setGroup ( '[Segment] Group' , groupId ) ;
277
290
}
278
291
}
279
292
} ;
@@ -314,7 +327,7 @@ Amplitude.prototype.setDeviceIdFromAnonymousId = function(facade) {
314
327
*/
315
328
316
329
Amplitude . prototype . setDeviceId = function ( deviceId ) {
317
- if ( deviceId ) window . amplitude . setDeviceId ( deviceId ) ;
330
+ if ( deviceId ) window . amplitude . getInstance ( ) . setDeviceId ( deviceId ) ;
318
331
} ;
319
332
320
333
Amplitude . prototype . setRevenue = function ( properties ) {
@@ -335,17 +348,19 @@ Amplitude.prototype.setRevenue = function(properties) {
335
348
}
336
349
337
350
var ampRevenue = new window . amplitude . Revenue ( )
338
- . setPrice ( price )
339
- . setQuantity ( quantity )
340
- . setEventProperties ( eventProps ) ;
351
+ . setPrice ( price )
352
+ . setQuantity ( quantity )
353
+ . setEventProperties ( eventProps ) ;
341
354
342
355
if ( revenueType ) ampRevenue . setRevenueType ( revenueType ) ;
343
356
344
357
if ( productId ) ampRevenue . setProductId ( productId ) ;
345
358
346
- window . amplitude . logRevenueV2 ( ampRevenue ) ;
359
+ window . amplitude . getInstance ( ) . logRevenueV2 ( ampRevenue ) ;
347
360
} else {
348
- window . amplitude . logRevenue ( revenue || price * quantity , quantity , productId ) ;
361
+ window . amplitude
362
+ . getInstance ( )
363
+ . logRevenue ( revenue || price * quantity , quantity , productId ) ;
349
364
}
350
365
} ;
351
366
@@ -360,7 +375,9 @@ function mapRevenueAttributes(track) {
360
375
return {
361
376
price : track . price ( ) ,
362
377
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 ( ) ] ,
364
381
quantity : track . quantity ( ) ,
365
382
eventProps : track . properties ( ) ,
366
383
revenue : track . revenue ( )
0 commit comments