Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ export default function _Persistence(mpInstance) {

// https://go.mparticle.com/work/SQDSDKS-6048
try {
if (mpInstance._Store.isLocalStorageAvailable) {
if (
mpInstance._Store.isLocalStorageAvailable &&
!mpInstance._Store.getNoTargeting()
) {
var encodedProducts = localStorage.getItem(
mpInstance._Store.prodStorageName
);
Expand Down Expand Up @@ -269,7 +272,10 @@ export default function _Persistence(mpInstance) {
};

this.getUserProductsFromLS = function(mpid) {
if (!mpInstance._Store.isLocalStorageAvailable) {
if (
mpInstance._Store.getNoTargeting() ||
!mpInstance._Store.isLocalStorageAvailable
) {
return [];
}

Expand Down Expand Up @@ -308,6 +314,15 @@ export default function _Persistence(mpInstance) {
};

this.getAllUserProductsFromLS = function() {
if (mpInstance._Store.getNoTargeting()) {
var currentUser = mpInstance.Identity.getCurrentUser();
var mpid = currentUser ? currentUser.getMPID() : null;
var result = {};
if (mpid) {
result[mpid] = { cp: [] };
}
return result;
Comment thread
rmi22186 marked this conversation as resolved.
}
var decodedProducts,
encodedProducts = localStorage.getItem(
mpInstance._Store.prodStorageName
Expand Down Expand Up @@ -342,14 +357,20 @@ export default function _Persistence(mpInstance) {
? allLocalStorageProducts[mpid].cp
: [],
};

if (mpInstance._Store.getNoTargeting()) {
return;
}
if (mpid) {
allLocalStorageProducts = allLocalStorageProducts || {};
allLocalStorageProducts[mpid] = currentUserProducts;
try {
window.localStorage.setItem(
encodeURIComponent(mpInstance._Store.prodStorageName),
Base64.encode(JSON.stringify(allLocalStorageProducts))
);
if (!mpInstance._Store.getNoTargeting()) {
window.localStorage.setItem(
encodeURIComponent(mpInstance._Store.prodStorageName),
Base64.encode(JSON.stringify(allLocalStorageProducts))
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also clean up the original code a bit so that i's more readable:

Suggested change
window.localStorage.setItem(
encodeURIComponent(mpInstance._Store.prodStorageName),
Base64.encode(JSON.stringify(allLocalStorageProducts))
);
const encodedKey = encodeURIComponent(mpInstance._Store.prodStorageName);
const encodedValue = Base64.encode(JSON.stringify(allLocalStorageProducts));
window.localStorage.setItem(encodedKey, encodedValue);

}
} catch (e) {
mpInstance.Logger.error(
'Error with setting products on localStorage.'
Expand Down Expand Up @@ -894,6 +915,9 @@ export default function _Persistence(mpInstance) {
};

this.getCartProducts = function(mpid) {
if (mpInstance._Store.getNoTargeting()) {
return [];
}
var allCartProducts,
cartProductsString = localStorage.getItem(
mpInstance._Store.prodStorageName
Expand All @@ -913,7 +937,10 @@ export default function _Persistence(mpInstance) {
};

this.setCartProducts = function(allProducts) {
if (!mpInstance._Store.isLocalStorageAvailable) {
if (
!mpInstance._Store.isLocalStorageAvailable &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be OR, not AND. If local storage isn't available, or if getNoTargeting returns true, then don't set the cart products

Suggested change
!mpInstance._Store.isLocalStorageAvailable &&
!mpInstance._Store.isLocalStorageAvailable || mpInstance._Store.getNoTargeting

mpInstance._Store.getNoTargeting()
) {
return;
}

Expand Down
33 changes: 33 additions & 0 deletions test/jest/persistence.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
mParticle,
MPConfig,
testMPID,
} from '../src/config/constants';

describe('Products Persistence', () => {
let mpInstance = mParticle.getInstance();
beforeEach(() => {
mParticle._resetForTests(MPConfig);
});

it('should update Products localStorage when noTargeting is false by default', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should update Products localStorage when noTargeting is false by default', () => {
it('should save products to localStorage when noTargeting is false by default', () => {

const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } });

expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1);
});

it('should NOT update Products localStorage when noTargeting is true', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should NOT update Products localStorage when noTargeting is true', () => {
it('should NOT save products to localStorage when noTargeting is true', () => {

mpInstance._Store.setNoTargeting(true);
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } });

expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0);
Comment thread
jaissica12 marked this conversation as resolved.
Outdated
expect(mpInstance._Persistence.getCartProducts(testMPID)).toEqual([]);
});

it('should update Products localStorage when noTargeting is false', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should update Products localStorage when noTargeting is false', () => {
it('should save products to localStorage when noTargeting is false', () => {

mpInstance._Store.setNoTargeting(false);
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } });

expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1);
});
});
47 changes: 47 additions & 0 deletions test/src/tests-persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,53 @@ describe('persistence', () => {
parsedProductsAfter['testMPID'].cp.length.should.equal(0);
});

describe('products persistence with noTargeting privacy flag', () => {
beforeEach(() => {
mParticle._resetForTests(MPConfig);
});

it('should not save products LS when noTargeting is true', async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should not save products LS when noTargeting is true', async () => {
it('should not save products to LS when noTargeting is true', async () => {

mParticle.config.noTargeting = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this is working.

Per the docs, this should be:

Suggested change
mParticle.config.noTargeting = true;
mParticle.config.launcherOptions = {noTargeting: true};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmi22186 Currently noFunctional / noTargeting is only read from top‑level mParticle.config during init, not from config.launcherOptions that is why mParticle.config.noTargeting = true; works. To use mParticle.config.launcherOptions = {noTargeting: true}; we need to update store to source privacy flags from config.launcherOptions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you have updated the noTargeting and noFunctional to pull from config.launcherOptions, this will need to be updated


mParticle.init(apiKey, mParticle.config);
await waitForCondition(hasIdentifyReturned);
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
expect(localStorageProducts).to.not.be.ok;
});

it('should save products LS when noTargeting is false', async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should save products LS when noTargeting is false', async () => {
it('should save products to LS when noTargeting is false', async () => {

mParticle.config.noTargeting = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mParticle.config.noTargeting = false;
mParticle.config.launcherOptions = {noTargeting: false};


mParticle.init(apiKey, mParticle.config);
await waitForCondition(hasIdentifyReturned);
const iphone = mParticle.eCommerce.createProduct(
'iphone',
'iphonesku',
599,
1
);
mParticle.eCommerce.Cart.add(iphone, true);

const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
expect(localStorageProducts).to.be.ok;
});

it('should save products LS when noTargeting is false by default', async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should save products LS when noTargeting is false by default', async () => {
it('should save products to LS when noTargeting is false by default', async () => {

mParticle.init(apiKey, mParticle.config);
await waitForCondition(hasIdentifyReturned);
const iphone = mParticle.eCommerce.createProduct(
'iphone',
'iphonesku',
599,
1
);
mParticle.eCommerce.Cart.add(iphone, true);

const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
expect(localStorageProducts).to.be.ok;
});
});

it('should only set setFirstSeenTime() once', async () => {
const cookies = JSON.stringify({
gs: {
Expand Down
Loading