@@ -5661,6 +5661,8 @@ describe('Rokt Forwarder', () => {
56615661 expect ( pending . length ) . toBe ( 1 ) ;
56625662 expect ( pending [ 0 ] . event_type ) . toBe ( 'login' ) ;
56635663 expect ( pending [ 0 ] . data . timestamp_unixtime_ms ) . toBeTypeOf ( 'number' ) ;
5664+ expect ( pending [ 0 ] . data . user_identities ) . toEqual ( { } ) ;
5665+ expect ( pending [ 0 ] . data . user_attributes ) . toEqual ( { } ) ;
56645666 } ) ;
56655667
56665668 it ( 'should add an identity event to pendingIdentityEvents on onLogoutComplete' , ( ) => {
@@ -5676,6 +5678,8 @@ describe('Rokt Forwarder', () => {
56765678 expect ( pending . length ) . toBe ( 1 ) ;
56775679 expect ( pending [ 0 ] . event_type ) . toBe ( 'logout' ) ;
56785680 expect ( pending [ 0 ] . data . timestamp_unixtime_ms ) . toBeTypeOf ( 'number' ) ;
5681+ expect ( pending [ 0 ] . data . user_identities ) . toEqual ( { } ) ;
5682+ expect ( pending [ 0 ] . data . user_attributes ) . toEqual ( { } ) ;
56795683 } ) ;
56805684
56815685 it ( 'should add identity events to pendingIdentityEvents on onModifyComplete and onUserIdentified' , ( ) => {
@@ -5694,6 +5698,104 @@ describe('Rokt Forwarder', () => {
56945698 expect ( pending [ 1 ] . event_type ) . toBe ( 'identify' ) ;
56955699 } ) ;
56965700
5701+ it ( 'should include user_identities inside data of the identity event payload' , ( ) => {
5702+ const mockUser = {
5703+ getMPID : ( ) => '123' ,
5704+ getAllUserAttributes : ( ) => ( { } ) ,
5705+ getUserIdentities : ( ) => ( {
5706+ userIdentities : {
5707+ email : 'user@example.com' ,
5708+ customerid : 'cust-456' ,
5709+ } ,
5710+ } ) ,
5711+ } ;
5712+
5713+ ( window as any ) . mParticle . forwarder . onLoginComplete ( mockUser , null ) ;
5714+
5715+ const pending = ( window as any ) . mParticle . forwarder . pendingIdentityEvents ;
5716+ expect ( pending . length ) . toBe ( 1 ) ;
5717+ expect ( pending [ 0 ] . data . user_identities ) . toEqual ( {
5718+ email : 'user@example.com' ,
5719+ customerid : 'cust-456' ,
5720+ } ) ;
5721+ } ) ;
5722+
5723+ it ( 'should apply emailsha256 mapping to user_identities inside data of the identity event payload' , ( ) => {
5724+ ( window as any ) . mParticle . forwarder . _mappedEmailSha256Key = 'other' ;
5725+
5726+ const mockUser = {
5727+ getMPID : ( ) => '123' ,
5728+ getAllUserAttributes : ( ) => ( { } ) ,
5729+ getUserIdentities : ( ) => ( {
5730+ userIdentities : {
5731+ customerid : 'cust-789' ,
5732+ other : 'abc123hashedemail' ,
5733+ } ,
5734+ } ) ,
5735+ } ;
5736+
5737+ ( window as any ) . mParticle . forwarder . onLoginComplete ( mockUser , null ) ;
5738+
5739+ const pending = ( window as any ) . mParticle . forwarder . pendingIdentityEvents ;
5740+ expect ( pending . length ) . toBe ( 1 ) ;
5741+ expect ( pending [ 0 ] . data . user_identities ) . toEqual ( {
5742+ customerid : 'cust-789' ,
5743+ emailsha256 : 'abc123hashedemail' ,
5744+ } ) ;
5745+ expect ( pending [ 0 ] . data . user_identities . other ) . toBeUndefined ( ) ;
5746+ } ) ;
5747+
5748+ it ( 'should include user_attributes inside data of the identity event payload' , ( ) => {
5749+ const mockUser = {
5750+ getMPID : ( ) => '123' ,
5751+ getAllUserAttributes : ( ) => ( { plan : 'premium' , region : 'us' } ) ,
5752+ getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
5753+ } ;
5754+
5755+ ( window as any ) . mParticle . forwarder . onLoginComplete ( mockUser , null ) ;
5756+
5757+ const pending = ( window as any ) . mParticle . forwarder . pendingIdentityEvents ;
5758+ expect ( pending . length ) . toBe ( 1 ) ;
5759+ expect ( pending [ 0 ] . data . user_attributes ) . toEqual ( { plan : 'premium' , region : 'us' } ) ;
5760+ } ) ;
5761+
5762+ it ( 'should snapshot user_attributes from the user at event build time' , ( ) => {
5763+ const mockUserAtLogin = {
5764+ getMPID : ( ) => '123' ,
5765+ getAllUserAttributes : ( ) => ( { role : 'admin' } ) ,
5766+ getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
5767+ } ;
5768+ const mockUserAtModify = {
5769+ getMPID : ( ) => '123' ,
5770+ getAllUserAttributes : ( ) => ( { role : 'viewer' } ) ,
5771+ getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
5772+ } ;
5773+
5774+ ( window as any ) . mParticle . forwarder . onLoginComplete ( mockUserAtLogin , null ) ;
5775+ ( window as any ) . mParticle . forwarder . onModifyComplete ( mockUserAtModify , null ) ;
5776+
5777+ const pending = ( window as any ) . mParticle . forwarder . pendingIdentityEvents ;
5778+ expect ( pending . length ) . toBe ( 2 ) ;
5779+ expect ( pending [ 0 ] . data . user_attributes ) . toEqual ( { role : 'admin' } ) ;
5780+ expect ( pending [ 1 ] . data . user_attributes ) . toEqual ( { role : 'viewer' } ) ;
5781+ } ) ;
5782+
5783+ it ( 'should include empty user_attributes inside data on logout for an anonymous session' , ( ) => {
5784+ const anonymousUser = {
5785+ getMPID : ( ) => '-1234567890' ,
5786+ getAllUserAttributes : ( ) => ( { } ) ,
5787+ getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
5788+ } ;
5789+
5790+ ( window as any ) . mParticle . forwarder . onLogoutComplete ( anonymousUser , null ) ;
5791+
5792+ const pending = ( window as any ) . mParticle . forwarder . pendingIdentityEvents ;
5793+ expect ( pending . length ) . toBe ( 1 ) ;
5794+ expect ( pending [ 0 ] . event_type ) . toBe ( 'logout' ) ;
5795+ expect ( pending [ 0 ] . data . user_identities ) . toEqual ( { } ) ;
5796+ expect ( pending [ 0 ] . data . user_attributes ) . toEqual ( { } ) ;
5797+ } ) ;
5798+
56975799 it ( 'should merge pendingIdentityEvents into the outgoing batch and clear the queue' , async ( ) => {
56985800 const receivedBatches : any [ ] = [ ] ;
56995801 ( window as any ) . Rokt . __batch_stream__ = function ( payload : any ) {
@@ -5719,6 +5821,8 @@ describe('Rokt Forwarder', () => {
57195821 expect ( receivedBatches [ 0 ] . events . length ) . toBe ( 2 ) ;
57205822 expect ( receivedBatches [ 0 ] . events [ 1 ] . event_type ) . toBe ( 'login' ) ;
57215823 expect ( receivedBatches [ 0 ] . events [ 1 ] . data . timestamp_unixtime_ms ) . toBeTypeOf ( 'number' ) ;
5824+ expect ( receivedBatches [ 0 ] . events [ 1 ] . data . user_identities ) . toEqual ( { } ) ;
5825+ expect ( receivedBatches [ 0 ] . events [ 1 ] . data . user_attributes ) . toEqual ( { } ) ;
57225826 // Queue should be cleared after flush
57235827 expect ( ( window as any ) . mParticle . forwarder . pendingIdentityEvents . length ) . toBe ( 0 ) ;
57245828 } ) ;
0 commit comments