Skip to content

Commit b7230d6

Browse files
committed
feat: disable product cookie when persistence is set and updated test
1 parent 645f46a commit b7230d6

2 files changed

Lines changed: 102 additions & 5 deletions

File tree

src/persistence.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ export default function _Persistence(mpInstance) {
8989
// https://go.mparticle.com/work/SQDSDKS-6048
9090
try {
9191
if (mpInstance._Store.isLocalStorageAvailable) {
92+
if (mpInstance._Store.getNoTargeting()) {
93+
localStorage.removeItem(
94+
mpInstance._Store.prodStorageName
95+
);
96+
mpInstance._Store.cartProducts = [];
97+
return;
98+
}
9299
var encodedProducts = localStorage.getItem(
93100
mpInstance._Store.prodStorageName
94101
);
@@ -269,7 +276,10 @@ export default function _Persistence(mpInstance) {
269276
};
270277

271278
this.getUserProductsFromLS = function(mpid) {
272-
if (!mpInstance._Store.isLocalStorageAvailable) {
279+
if (
280+
mpInstance._Store.getNoTargeting() ||
281+
!mpInstance._Store.isLocalStorageAvailable
282+
) {
273283
return [];
274284
}
275285

@@ -308,6 +318,15 @@ export default function _Persistence(mpInstance) {
308318
};
309319

310320
this.getAllUserProductsFromLS = function() {
321+
if (mpInstance._Store.getNoTargeting()) {
322+
var currentUser = mpInstance.Identity.getCurrentUser();
323+
var mpid = currentUser ? currentUser.getMPID() : null;
324+
var result = {};
325+
if (mpid) {
326+
result[mpid] = { cp: [] };
327+
}
328+
return result;
329+
}
311330
var decodedProducts,
312331
encodedProducts = localStorage.getItem(
313332
mpInstance._Store.prodStorageName
@@ -342,14 +361,24 @@ export default function _Persistence(mpInstance) {
342361
? allLocalStorageProducts[mpid].cp
343362
: [],
344363
};
364+
365+
if (mpInstance._Store.getNoTargeting()) {
366+
try {
367+
window.localStorage.removeItem(
368+
mpInstance._Store.prodStorageName
369+
);
370+
} catch (e) {}
371+
}
345372
if (mpid) {
346373
allLocalStorageProducts = allLocalStorageProducts || {};
347374
allLocalStorageProducts[mpid] = currentUserProducts;
348375
try {
349-
window.localStorage.setItem(
350-
encodeURIComponent(mpInstance._Store.prodStorageName),
351-
Base64.encode(JSON.stringify(allLocalStorageProducts))
352-
);
376+
if (!mpInstance._Store.getNoTargeting()) {
377+
window.localStorage.setItem(
378+
encodeURIComponent(mpInstance._Store.prodStorageName),
379+
Base64.encode(JSON.stringify(allLocalStorageProducts))
380+
);
381+
}
353382
} catch (e) {
354383
mpInstance.Logger.error(
355384
'Error with setting products on localStorage.'
@@ -894,6 +923,9 @@ export default function _Persistence(mpInstance) {
894923
};
895924

896925
this.getCartProducts = function(mpid) {
926+
if (mpInstance._Store.getNoTargeting()) {
927+
return [];
928+
}
897929
var allCartProducts,
898930
cartProductsString = localStorage.getItem(
899931
mpInstance._Store.prodStorageName
@@ -916,6 +948,12 @@ export default function _Persistence(mpInstance) {
916948
if (!mpInstance._Store.isLocalStorageAvailable) {
917949
return;
918950
}
951+
if (mpInstance._Store.getNoTargeting()) {
952+
try {
953+
localStorage.removeItem(mpInstance._Store.prodStorageName);
954+
} catch (e) {}
955+
return;
956+
}
919957

920958
try {
921959
window.localStorage.setItem(

test/src/tests-persistence.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,65 @@ describe('persistence', () => {
16611661
parsedProductsAfter['testMPID'].cp.length.should.equal(0);
16621662
});
16631663

1664+
describe('products persistence with noTargeting privacy flag', () => {
1665+
beforeEach(() => {
1666+
mParticle._resetForTests(MPConfig);
1667+
});
1668+
1669+
it('should not save products LS when noTargeting is true', async () => {
1670+
mParticle.config.noTargeting = true;
1671+
1672+
mParticle.init(apiKey, mParticle.config);
1673+
await waitForCondition(hasIdentifyReturned);
1674+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1675+
expect(localStorageProducts).to.not.be.ok;
1676+
});
1677+
1678+
it('should clear existing products LS when noTargeting is true', async () => {
1679+
localStorage.setItem(
1680+
LocalStorageProductsV4WithWorkSpaceName,
1681+
btoa(JSON.stringify({ testMPID: { cp: [{ Name: 'x' }] } }))
1682+
);
1683+
mParticle.config.noTargeting = true;
1684+
mParticle.init(apiKey, mParticle.config);
1685+
await waitForCondition(hasIdentifyReturned);
1686+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1687+
expect(localStorageProducts).to.not.be.ok;
1688+
});
1689+
1690+
it('should save products LS when noTargeting is false', async () => {
1691+
mParticle.config.noTargeting = false;
1692+
1693+
mParticle.init(apiKey, mParticle.config);
1694+
await waitForCondition(hasIdentifyReturned);
1695+
const iphone = mParticle.eCommerce.createProduct(
1696+
'iphone',
1697+
'iphonesku',
1698+
599,
1699+
1
1700+
);
1701+
mParticle.eCommerce.Cart.add(iphone, true);
1702+
1703+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1704+
expect(localStorageProducts).to.be.ok;
1705+
});
1706+
1707+
it('should save products LS when noTargeting is default (false)', async () => {
1708+
mParticle.init(apiKey, mParticle.config);
1709+
await waitForCondition(hasIdentifyReturned);
1710+
const iphone = mParticle.eCommerce.createProduct(
1711+
'iphone',
1712+
'iphonesku',
1713+
599,
1714+
1
1715+
);
1716+
mParticle.eCommerce.Cart.add(iphone, true);
1717+
1718+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1719+
expect(localStorageProducts).to.be.ok;
1720+
});
1721+
});
1722+
16641723
it('should only set setFirstSeenTime() once', async () => {
16651724
const cookies = JSON.stringify({
16661725
gs: {

0 commit comments

Comments
 (0)