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