Skip to content

Commit

Permalink
refactor: Migrate User Attribute Persistence Methods (#868) (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle authored Jun 11, 2024
1 parent 12d0dd7 commit 89387c2
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 83 deletions.
4 changes: 1 addition & 3 deletions src/filteredMparticleUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ export default function filteredMparticleUser(
},
getAllUserAttributes: function() {
var userAttributesCopy = {};
var userAttributes = mpInstance._Persistence.getAllUserAttributes(
mpid
);
var userAttributes = mpInstance._Store.getUserAttributes(mpid);

if (userAttributes) {
for (var prop in userAttributes) {
Expand Down
4 changes: 4 additions & 0 deletions src/identity-user-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export interface IUserIdentityChangeEvent extends BaseEvent {
userIdentityChanges: ISDKUserIdentityChanges;
}

export interface ISDKUserAttributes {
[key: string]: string | string[] | null;
}

export interface ISDKUserAttributeChangeData {
UserAttributeName: string;
New: string;
Expand Down
61 changes: 24 additions & 37 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,12 +866,9 @@ export default function Identity(mpInstance) {
* @param {String} key
* @param {String} value
*/
// https://go.mparticle.com/work/SQDSDKS-4576
// https://go.mparticle.com/work/SQDSDKS-6373
setUserAttribute: function(key, newValue) {
var cookies,
userAttributes,
previousUserAttributeValue,
isNewAttribute;

mpInstance._SessionManager.resetSessionTimer();

if (mpInstance._Helpers.canLog()) {
Expand All @@ -896,11 +893,11 @@ export default function Identity(mpInstance) {
JSON.stringify({ key: key, value: newValue })
);
} else {
cookies = mpInstance._Persistence.getPersistence();

userAttributes = this.getAllUserAttributes();
const userAttributes = this.getAllUserAttributes();
let previousUserAttributeValue;
let isNewAttribute;

var existingProp = mpInstance._Helpers.findKeyInObject(
const existingProp = mpInstance._Helpers.findKeyInObject(
userAttributes,
key
);
Expand All @@ -915,13 +912,10 @@ export default function Identity(mpInstance) {
}

userAttributes[key] = newValue;
if (cookies && cookies[mpid]) {
cookies[mpid].ua = userAttributes;
mpInstance._Persistence.savePersistence(
cookies,
mpid
);
}
mpInstance._Store.setUserAttributes(
mpid,
userAttributes
);

self.sendUserAttributeChangeEvent(
key,
Expand Down Expand Up @@ -949,6 +943,7 @@ export default function Identity(mpInstance) {
* @method setUserAttributes
* @param {Object} user attribute object with keys of the attribute type, and value of the attribute value
*/
// https://go.mparticle.com/work/SQDSDKS-6373
setUserAttributes: function(userAttributes) {
mpInstance._SessionManager.resetSessionTimer();
if (isObject(userAttributes)) {
Expand Down Expand Up @@ -1036,13 +1031,8 @@ export default function Identity(mpInstance) {
* @param {String} key
* @param {Array} value an array of values
*/
// https://go.mparticle.com/work/SQDSDKS-6373
setUserAttributeList: function(key, newValue) {
var cookies,
userAttributes,
previousUserAttributeValue,
isNewAttribute,
userAttributeChange;

mpInstance._SessionManager.resetSessionTimer();

if (!mpInstance._Helpers.Validators.isValidKeyValue(key)) {
Expand All @@ -1058,19 +1048,20 @@ export default function Identity(mpInstance) {
return;
}

var arrayCopy = newValue.slice();
const arrayCopy = newValue.slice();

if (mpInstance._Store.webviewBridgeEnabled) {
mpInstance._NativeSdkHelpers.sendToNative(
Constants.NativeSdkPaths.SetUserAttributeList,
JSON.stringify({ key: key, value: arrayCopy })
);
} else {
cookies = mpInstance._Persistence.getPersistence();

userAttributes = this.getAllUserAttributes();
const userAttributes = this.getAllUserAttributes();
let previousUserAttributeValue;
let isNewAttribute;
let userAttributeChange;

var existingProp = mpInstance._Helpers.findKeyInObject(
const existingProp = mpInstance._Helpers.findKeyInObject(
userAttributes,
key
);
Expand All @@ -1085,12 +1076,9 @@ export default function Identity(mpInstance) {
}

userAttributes[key] = arrayCopy;
if (cookies && cookies[mpid]) {
cookies[mpid].ua = userAttributes;
mpInstance._Persistence.savePersistence(cookies, mpid);
}
mpInstance._Store.setUserAttributes(mpid, userAttributes);

// If the new attributeList length is different previous, then there is a change event.
// If the new attributeList length is different than the previous, then there is a change event.
// Loop through new attributes list, see if they are all in the same index as previous user attributes list
// If there are any changes, break, and immediately send a userAttributeChangeEvent with full array as a value
if (
Expand Down Expand Up @@ -1194,13 +1182,12 @@ export default function Identity(mpInstance) {
* @return {Object} an object of all user attributes. Example: { attr1: 'value1', attr2: ['a', 'b', 'c'] }
*/
getAllUserAttributes: function() {
var userAttributesCopy = {};
var userAttributes = mpInstance._Persistence.getAllUserAttributes(
mpid
);
const { getUserAttributes } = mpInstance._Store;
const userAttributesCopy = {};
const userAttributes = getUserAttributes(mpid);

if (userAttributes) {
for (var prop in userAttributes) {
for (const prop in userAttributes) {
if (userAttributes.hasOwnProperty(prop)) {
if (Array.isArray(userAttributes[prop])) {
userAttributesCopy[prop] = userAttributes[
Expand Down
6 changes: 0 additions & 6 deletions src/persistence.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Context } from '@mparticle/event-models';
import {
AllUserAttributes,
ConsentState,
IdentityApiData,
MPID,
Product,
Expand Down Expand Up @@ -122,17 +121,12 @@ export interface IPersistence {
getCookieDomain(): string;
getDomain(doc: string, locationHostname: string): string;
getUserIdentities(mpid: MPID): UserIdentities;
getAllUserAttributes(mpid: MPID): AllUserAttributes;
getCartProducts(mpid: MPID): Product[];
setCartProducts(allProducts: Product[]): void;
saveUserIdentitiesToPersistence(
mpid: MPID,
userIdentities: UserIdentities
): void;
saveUserAttributesToPersistence(
mpid: MPID,
userAttributes: UserAttributes
): void;
saveUserCookieSyncDatesToPersistence(mpid: MPID, csd: CookieSyncDate): void;
savePersistence(persistance: IPersistenceMinified): void;
getPersistence(): IPersistenceMinified;
Expand Down
29 changes: 0 additions & 29 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,16 +892,6 @@ export default function _Persistence(mpInstance) {
}
};

this.getAllUserAttributes = function(mpid) {
var persistence = self.getPersistence();

if (persistence && persistence[mpid] && persistence[mpid].ua) {
return persistence[mpid].ua;
} else {
return {};
}
};

this.getCartProducts = function(mpid) {
var allCartProducts,
cartProductsString = localStorage.getItem(
Expand Down Expand Up @@ -954,25 +944,6 @@ export default function _Persistence(mpInstance) {
}
};

this.saveUserAttributesToPersistence = function(mpid, userAttributes) {
var persistence = self.getPersistence();
if (userAttributes) {
if (persistence) {
if (persistence[mpid]) {
// TODO: Investigate why setting this to UI still shows up as UA
// when running `mParticle.getInstance()._Persistence.getLocalStorage()`
// https://go.mparticle.com/work/SQDSDKS-5195
persistence[mpid].ui = userAttributes;
} else {
persistence[mpid] = {
ui: userAttributes,
};
}
}
self.savePersistence(persistence);
}
};

this.saveUserCookieSyncDatesToPersistence = function(mpid, csd) {
if (csd) {
var persistence = self.getPersistence();
Expand Down
6 changes: 2 additions & 4 deletions src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
IMParticleUser,
ISDKUserIdentity,
IdentityCallback,
ISDKUserAttributes,
} from './identity-user-interfaces';
import { IIdentityType } from './types.interfaces';

Expand All @@ -34,10 +35,7 @@ export interface SDKEvent {
IsFirstRun: boolean;
EventName: string;
EventCategory: number;

// https://go.mparticle.com/work/SQDSDKS-5196
UserAttributes?: { [key: string]: string | string[] | null };

UserAttributes?: ISDKUserAttributes;
UserIdentities?: ISDKUserIdentity[];
SourceMessageId: string;
MPID: string;
Expand Down
12 changes: 11 additions & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from './utils';
import { IMinifiedConsentJSONObject, SDKConsentState } from './consent';
import { Kit, MPForwarder } from './forwarders.interfaces';
import { IdentityCallback } from './identity-user-interfaces';
import { IdentityCallback, UserAttributes } from './identity-user-interfaces';
import {
IGlobalStoreV2MinifiedKeys,
IPersistenceMinified,
Expand Down Expand Up @@ -195,6 +195,8 @@ export interface IStore {
setFirstSeenTime?(mpid: MPID, time?: number): void;
getLastSeenTime?(mpid: MPID): number;
setLastSeenTime?(mpid: MPID, time?: number): void;
getUserAttributes?(mpid: MPID): UserAttributes;
setUserAttributes?(mpid: MPID, attributes: UserAttributes): void;

addMpidToSessionHistory?(mpid: MPID, previousMpid?: MPID): void;
hasInvalidIdentifyRequest?: () => boolean;
Expand Down Expand Up @@ -617,6 +619,14 @@ export default function Store(
);
};

this.getUserAttributes = (mpid: MPID): UserAttributes =>
this._getFromPersistence(mpid, 'ua') || {};

this.setUserAttributes = (
mpid: MPID,
userAttributes: UserAttributes
): void => this._setPersistence(mpid, 'ua', userAttributes);

this.addMpidToSessionHistory = (mpid: MPID, previousMPID?: MPID): void => {
const indexOfMPID = this.currentSessionMPIDs.indexOf(mpid);

Expand Down
6 changes: 3 additions & 3 deletions test/src/tests-serverModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import sinon from 'sinon';
import { urls, testMPID, apiKey } from './config/constants';
import { expect } from 'chai';
import { IUploadObject } from '../../src/serverModel';
import { AllUserAttributes, IdentityApiData } from '@mparticle/web-sdk';
import { IdentityApiData } from '@mparticle/web-sdk';
import { BaseEvent, SDKEvent } from '../../src/sdkRuntimeModels';
import Constants from '../../src/constants';
import {
SDKConsentState,
SDKCCPAConsentState,
SDKGDPRConsentState,
} from '../../src/consent';
import { IMParticleUser } from '../../src/identity-user-interfaces';
import { IMParticleUser, ISDKUserAttributes } from '../../src/identity-user-interfaces';

let mockServer;
let initialEvent = {};
Expand Down Expand Up @@ -1584,7 +1584,7 @@ describe('ServerModel', () => {
getUserIdentities: (): IdentityApiData => ({
userIdentities: {},
}),
getAllUserAttributes: (): AllUserAttributes => {
getAllUserAttributes: (): ISDKUserAttributes => {
return attributes;
},
getMPID: () => {
Expand Down
Loading

0 comments on commit 89387c2

Please sign in to comment.