88 SegmentIntegration
99} from './types' ;
1010
11+ import cloneDeep from 'lodash.clonedeep'
12+
1113var _analytics = global . analytics ;
1214
1315/*
@@ -27,18 +29,14 @@ var DestinationMiddlewareChain = require('./middleware')
2729var Page = require ( 'segmentio-facade' ) . Page ;
2830var Track = require ( 'segmentio-facade' ) . Track ;
2931var bindAll = require ( 'bind-all' ) ;
30- var clone = require ( './utils/clone' ) ;
3132var extend = require ( 'extend' ) ;
3233var cookie = require ( './cookie' ) ;
3334var metrics = require ( './metrics' ) ;
3435var debug = require ( 'debug' ) ;
3536var defaults = require ( '@ndhoule/defaults' ) ;
36- var each = require ( './utils/each' ) ;
37- var foldl = require ( '@ndhoule/foldl' ) ;
3837var group = require ( './group' ) ;
3938var is = require ( 'is' ) ;
4039var isMeta = require ( '@segment/is-meta' ) ;
41- var keys = require ( '@ndhoule/keys' ) ;
4240var memory = require ( './memory' ) ;
4341var nextTick = require ( 'next-tick' ) ;
4442var normalize = require ( './normalize' ) ;
@@ -69,8 +67,9 @@ function Analytics() {
6967 this . log = debug ( 'analytics.js' ) ;
7068 bindAll ( this ) ;
7169
72- var self = this ;
73- this . on ( 'initialize' , function ( settings , options ) {
70+
71+ const self = this ;
72+ this . on ( 'initialize' , function ( _ , options ) {
7473 if ( options . initialPageview ) self . page ( ) ;
7574 self . _parseQuery ( window . location . search ) ;
7675 } ) ;
@@ -169,13 +168,16 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
169168
170169 // clean unknown integrations from settings
171170 var self = this ;
172- each ( function ( _opts : unknown , name : string | number ) {
173- var Integration = self . Integrations [ name ] ;
174- if ( ! Integration ) delete settings [ name ] ;
175- } , settings ) ;
171+ Object . keys ( settings ) . forEach ( key => {
172+ var Integration = self . Integrations [ key ] ;
173+ if ( ! Integration ) delete settings [ key ] ;
174+ } ) ;
176175
177176 // add integrations
178- each ( function ( opts : unknown , name : string | number ) {
177+ Object . keys ( settings ) . forEach ( key => {
178+ const opts = settings [ key ]
179+ const name = key
180+
179181 // Don't load disabled integrations
180182 if ( options . integrations ) {
181183 if (
@@ -186,13 +188,13 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
186188 }
187189 }
188190
189- var Integration = self . Integrations [ name ] ;
190- var clonedOpts = { } ;
191+ const Integration = self . Integrations [ name ] ;
192+ const clonedOpts = { } ;
191193 extend ( true , clonedOpts , opts ) ; // deep clone opts
192- var integration = new Integration ( clonedOpts ) ;
194+ const integration = new Integration ( clonedOpts ) ;
193195 self . log ( 'initialize %o - %o' , name , opts ) ;
194196 self . add ( integration ) ;
195- } , settings ) ;
197+ } ) ;
196198
197199 var integrations = this . _integrations ;
198200
@@ -202,7 +204,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
202204
203205 // make ready callback
204206 var readyCallCount = 0 ;
205- var integrationCount = keys ( integrations ) . length ;
207+ var integrationCount = Object . keys ( integrations ) . length ;
206208 var ready = function ( ) {
207209 readyCallCount ++ ;
208210 if ( readyCallCount >= integrationCount ) {
@@ -219,14 +221,15 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
219221 // initialize integrations, passing ready
220222 // create a list of any integrations that did not initialize - this will be passed with all events for replay support:
221223 this . failedInitializations = [ ] ;
222- var initialPageSkipped = false ;
223- each ( function ( integration ) {
224+ let initialPageSkipped = false ;
225+ Object . keys ( integrations ) . forEach ( key => {
226+ const integration = integrations [ key ]
224227 if (
225228 options . initialPageview &&
226229 integration . options . initialPageview === false
227230 ) {
228231 // We've assumed one initial pageview, so make sure we don't count the first page call.
229- var page = integration . page ;
232+ let page = integration . page ;
230233 integration . page = function ( ) {
231234 if ( initialPageSkipped ) {
232235 return page . apply ( this , arguments ) ;
@@ -246,7 +249,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
246249 } ) ;
247250 integration . initialize ( ) ;
248251 } catch ( e ) {
249- var integrationName = integration . name ;
252+ let integrationName = integration . name ;
250253 metrics . increment ( 'analytics_js.integration.invoke.error' , {
251254 method : 'initialize' ,
252255 integration_name : integration . name
@@ -257,7 +260,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
257260
258261 integration . ready ( ) ;
259262 }
260- } , integrations ) ;
263+ } ) ;
261264
262265 // backwards compat with angular plugin and used for init logic checks
263266 this . initialized = true ;
@@ -466,37 +469,44 @@ Analytics.prototype.track = function(
466469 */
467470
468471Analytics . prototype . trackClick = Analytics . prototype . trackLink = function (
469- links : Element | Array < unknown > ,
472+ links : Element | Array < Element > | JQuery ,
470473 event : any ,
471474 properties ?: any
472475) : SegmentAnalytics {
476+ let elements : Array < Element > = [ ]
473477 if ( ! links ) return this ;
474478 // always arrays, handles jquery
475- 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+ }
476486
477- var self = this ;
478- each ( function ( el ) {
487+ elements . forEach ( el => {
479488 if ( type ( el ) !== 'element' ) {
480489 throw new TypeError ( 'Must pass HTMLElement to `analytics.trackLink`.' ) ;
481490 }
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 =
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 =
486495 el . getAttribute ( 'href' ) ||
487496 el . getAttributeNS ( 'http://www.w3.org/1999/xlink' , 'href' ) ||
488497 el . getAttribute ( 'xlink:href' ) ;
489498
490- self . track ( ev , props ) ;
499+ this . track ( ev , props ) ;
491500
501+ // @ts -ignore
492502 if ( href && el . target !== '_blank' && ! isMeta ( e ) ) {
493503 prevent ( e ) ;
494- self . _callback ( function ( ) {
504+ this . _callback ( function ( ) {
495505 window . location . href = href ;
496506 } ) ;
497507 }
498508 } ) ;
499- } , links ) ;
509+ } ) ;
500510
501511 return this ;
502512} ;
@@ -522,18 +532,19 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
522532 // always arrays, handles jquery
523533 if ( type ( forms ) === 'element' ) forms = [ forms ] ;
524534
525- var self = this ;
526- each ( function ( el : { submit : ( ) => void } ) {
535+ const elements = forms as Array < unknown >
536+
537+ elements . forEach ( ( el : { submit : ( ) => void } ) => {
527538 if ( type ( el ) !== 'element' )
528539 throw new TypeError ( 'Must pass HTMLElement to `analytics.trackForm`.' ) ;
529- function handler ( e ) {
540+ const handler = ( e ) => {
530541 prevent ( e ) ;
531542
532- var ev = is . fn ( event ) ? event ( el ) : event ;
533- var props = is . fn ( properties ) ? properties ( el ) : properties ;
534- 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 ) ;
535546
536- self . _callback ( function ( ) {
547+ this . _callback ( function ( ) {
537548 el . submit ( ) ;
538549 } ) ;
539550 }
@@ -546,7 +557,7 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
546557 } else {
547558 on ( el , 'submit' , handler ) ;
548559 }
549- } , forms ) ;
560+ } ) ;
550561
551562 return this ;
552563} ;
@@ -583,7 +594,7 @@ Analytics.prototype.page = function(
583594 ( name = category ) , ( category = null ) ;
584595 /* eslint-enable no-unused-expressions, no-sequences */
585596
586- properties = clone ( properties ) || { } ;
597+ properties = cloneDeep ( properties ) || { } ;
587598 if ( name ) properties . name = name ;
588599 if ( category ) properties . category = category ;
589600
@@ -595,7 +606,7 @@ Analytics.prototype.page = function(
595606 // Mirror user overrides to `options.context.page` (but exclude custom properties)
596607 // (Any page defaults get applied in `this.normalize` for consistency.)
597608 // Weird, yeah--moving special props to `context.page` will fix this in the long term.
598- var overrides = pick ( keys ( defs ) , properties ) ;
609+ var overrides = pick ( Object . keys ( defs ) , properties ) ;
599610 if ( ! is . empty ( overrides ) ) {
600611 options = options || { } ;
601612 options . context = options . context || { } ;
@@ -796,9 +807,11 @@ Analytics.prototype._invoke = function(
796807 return this ;
797808
798809 function applyIntegrationMiddlewares ( facade ) {
799- var failedInitializations = self . failedInitializations || [ ] ;
800- each ( function ( integration , name ) {
801- 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 ) ;
802815
803816 if ( ! facadeCopy . enabled ( name ) ) return ;
804817 // Check if an integration failed to initialize.
@@ -882,7 +895,7 @@ Analytics.prototype._invoke = function(
882895 ) ;
883896 }
884897 }
885- } , self . _integrations ) ;
898+ } ) ;
886899 }
887900} ;
888901
@@ -957,7 +970,7 @@ Analytics.prototype.normalize = function(msg: {
957970 context : { page } ;
958971 anonymousId : string ;
959972} ) : object {
960- msg = normalize ( msg , keys ( this . _integrations ) ) ;
973+ msg = normalize ( msg , Object . keys ( this . _integrations ) ) ;
961974 if ( msg . anonymousId ) user . anonymousId ( msg . anonymousId ) ;
962975 msg . anonymousId = user . anonymousId ( ) ;
963976
0 commit comments