Skip to content

Commit

Permalink
fix: Add filtered user identities to identity complete methods (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Jan 2, 2024
1 parent 1bca974 commit 715e217
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 31 deletions.
10 changes: 3 additions & 7 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import Constants from './constants';
import Types from './types';
import { BatchUploader } from './batchUploader';
import {
MParticleUser,
MParticleWebSDK,
MPForwarder,
SDKEvent,
} from './sdkRuntimeModels';
import { MParticleUser, MParticleWebSDK, SDKEvent } from './sdkRuntimeModels';
import KitBlocker from './kitBlocking';
import { Dictionary, getRampNumber, isEmpty } from './utils';
import { Dictionary, isEmpty } from './utils';
import { IUploadObject } from './serverModel';
import { MPForwarder } from './forwarders.interfaces';

export type ForwardingStatsData = Dictionary<any>;

Expand Down
34 changes: 28 additions & 6 deletions src/forwarders.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { MParticleUser, SDKEvent, SDKEventCustomFlags, SDKUserIdentity } from './sdkRuntimeModels';
import {
MParticleUser,
SDKEvent,
SDKEventCustomFlags,
SDKUserIdentity,
} from './sdkRuntimeModels';
import { Dictionary } from './utils';
import { IKitConfigs } from './configAPIClient';
import { UserAttributes } from './persistence.interfaces';
import { IdentityApiData } from '@mparticle/web-sdk';

// TODO: https://go.mparticle.com/work/SQDSDKS-6035
export type Kit = Dictionary;
export type MPForwarder = Dictionary;

// The state of the kit when accessed via window.KitName via CDN
// or imported as an NPM package
export interface UnregisteredKit {
Expand Down Expand Up @@ -32,11 +41,24 @@ export interface ConfiguredKit
appVersion: string,
appName: string,
customFlags: SDKEventCustomFlags,
clientId: string): string;
onIdentifyComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
onLoginComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
onLogoutComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
onModifyComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
clientId: string
): string;
onIdentifyComplete(
user: MParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onLoginComplete(
user: MParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onLogoutComplete(
user: MParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onModifyComplete(
user: MParticleUser,
filteredIdentityRequest: IdentityApiData
): string | KitMappedMethodFailure;
onUserIdentified(user: MParticleUser): string | KitMappedMethodFailure;
process(event: SDKEvent): string;
setOptOut(isOptingOut: boolean): string | KitMappedMethodFailure;
Expand Down
23 changes: 19 additions & 4 deletions src/forwarders.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,30 +491,45 @@ export default function Forwarders(mpInstance, kitBlocker) {
mpInstance,
kitBlocker
);

const filteredUserIdentities = filteredUser.getUserIdentities();

if (identityMethod === Identify) {
if (forwarder.onIdentifyComplete) {
result = forwarder.onIdentifyComplete(filteredUser);
result = forwarder.onIdentifyComplete(
filteredUser,
filteredUserIdentities
);
if (result) {
mpInstance.Logger.verbose(result);
}
}
} else if (identityMethod === Login) {
if (forwarder.onLoginComplete) {
result = forwarder.onLoginComplete(filteredUser);
result = forwarder.onLoginComplete(
filteredUser,
filteredUserIdentities
);
if (result) {
mpInstance.Logger.verbose(result);
}
}
} else if (identityMethod === Logout) {
if (forwarder.onLogoutComplete) {
result = forwarder.onLogoutComplete(filteredUser);
result = forwarder.onLogoutComplete(
filteredUser,
filteredUserIdentities
);
if (result) {
mpInstance.Logger.verbose(result);
}
}
} else if (identityMethod === Modify) {
if (forwarder.onModifyComplete) {
result = forwarder.onModifyComplete(filteredUser);
result = forwarder.onModifyComplete(
filteredUser,
filteredUserIdentities
);
if (result) {
mpInstance.Logger.verbose(result);
}
Expand Down
5 changes: 1 addition & 4 deletions src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SDKConsentApi, SDKConsentState } from './consent';
import { IPersistence } from './persistence.interfaces';
import { IMPSideloadedKit } from './sideloadedKit';
import { ISessionManager } from './sessionManager';
import { Kit, MPForwarder } from './forwarders.interfaces';

// TODO: Resolve this with version in @mparticle/web-sdk
export type SDKEventCustomFlags = Dictionary<any>;
Expand Down Expand Up @@ -184,10 +185,6 @@ export type BooleanStringTitleCase = 'False' | 'True';

export type LogLevelType = 'none' | 'verbose' | 'warning' | 'error';

// TODO: Create true types for Kits and Kit Configs
export type Kit = Dictionary;
export type MPForwarder = Dictionary;

// TODO: This should eventually be moved into wherever init logic lives
// TODO: Replace/Merge this with MPConfiguration in @types/mparticle__web-sdk
// SDK Init Config represents the config that is passed into mParticle.init when
Expand Down
3 changes: 1 addition & 2 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import { IKitConfigs } from './configAPIClient';
import Constants from './constants';
import {
DataPlanResult,
Kit,
KitBlockerOptions,
LogLevelType,
MParticleWebSDK,
MPForwarder,
SDKDataPlan,
SDKEvent,
SDKGeoLocation,
Expand All @@ -24,6 +22,7 @@ import {
} from './sdkRuntimeModels';
import { isNumber, isDataPlanSlug, Dictionary } from './utils';
import { SDKConsentState } from './consent';
import { Kit, MPForwarder } from './forwarders.interfaces';

// This represents the runtime configuration of the SDK AFTER
// initialization has been complete and all settings and
Expand Down
155 changes: 155 additions & 0 deletions test/src/tests-forwarders.js
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,161 @@ describe('forwarders', function() {
done();
});

it('should pass filteredUser and filteredUserIdentities to onIdentifyComplete methods', function(done) {
mParticle._resetForTests(MPConfig);
const mockForwarder = new MockForwarder();
mockForwarder.register(window.mParticle.config);

const config1 = forwarderDefaultConfiguration('MockForwarder', 1);

// This removes the Google User Identity
config1.userIdentityFilters = [mParticle.IdentityType.Google];
window.mParticle.config.kitConfigs.push(config1);

mParticle.init(apiKey, window.mParticle.config);

mParticle.Identity.identify({
userIdentities: {
google: '[email protected]',
email: '[email protected]',
customerid: '123',
},
});

expect(window.MockForwarder1.instance.onIdentifyCompleteCalled).to.eq(
true
);
expect(window.MockForwarder1.instance.onIdentifyCompleteUser).to.be.ok;
expect(
window.MockForwarder1.instance
.onIdentifyCompleteFilteredUserIdentities
).to.deep.equal({
userIdentities: {
// Filtered Identity should no longer have `google` as an identity
email: '[email protected]',
customerid: '123',
},
});

done();
});

it('should pass filteredUser and filteredUserIdentities to onLoginComplete methods', function(done) {
mParticle._resetForTests(MPConfig);
const mockForwarder = new MockForwarder();
mockForwarder.register(window.mParticle.config);

const config1 = forwarderDefaultConfiguration('MockForwarder', 1);

// This removes the Google User Identity
config1.userIdentityFilters = [mParticle.IdentityType.Google];
window.mParticle.config.kitConfigs.push(config1);

mParticle.init(apiKey, window.mParticle.config);

mParticle.Identity.login({
userIdentities: {
google: '[email protected]',
email: '[email protected]',
customerid: '123',
},
});

expect(window.MockForwarder1.instance.onLoginCompleteCalled).to.eq(
true
);
expect(window.MockForwarder1.instance.onLoginCompleteUser).to.be.ok;
expect(
window.MockForwarder1.instance.onLoginCompleteFilteredUserIdentities
).to.deep.equal({
userIdentities: {
// Filtered Identity should no longer have `google` as an identity
email: '[email protected]',
customerid: '123',
},
});

done();
});

it('should pass filteredUser and filteredUserIdentities to onLogoutComplete methods', function(done) {
mParticle._resetForTests(MPConfig);
const mockForwarder = new MockForwarder();
mockForwarder.register(window.mParticle.config);

const config1 = forwarderDefaultConfiguration('MockForwarder', 1);

// This removes the Google User Identity
config1.userIdentityFilters = [mParticle.IdentityType.Google];
window.mParticle.config.kitConfigs.push(config1);

mParticle.init(apiKey, window.mParticle.config);

mParticle.Identity.logout({
userIdentities: {
google: '[email protected]',
email: '[email protected]',
customerid: '123',
},
});

expect(window.MockForwarder1.instance.onLogoutCompleteCalled).to.eq(
true
);
expect(window.MockForwarder1.instance.onLogoutCompleteUser).to.be.ok;
expect(
window.MockForwarder1.instance
.onLogoutCompleteFilteredUserIdentities
).to.deep.equal({
userIdentities: {
// Filtered Identity should no longer have `google` as an identity
email: '[email protected]',
customerid: '123',
},
});

done();
});

it('should pass filteredUser and filteredUserIdentities to onModifyComplete methods', function(done) {
mParticle._resetForTests(MPConfig);
const mockForwarder = new MockForwarder();
mockForwarder.register(window.mParticle.config);

const config1 = forwarderDefaultConfiguration('MockForwarder', 1);

// This removes the Google User Identity
config1.userIdentityFilters = [mParticle.IdentityType.Google];
window.mParticle.config.kitConfigs.push(config1);

mParticle.init(apiKey, window.mParticle.config);

mParticle.Identity.modify({
userIdentities: {
google: '[email protected]',
email: '[email protected]',
customerid: '123',
},
});

expect(window.MockForwarder1.instance.onModifyCompleteCalled).to.eq(
true
);
expect(window.MockForwarder1.instance.onModifyCompleteUser).to.be.ok;
expect(
window.MockForwarder1.instance
.onModifyCompleteFilteredUserIdentities
).to.deep.equal({
userIdentities: {
// Filtered Identity should no longer have `google` as an identity
email: '[email protected]',
customerid: '123',
},
});

done();
});

it('should not forward event if attribute forwarding rule is set', function(done) {
mParticle._resetForTests(MPConfig);
const mockForwarder = new MockForwarder();
Expand Down
32 changes: 24 additions & 8 deletions test/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,24 +391,40 @@ var pluses = /\+/g,
this.onUserIdentifiedUser = user;
};

this.onIdentifyComplete = function(user) {
this.onIdentifyComplete = function(
filteredUser,
filteredUserIdentities
) {
this.onIdentifyCompleteCalled = true;
this.onIdentifyCompleteUser = user;
this.onIdentifyCompleteUser = filteredUser;
this.onIdentifyCompleteFilteredUserIdentities = filteredUserIdentities;
};

this.onLoginComplete = function(user) {
this.onLoginComplete = function(
filteredUser,
filteredUserIdentities
) {
this.onLoginCompleteCalled = true;
this.onLoginCompleteUser = user;
this.onLoginCompleteUser = filteredUser;
this.onLoginCompleteFilteredUserIdentities = filteredUserIdentities;
};

this.onLogoutComplete = function(user) {
this.onLogoutComplete = function(
filteredUser,
filteredUserIdentities
) {
this.onLogoutCompleteCalled = true;
this.onLogoutCompleteUser = user;
this.onLogoutCompleteUser = filteredUser;
this.onLogoutCompleteFilteredUserIdentities = filteredUserIdentities;
};

this.onModifyComplete = function(user) {
this.onModifyComplete = function(
filteredUser,
filteredUserIdentities
) {
this.onModifyCompleteCalled = true;
this.onModifyCompleteUser = user;
this.onModifyCompleteUser = filteredUser;
this.onModifyCompleteFilteredUserIdentities = filteredUserIdentities;
};

this.setUserAttribute = function(key, value) {
Expand Down

0 comments on commit 715e217

Please sign in to comment.