@@ -68,8 +68,9 @@ function Analytics() {
68
68
this . log = debug ( 'analytics.js' ) ;
69
69
bindAll ( this ) ;
70
70
71
- var self = this ;
72
- this . on ( 'initialize' , function ( settings , options ) {
71
+
72
+ const self = this ;
73
+ this . on ( 'initialize' , function ( _ , options ) {
73
74
if ( options . initialPageview ) self . page ( ) ;
74
75
self . _parseQuery ( window . location . search ) ;
75
76
} ) ;
@@ -168,13 +169,16 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
168
169
169
170
// clean unknown integrations from settings
170
171
var self = this ;
171
- each ( function ( _opts : unknown , name : string | number ) {
172
- var Integration = self . Integrations [ name ] ;
173
- if ( ! Integration ) delete settings [ name ] ;
174
- } , settings ) ;
172
+ Object . keys ( settings ) . forEach ( key => {
173
+ var Integration = self . Integrations [ key ] ;
174
+ if ( ! Integration ) delete settings [ key ] ;
175
+ } ) ;
175
176
176
177
// add integrations
177
- each ( function ( opts : unknown , name : string | number ) {
178
+ Object . keys ( settings ) . forEach ( key => {
179
+ const opts = settings [ key ]
180
+ const name = key
181
+
178
182
// Don't load disabled integrations
179
183
if ( options . integrations ) {
180
184
if (
@@ -185,13 +189,13 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
185
189
}
186
190
}
187
191
188
- var Integration = self . Integrations [ name ] ;
189
- var clonedOpts = { } ;
192
+ const Integration = self . Integrations [ name ] ;
193
+ const clonedOpts = { } ;
190
194
extend ( true , clonedOpts , opts ) ; // deep clone opts
191
- var integration = new Integration ( clonedOpts ) ;
195
+ const integration = new Integration ( clonedOpts ) ;
192
196
self . log ( 'initialize %o - %o' , name , opts ) ;
193
197
self . add ( integration ) ;
194
- } , settings ) ;
198
+ } ) ;
195
199
196
200
var integrations = this . _integrations ;
197
201
@@ -201,7 +205,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
201
205
202
206
// make ready callback
203
207
var readyCallCount = 0 ;
204
- var integrationCount = keys ( integrations ) . length ;
208
+ var integrationCount = Object . keys ( integrations ) . length ;
205
209
var ready = function ( ) {
206
210
readyCallCount ++ ;
207
211
if ( readyCallCount >= integrationCount ) {
@@ -218,14 +222,15 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
218
222
// initialize integrations, passing ready
219
223
// create a list of any integrations that did not initialize - this will be passed with all events for replay support:
220
224
this . failedInitializations = [ ] ;
221
- var initialPageSkipped = false ;
222
- each ( function ( integration ) {
225
+ let initialPageSkipped = false ;
226
+ Object . keys ( integrations ) . forEach ( key => {
227
+ const integration = integrations [ key ]
223
228
if (
224
229
options . initialPageview &&
225
230
integration . options . initialPageview === false
226
231
) {
227
232
// We've assumed one initial pageview, so make sure we don't count the first page call.
228
- var page = integration . page ;
233
+ let page = integration . page ;
229
234
integration . page = function ( ) {
230
235
if ( initialPageSkipped ) {
231
236
return page . apply ( this , arguments ) ;
@@ -245,7 +250,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
245
250
} ) ;
246
251
integration . initialize ( ) ;
247
252
} catch ( e ) {
248
- var integrationName = integration . name ;
253
+ let integrationName = integration . name ;
249
254
metrics . increment ( 'analytics_js.integration.invoke.error' , {
250
255
method : 'initialize' ,
251
256
integration_name : integration . name
@@ -256,7 +261,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
256
261
257
262
integration . ready ( ) ;
258
263
}
259
- } , integrations ) ;
264
+ } ) ;
260
265
261
266
// backwards compat with angular plugin and used for init logic checks
262
267
this . initialized = true ;
@@ -465,37 +470,44 @@ Analytics.prototype.track = function(
465
470
*/
466
471
467
472
Analytics . prototype . trackClick = Analytics . prototype . trackLink = function (
468
- links : Element | Array < unknown > ,
473
+ links : Element | Array < Element > | JQuery ,
469
474
event : any ,
470
475
properties ?: any
471
476
) : SegmentAnalytics {
477
+ let elements : Array < Element > = [ ]
472
478
if ( ! links ) return this ;
473
479
// always arrays, handles jquery
474
- if ( type ( links ) === 'element' ) links = [ links ] ;
480
+ if ( links instanceof Element ) {
481
+ elements = [ links ]
482
+ } else if ( "toArray" in links ) {
483
+ elements = links . toArray ( )
484
+ } else {
485
+ elements = links as Array < Element >
486
+ }
475
487
476
- var self = this ;
477
- each ( function ( el ) {
488
+ elements . forEach ( el => {
478
489
if ( type ( el ) !== 'element' ) {
479
490
throw new TypeError ( 'Must pass HTMLElement to `analytics.trackLink`.' ) ;
480
491
}
481
- on ( el , 'click' , function ( e ) {
482
- var ev = is . fn ( event ) ? event ( el ) : event ;
483
- var props = is . fn ( properties ) ? properties ( el ) : properties ;
484
- var href =
492
+ on ( el , 'click' , ( e ) => {
493
+ const ev = is . fn ( event ) ? event ( el ) : event ;
494
+ const props = is . fn ( properties ) ? properties ( el ) : properties ;
495
+ const href =
485
496
el . getAttribute ( 'href' ) ||
486
497
el . getAttributeNS ( 'http://www.w3.org/1999/xlink' , 'href' ) ||
487
498
el . getAttribute ( 'xlink:href' ) ;
488
499
489
- self . track ( ev , props ) ;
500
+ this . track ( ev , props ) ;
490
501
502
+ // @ts -ignore
491
503
if ( href && el . target !== '_blank' && ! isMeta ( e ) ) {
492
504
prevent ( e ) ;
493
- self . _callback ( function ( ) {
505
+ this . _callback ( function ( ) {
494
506
window . location . href = href ;
495
507
} ) ;
496
508
}
497
509
} ) ;
498
- } , links ) ;
510
+ } ) ;
499
511
500
512
return this ;
501
513
} ;
@@ -521,18 +533,19 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
521
533
// always arrays, handles jquery
522
534
if ( type ( forms ) === 'element' ) forms = [ forms ] ;
523
535
524
- var self = this ;
525
- each ( function ( el : { submit : ( ) => void } ) {
536
+ const elements = forms as Array < unknown >
537
+
538
+ elements . forEach ( ( el : { submit : ( ) => void } ) => {
526
539
if ( type ( el ) !== 'element' )
527
540
throw new TypeError ( 'Must pass HTMLElement to `analytics.trackForm`.' ) ;
528
- function handler ( e ) {
541
+ const handler = ( e ) => {
529
542
prevent ( e ) ;
530
543
531
- var ev = is . fn ( event ) ? event ( el ) : event ;
532
- var props = is . fn ( properties ) ? properties ( el ) : properties ;
533
- self . track ( ev , props ) ;
544
+ const ev = is . fn ( event ) ? event ( el ) : event ;
545
+ const props = is . fn ( properties ) ? properties ( el ) : properties ;
546
+ this . track ( ev , props ) ;
534
547
535
- self . _callback ( function ( ) {
548
+ this . _callback ( function ( ) {
536
549
el . submit ( ) ;
537
550
} ) ;
538
551
}
@@ -545,7 +558,7 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
545
558
} else {
546
559
on ( el , 'submit' , handler ) ;
547
560
}
548
- } , forms ) ;
561
+ } ) ;
549
562
550
563
return this ;
551
564
} ;
@@ -582,7 +595,7 @@ Analytics.prototype.page = function(
582
595
( name = category ) , ( category = null ) ;
583
596
/* eslint-enable no-unused-expressions, no-sequences */
584
597
585
- properties = clone ( properties ) || { } ;
598
+ properties = cloneDeep ( properties ) || { } ;
586
599
if ( name ) properties . name = name ;
587
600
if ( category ) properties . category = category ;
588
601
@@ -594,7 +607,7 @@ Analytics.prototype.page = function(
594
607
// Mirror user overrides to `options.context.page` (but exclude custom properties)
595
608
// (Any page defaults get applied in `this.normalize` for consistency.)
596
609
// Weird, yeah--moving special props to `context.page` will fix this in the long term.
597
- var overrides = pick ( keys ( defs ) , properties ) ;
610
+ var overrides = pick ( Object . keys ( defs ) , properties ) ;
598
611
if ( ! is . empty ( overrides ) ) {
599
612
options = options || { } ;
600
613
options . context = options . context || { } ;
@@ -795,9 +808,11 @@ Analytics.prototype._invoke = function(
795
808
return this ;
796
809
797
810
function applyIntegrationMiddlewares ( facade ) {
798
- var failedInitializations = self . failedInitializations || [ ] ;
799
- each ( function ( integration , name ) {
800
- var facadeCopy = extend ( true , new Facade ( { } ) , facade ) ;
811
+ let failedInitializations = self . failedInitializations || [ ] ;
812
+ Object . keys ( self . _integrations ) . forEach ( key => {
813
+ const integration = self . _integrations [ key ]
814
+ const { name } = integration
815
+ const facadeCopy = extend ( true , new Facade ( { } ) , facade ) ;
801
816
802
817
if ( ! facadeCopy . enabled ( name ) ) return ;
803
818
// Check if an integration failed to initialize.
@@ -881,7 +896,7 @@ Analytics.prototype._invoke = function(
881
896
) ;
882
897
}
883
898
}
884
- } , self . _integrations ) ;
899
+ } ) ;
885
900
}
886
901
} ;
887
902
@@ -963,7 +978,7 @@ Analytics.prototype.normalize = function(msg: {
963
978
context : { page } ;
964
979
anonymousId : string ;
965
980
} ) : object {
966
- msg = normalize ( msg , keys ( this . _integrations ) ) ;
981
+ msg = normalize ( msg , Object . keys ( this . _integrations ) ) ;
967
982
if ( msg . anonymousId ) user . anonymousId ( msg . anonymousId ) ;
968
983
msg . anonymousId = user . anonymousId ( ) ;
969
984
0 commit comments