5
5
InitOptions ,
6
6
SegmentAnalytics ,
7
7
SegmentOpts ,
8
- SegmentIntegration
8
+ SegmentIntegration ,
9
+ PageDefaults , Message
9
10
} from './types' ;
10
11
11
12
import cloneDeep from 'lodash.clonedeep'
@@ -323,8 +324,13 @@ Analytics.prototype.identify = function(
323
324
} ) ;
324
325
325
326
// Add the initialize integrations so the server-side ones can be disabled too
327
+ // NOTE: We need to merge integrations, not override them with assign
328
+ // since it is possible to change the initialized integrations at runtime.
326
329
if ( this . options . integrations ) {
327
- defaults ( msg . integrations , this . options . integrations ) ;
330
+ msg . integrations = {
331
+ ...this . options . integrations ,
332
+ ...msg . integrations
333
+ }
328
334
}
329
335
330
336
this . _invoke ( 'identify' , new Identify ( msg ) ) ;
@@ -379,8 +385,13 @@ Analytics.prototype.group = function(
379
385
} ) ;
380
386
381
387
// Add the initialize integrations so the server-side ones can be disabled too
388
+ // NOTE: We need to merge integrations, not override them with assign
389
+ // since it is possible to change the initialized integrations at runtime.
382
390
if ( this . options . integrations ) {
383
- defaults ( msg . integrations , this . options . integrations ) ;
391
+ msg . integrations = {
392
+ ...this . options . integrations ,
393
+ ...msg . integrations
394
+ }
384
395
}
385
396
386
397
this . _invoke ( 'group' , new Group ( msg ) ) ;
@@ -444,10 +455,12 @@ Analytics.prototype.track = function(
444
455
}
445
456
446
457
// Add the initialize integrations so the server-side ones can be disabled too
447
- defaults (
448
- msg . integrations ,
449
- this . _mergeInitializeAndPlanIntegrations ( planIntegrationOptions )
450
- ) ;
458
+ // NOTE: We need to merge integrations, not override them with assign
459
+ // since it is possible to change the initialized integrations at runtime.
460
+ msg . integrations = {
461
+ ...this . _mergeInitializeAndPlanIntegrations ( planIntegrationOptions ) ,
462
+ ...msg . integrations
463
+ }
451
464
452
465
this . _invoke ( 'track' , new Track ( msg ) ) ;
453
466
@@ -600,8 +613,15 @@ Analytics.prototype.page = function(
600
613
601
614
// Ensure properties has baseline spec properties.
602
615
// TODO: Eventually move these entirely to `options.context.page`
603
- const defs = pageDefaults ( ) ;
604
- defaults ( properties , defs ) ;
616
+ // FIXME: This is purposely not overriding `defs`. There was a bug in the logic implemented by `@ndhoule/defaults`.
617
+ // This bug made it so we only would overwrite values in `defs` that were set to `undefined`.
618
+ // In some cases, though, pageDefaults will return defaults with values set to "" (such as `window.location.search` defaulting to "").
619
+ // The decision to not fix this bus was made to preserve backwards compatibility.
620
+ const defs = pageDefaults ( )
621
+ properties = {
622
+ ...properties ,
623
+ ...defs
624
+ }
605
625
606
626
// Mirror user overrides to `options.context.page` (but exclude custom properties)
607
627
// (Any page defaults get applied in `this.normalize` for consistency.)
@@ -621,8 +641,13 @@ Analytics.prototype.page = function(
621
641
} ) ;
622
642
623
643
// Add the initialize integrations so the server-side ones can be disabled too
644
+ // NOTE: We need to merge integrations, not override them with assign
645
+ // since it is possible to change the initialized integrations at runtime.
624
646
if ( this . options . integrations ) {
625
- defaults ( msg . integrations , this . options . integrations ) ;
647
+ msg . integrations = {
648
+ ...this . options . integrations ,
649
+ ...msg . integrations
650
+ }
626
651
}
627
652
628
653
this . _invoke ( 'page' , new Page ( msg ) ) ;
@@ -674,8 +699,13 @@ Analytics.prototype.alias = function(
674
699
} ) ;
675
700
676
701
// Add the initialize integrations so the server-side ones can be disabled too
702
+ // NOTE: We need to merge integrations, not override them with assign
703
+ // since it is possible to change the initialized integrations at runtime.
677
704
if ( this . options . integrations ) {
678
- defaults ( msg . integrations , this . options . integrations ) ;
705
+ msg . integrations = {
706
+ ...this . options . integrations ,
707
+ ...msg . integrations
708
+ }
679
709
}
680
710
681
711
this . _invoke ( 'alias' , new Alias ( msg ) ) ;
@@ -967,15 +997,19 @@ Analytics.prototype._parseQuery = function(query: string): SegmentAnalytics {
967
997
*/
968
998
969
999
Analytics . prototype . normalize = function ( msg : {
970
- context : { page } ;
1000
+ options : { [ key : string ] : unknown }
1001
+ context : { page : Partial < PageDefaults > } ;
971
1002
anonymousId : string ;
972
1003
} ) : object {
973
1004
msg = normalize ( msg , Object . keys ( this . _integrations ) ) ;
974
1005
if ( msg . anonymousId ) user . anonymousId ( msg . anonymousId ) ;
975
1006
msg . anonymousId = user . anonymousId ( ) ;
976
1007
977
1008
// Ensure all outgoing requests include page data in their contexts.
978
- msg . context . page = defaults ( msg . context . page || { } , pageDefaults ( ) ) ;
1009
+ msg . context . page = {
1010
+ ...pageDefaults ( ) ,
1011
+ ...msg . context . page
1012
+ } ;
979
1013
980
1014
return msg ;
981
1015
} ;
0 commit comments