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