@@ -70,8 +70,9 @@ function Analytics() {
70
70
this . log = debug ( 'analytics.js' ) ;
71
71
bindAll ( this ) ;
72
72
73
- var self = this ;
74
- this . on ( 'initialize' , function ( settings , options ) {
73
+
74
+ const self = this ;
75
+ this . on ( 'initialize' , function ( _ , options ) {
75
76
if ( options . initialPageview ) self . page ( ) ;
76
77
self . _parseQuery ( window . location . search ) ;
77
78
} ) ;
@@ -170,13 +171,16 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
170
171
171
172
// clean unknown integrations from settings
172
173
var self = this ;
173
- each ( function ( _opts : unknown , name : string | number ) {
174
- var Integration = self . Integrations [ name ] ;
175
- if ( ! Integration ) delete settings [ name ] ;
176
- } , settings ) ;
174
+ Object . keys ( settings ) . forEach ( key => {
175
+ var Integration = self . Integrations [ key ] ;
176
+ if ( ! Integration ) delete settings [ key ] ;
177
+ } ) ;
177
178
178
179
// add integrations
179
- each ( function ( opts : unknown , name : string | number ) {
180
+ Object . keys ( settings ) . forEach ( key => {
181
+ const opts = settings [ key ]
182
+ const name = key
183
+
180
184
// Don't load disabled integrations
181
185
if ( options . integrations ) {
182
186
if (
@@ -187,13 +191,13 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
187
191
}
188
192
}
189
193
190
- var Integration = self . Integrations [ name ] ;
191
- var clonedOpts = { } ;
194
+ const Integration = self . Integrations [ name ] ;
195
+ const clonedOpts = { } ;
192
196
extend ( true , clonedOpts , opts ) ; // deep clone opts
193
- var integration = new Integration ( clonedOpts ) ;
197
+ const integration = new Integration ( clonedOpts ) ;
194
198
self . log ( 'initialize %o - %o' , name , opts ) ;
195
199
self . add ( integration ) ;
196
- } , settings ) ;
200
+ } ) ;
197
201
198
202
var integrations = this . _integrations ;
199
203
@@ -220,14 +224,15 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
220
224
// initialize integrations, passing ready
221
225
// create a list of any integrations that did not initialize - this will be passed with all events for replay support:
222
226
this . failedInitializations = [ ] ;
223
- var initialPageSkipped = false ;
224
- each ( function ( integration ) {
227
+ let initialPageSkipped = false ;
228
+ Object . keys ( integrations ) . forEach ( key => {
229
+ const integration = integrations [ key ]
225
230
if (
226
231
options . initialPageview &&
227
232
integration . options . initialPageview === false
228
233
) {
229
234
// We've assumed one initial pageview, so make sure we don't count the first page call.
230
- var page = integration . page ;
235
+ let page = integration . page ;
231
236
integration . page = function ( ) {
232
237
if ( initialPageSkipped ) {
233
238
return page . apply ( this , arguments ) ;
@@ -247,7 +252,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
247
252
} ) ;
248
253
integration . initialize ( ) ;
249
254
} catch ( e ) {
250
- var integrationName = integration . name ;
255
+ let integrationName = integration . name ;
251
256
metrics . increment ( 'analytics_js.integration.invoke.error' , {
252
257
method : 'initialize' ,
253
258
integration_name : integration . name
@@ -258,7 +263,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
258
263
259
264
integration . ready ( ) ;
260
265
}
261
- } , integrations ) ;
266
+ } ) ;
262
267
263
268
// backwards compat with angular plugin and used for init logic checks
264
269
this . initialized = true ;
@@ -467,37 +472,44 @@ Analytics.prototype.track = function(
467
472
*/
468
473
469
474
Analytics . prototype . trackClick = Analytics . prototype . trackLink = function (
470
- links : Element | Array < unknown > ,
475
+ links : Element | Array < Element > | JQuery ,
471
476
event : any ,
472
477
properties ?: any
473
478
) : SegmentAnalytics {
479
+ let elements : Array < Element > = [ ]
474
480
if ( ! links ) return this ;
475
481
// always arrays, handles jquery
476
- if ( type ( links ) === 'element' ) links = [ links ] ;
482
+ if ( links instanceof Element ) {
483
+ elements = [ links ]
484
+ } else if ( "toArray" in links ) {
485
+ elements = links . toArray ( )
486
+ } else {
487
+ elements = links as Array < Element >
488
+ }
477
489
478
- var self = this ;
479
- each ( function ( el ) {
490
+ elements . forEach ( el => {
480
491
if ( type ( el ) !== 'element' ) {
481
492
throw new TypeError ( 'Must pass HTMLElement to `analytics.trackLink`.' ) ;
482
493
}
483
- on ( el , 'click' , function ( e ) {
484
- var ev = is . fn ( event ) ? event ( el ) : event ;
485
- var props = is . fn ( properties ) ? properties ( el ) : properties ;
486
- var href =
494
+ on ( el , 'click' , ( e ) => {
495
+ const ev = is . fn ( event ) ? event ( el ) : event ;
496
+ const props = is . fn ( properties ) ? properties ( el ) : properties ;
497
+ const href =
487
498
el . getAttribute ( 'href' ) ||
488
499
el . getAttributeNS ( 'http://www.w3.org/1999/xlink' , 'href' ) ||
489
500
el . getAttribute ( 'xlink:href' ) ;
490
501
491
- self . track ( ev , props ) ;
502
+ this . track ( ev , props ) ;
492
503
504
+ // @ts -ignore
493
505
if ( href && el . target !== '_blank' && ! isMeta ( e ) ) {
494
506
prevent ( e ) ;
495
- self . _callback ( function ( ) {
507
+ this . _callback ( function ( ) {
496
508
window . location . href = href ;
497
509
} ) ;
498
510
}
499
511
} ) ;
500
- } , links ) ;
512
+ } ) ;
501
513
502
514
return this ;
503
515
} ;
@@ -523,18 +535,19 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
523
535
// always arrays, handles jquery
524
536
if ( type ( forms ) === 'element' ) forms = [ forms ] ;
525
537
526
- var self = this ;
527
- each ( function ( el : { submit : ( ) => void } ) {
538
+ const elements = forms as Array < unknown >
539
+
540
+ elements . forEach ( ( el : { submit : ( ) => void } ) => {
528
541
if ( type ( el ) !== 'element' )
529
542
throw new TypeError ( 'Must pass HTMLElement to `analytics.trackForm`.' ) ;
530
- function handler ( e ) {
543
+ const handler = ( e ) => {
531
544
prevent ( e ) ;
532
545
533
- var ev = is . fn ( event ) ? event ( el ) : event ;
534
- var props = is . fn ( properties ) ? properties ( el ) : properties ;
535
- self . track ( ev , props ) ;
546
+ const ev = is . fn ( event ) ? event ( el ) : event ;
547
+ const props = is . fn ( properties ) ? properties ( el ) : properties ;
548
+ this . track ( ev , props ) ;
536
549
537
- self . _callback ( function ( ) {
550
+ this . _callback ( function ( ) {
538
551
el . submit ( ) ;
539
552
} ) ;
540
553
}
@@ -547,7 +560,7 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
547
560
} else {
548
561
on ( el , 'submit' , handler ) ;
549
562
}
550
- } , forms ) ;
563
+ } ) ;
551
564
552
565
return this ;
553
566
} ;
@@ -565,11 +578,11 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
565
578
*/
566
579
567
580
Analytics . prototype . page = function (
568
- category : string ,
569
- name : string ,
570
- properties : any ,
571
- options : any ,
572
- fn : unknown
581
+ category ? : string ,
582
+ name ? : string ,
583
+ properties ? : any ,
584
+ options ? : any ,
585
+ fn ? : unknown
573
586
) : SegmentAnalytics {
574
587
// Argument reshuffling.
575
588
/* eslint-disable no-unused-expressions, no-sequences */
@@ -797,9 +810,11 @@ Analytics.prototype._invoke = function(
797
810
return this ;
798
811
799
812
function applyIntegrationMiddlewares ( facade ) {
800
- var failedInitializations = self . failedInitializations || [ ] ;
801
- each ( function ( integration , name ) {
802
- var facadeCopy = extend ( true , new Facade ( { } ) , facade ) ;
813
+ let failedInitializations = self . failedInitializations || [ ] ;
814
+ Object . keys ( self . _integrations ) . forEach ( key => {
815
+ const integration = self . _integrations [ key ]
816
+ const { name } = integration
817
+ const facadeCopy = extend ( true , new Facade ( { } ) , facade ) ;
803
818
804
819
if ( ! facadeCopy . enabled ( name ) ) return ;
805
820
// Check if an integration failed to initialize.
@@ -883,7 +898,7 @@ Analytics.prototype._invoke = function(
883
898
) ;
884
899
}
885
900
}
886
- } , self . _integrations ) ;
901
+ } ) ;
887
902
}
888
903
} ;
889
904
0 commit comments