@@ -5,13 +5,12 @@ import {
55 InitOptions ,
66 SegmentAnalytics ,
77 SegmentOpts ,
8- SegmentIntegration ,
9- PageDefaults
8+ SegmentIntegration
109} from './types' ;
1110var url = require ( 'component-url' ) ;
1211
13- import cloneDeep from 'lodash.clonedeep'
14- import pick from 'lodash.pick'
12+ import cloneDeep from 'lodash.clonedeep' ;
13+ import pick from 'lodash.pick' ;
1514
1615var _analytics = global . analytics ;
1716
@@ -45,6 +44,7 @@ var normalize = require('./normalize');
4544var on = require ( 'component-event' ) . bind ;
4645var pageDefaults = require ( './pageDefaults' ) ;
4746var prevent = require ( '@segment/prevent-default' ) ;
47+ var defaults = require ( '@ndhoule/defaults' ) ;
4848var store = require ( './store' ) ;
4949var user = require ( './user' ) ;
5050var type = require ( 'component-type' ) ;
@@ -322,13 +322,8 @@ Analytics.prototype.identify = function(
322322 } ) ;
323323
324324 // Add the initialize integrations so the server-side ones can be disabled too
325- // NOTE: We need to merge integrations, not override them with assign
326- // since it is possible to change the initialized integrations at runtime.
327325 if ( this . options . integrations ) {
328- msg . integrations = {
329- ...this . options . integrations ,
330- ...msg . integrations
331- } ;
326+ defaults ( msg . integrations , this . options . integrations ) ;
332327 }
333328
334329 this . _invoke ( 'identify' , new Identify ( msg ) ) ;
@@ -383,13 +378,8 @@ Analytics.prototype.group = function(
383378 } ) ;
384379
385380 // Add the initialize integrations so the server-side ones can be disabled too
386- // NOTE: We need to merge integrations, not override them with assign
387- // since it is possible to change the initialized integrations at runtime.
388381 if ( this . options . integrations ) {
389- msg . integrations = {
390- ...this . options . integrations ,
391- ...msg . integrations
392- } ;
382+ defaults ( msg . integrations , this . options . integrations ) ;
393383 }
394384
395385 this . _invoke ( 'group' , new Group ( msg ) ) ;
@@ -453,12 +443,10 @@ Analytics.prototype.track = function(
453443 }
454444
455445 // Add the initialize integrations so the server-side ones can be disabled too
456- // NOTE: We need to merge integrations, not override them with assign
457- // since it is possible to change the initialized integrations at runtime.
458- msg . integrations = {
459- ...this . _mergeInitializeAndPlanIntegrations ( planIntegrationOptions ) ,
460- ...msg . integrations
461- } ;
446+ defaults (
447+ msg . integrations ,
448+ this . _mergeInitializeAndPlanIntegrations ( planIntegrationOptions )
449+ ) ;
462450
463451 this . _invoke ( 'track' , new Track ( msg ) ) ;
464452
@@ -611,15 +599,8 @@ Analytics.prototype.page = function(
611599
612600 // Ensure properties has baseline spec properties.
613601 // TODO: Eventually move these entirely to `options.context.page`
614- // FIXME: This is purposely not overriding `defs`. There was a bug in the logic implemented by `@ndhoule/defaults`.
615- // This bug made it so we only would overwrite values in `defs` that were set to `undefined`.
616- // In some cases, though, pageDefaults will return defaults with values set to "" (such as `window.location.search` defaulting to "").
617- // The decision to not fix this bus was made to preserve backwards compatibility.
618602 const defs = pageDefaults ( ) ;
619- properties = {
620- ...properties ,
621- ...defs
622- } ;
603+ defaults ( properties , defs ) ;
623604
624605 // Mirror user overrides to `options.context.page` (but exclude custom properties)
625606 // (Any page defaults get applied in `this.normalize` for consistency.)
@@ -639,13 +620,8 @@ Analytics.prototype.page = function(
639620 } ) ;
640621
641622 // Add the initialize integrations so the server-side ones can be disabled too
642- // NOTE: We need to merge integrations, not override them with assign
643- // since it is possible to change the initialized integrations at runtime.
644623 if ( this . options . integrations ) {
645- msg . integrations = {
646- ...this . options . integrations ,
647- ...msg . integrations
648- } ;
624+ defaults ( msg . integrations , this . options . integrations ) ;
649625 }
650626
651627 this . _invoke ( 'page' , new Page ( msg ) ) ;
@@ -697,13 +673,8 @@ Analytics.prototype.alias = function(
697673 } ) ;
698674
699675 // Add the initialize integrations so the server-side ones can be disabled too
700- // NOTE: We need to merge integrations, not override them with assign
701- // since it is possible to change the initialized integrations at runtime.
702676 if ( this . options . integrations ) {
703- msg . integrations = {
704- ...this . options . integrations ,
705- ...msg . integrations
706- } ;
677+ defaults ( msg . integrations , this . options . integrations ) ;
707678 }
708679
709680 this . _invoke ( 'alias' , new Alias ( msg ) ) ;
@@ -1002,19 +973,15 @@ Analytics.prototype._parseQuery = function(query: string): SegmentAnalytics {
1002973 */
1003974
1004975Analytics . prototype . normalize = function ( msg : {
1005- options : { [ key : string ] : unknown } ;
1006- context : { page : Partial < PageDefaults > } ;
976+ context : { page } ;
1007977 anonymousId : string ;
1008978} ) : object {
1009979 msg = normalize ( msg , Object . keys ( this . _integrations ) ) ;
1010980 if ( msg . anonymousId ) user . anonymousId ( msg . anonymousId ) ;
1011981 msg . anonymousId = user . anonymousId ( ) ;
1012982
1013983 // Ensure all outgoing requests include page data in their contexts.
1014- msg . context . page = {
1015- ...pageDefaults ( ) ,
1016- ...msg . context . page
1017- } ;
984+ msg . context . page = defaults ( msg . context . page || { } , pageDefaults ( ) ) ;
1018985
1019986 return msg ;
1020987} ;
0 commit comments