Skip to content

Commit 43345a3

Browse files
Jaissica HoraJaissica Hora
authored andcommitted
feat: disable product cookie when no targeting is set
1 parent 848ae7c commit 43345a3

3 files changed

Lines changed: 191 additions & 17 deletions

File tree

src/persistence.js

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,27 @@ export default function _Persistence(mpInstance) {
8989
// https://go.mparticle.com/work/SQDSDKS-6048
9090
try {
9191
if (mpInstance._Store.isLocalStorageAvailable) {
92-
var encodedProducts = localStorage.getItem(
93-
mpInstance._Store.prodStorageName
94-
);
95-
96-
if (encodedProducts) {
97-
var decodedProducts = JSON.parse(
98-
Base64.decode(encodedProducts)
92+
if (mpInstance._Store.getNoTargeting()) {
93+
localStorage.removeItem(
94+
mpInstance._Store.prodStorageName
9995
);
100-
}
101-
if (mpInstance._Store.mpid) {
102-
self.storeProductsInMemory(
103-
decodedProducts,
104-
mpInstance._Store.mpid
96+
mpInstance._Store.cartProducts = [];
97+
} else {
98+
var encodedProducts = localStorage.getItem(
99+
mpInstance._Store.prodStorageName
105100
);
101+
102+
if (encodedProducts) {
103+
var decodedProducts = JSON.parse(
104+
Base64.decode(encodedProducts)
105+
);
106+
}
107+
if (mpInstance._Store.mpid) {
108+
self.storeProductsInMemory(
109+
decodedProducts,
110+
mpInstance._Store.mpid
111+
);
112+
}
106113
}
107114
}
108115
} catch (e) {
@@ -267,7 +274,10 @@ export default function _Persistence(mpInstance) {
267274
};
268275

269276
this.getUserProductsFromLS = function(mpid) {
270-
if (!mpInstance._Store.isLocalStorageAvailable) {
277+
if (
278+
mpInstance._Store.getNoTargeting() ||
279+
!mpInstance._Store.isLocalStorageAvailable
280+
) {
271281
return [];
272282
}
273283

@@ -306,6 +316,15 @@ export default function _Persistence(mpInstance) {
306316
};
307317

308318
this.getAllUserProductsFromLS = function() {
319+
if (mpInstance._Store.getNoTargeting()) {
320+
var currentUser = mpInstance.Identity.getCurrentUser();
321+
var mpid = currentUser ? currentUser.getMPID() : null;
322+
var result = {};
323+
if (mpid) {
324+
result[mpid] = { cp: [] };
325+
}
326+
return result;
327+
}
309328
var decodedProducts,
310329
encodedProducts = localStorage.getItem(
311330
mpInstance._Store.prodStorageName
@@ -340,14 +359,24 @@ export default function _Persistence(mpInstance) {
340359
? allLocalStorageProducts[mpid].cp
341360
: [],
342361
};
362+
363+
if (mpInstance._Store.getNoTargeting()) {
364+
try {
365+
window.localStorage.removeItem(
366+
mpInstance._Store.prodStorageName
367+
);
368+
} catch (e) {}
369+
}
343370
if (mpid) {
344371
allLocalStorageProducts = allLocalStorageProducts || {};
345372
allLocalStorageProducts[mpid] = currentUserProducts;
346373
try {
347-
window.localStorage.setItem(
348-
encodeURIComponent(mpInstance._Store.prodStorageName),
349-
Base64.encode(JSON.stringify(allLocalStorageProducts))
350-
);
374+
if (!mpInstance._Store.getNoTargeting()) {
375+
window.localStorage.setItem(
376+
encodeURIComponent(mpInstance._Store.prodStorageName),
377+
Base64.encode(JSON.stringify(allLocalStorageProducts))
378+
);
379+
}
351380
} catch (e) {
352381
mpInstance.Logger.error(
353382
'Error with setting products on localStorage.'
@@ -890,6 +919,9 @@ export default function _Persistence(mpInstance) {
890919
};
891920

892921
this.getCartProducts = function(mpid) {
922+
if (mpInstance._Store.getNoTargeting()) {
923+
return [];
924+
}
893925
var allCartProducts,
894926
cartProductsString = localStorage.getItem(
895927
mpInstance._Store.prodStorageName
@@ -912,6 +944,12 @@ export default function _Persistence(mpInstance) {
912944
if (!mpInstance._Store.isLocalStorageAvailable) {
913945
return;
914946
}
947+
if (mpInstance._Store.getNoTargeting()) {
948+
try {
949+
localStorage.removeItem(mpInstance._Store.prodStorageName);
950+
} catch (e) {}
951+
return;
952+
}
915953

916954
try {
917955
window.localStorage.setItem(
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {
2+
LocalStorageProductsV4WithWorkSpaceName as PRODUCTS_KEY,
3+
mParticle,
4+
MPConfig,
5+
} from '../src/config/constants';
6+
import { deleteAllCookies } from './utils';
7+
import type { IMParticleWebSDKInstance } from '../../src/mp-instance';
8+
9+
type MPInstForProducts = IMParticleWebSDKInstance & {
10+
_Store: { isLocalStorageAvailable: boolean; prodStorageName: string; getNoTargeting?: () => boolean };
11+
_Persistence: {
12+
setLocalStorage(): void;
13+
setCartProducts(a: Record<string, unknown>): void;
14+
};
15+
};
16+
17+
function getMPInstance(): MPInstForProducts {
18+
const mpInstance = (mParticle as unknown as { getInstance(): MPInstForProducts }).getInstance();
19+
mpInstance._Store.isLocalStorageAvailable = true;
20+
mpInstance._Store.prodStorageName = PRODUCTS_KEY;
21+
return mpInstance;
22+
}
23+
24+
function buildProducts(mpid: string): Record<string, unknown> {
25+
return { [mpid]: { cp: [{ Name: 'x' }] } } as Record<string, unknown>;
26+
}
27+
28+
describe('Product Persistence', () => {
29+
describe('#noTargeting', () => {
30+
beforeEach(() => {
31+
mParticle._resetForTests(MPConfig);
32+
deleteAllCookies();
33+
localStorage.clear();
34+
});
35+
36+
afterEach(() => {
37+
localStorage.clear();
38+
deleteAllCookies();
39+
});
40+
41+
it('should not write mprtcl-prodv4 key on init when noTargeting is true', () => {
42+
const mpInstance = getMPInstance();
43+
mpInstance._Store.getNoTargeting = () => true;
44+
mpInstance._Persistence.setLocalStorage();
45+
expect(localStorage.getItem(PRODUCTS_KEY)).toBeNull();
46+
expect(localStorage.getItem(encodeURIComponent(PRODUCTS_KEY))).toBeNull();
47+
});
48+
49+
it('should remove existing mprtcl-prodv4 key on init when noTargeting is true', () => {
50+
localStorage.setItem(
51+
PRODUCTS_KEY,
52+
btoa(JSON.stringify({ testMPID: { cp: [{ Name: 'x' }] } }))
53+
);
54+
const mpInstance = getMPInstance();
55+
mpInstance._Store.getNoTargeting = () => true;
56+
mpInstance._Persistence.setLocalStorage();
57+
expect(localStorage.getItem(PRODUCTS_KEY)).toBeNull();
58+
expect(localStorage.getItem(encodeURIComponent(PRODUCTS_KEY))).toBeNull();
59+
});
60+
61+
it('should write mprtcl-prodv4 key when noTargeting is false', () => {
62+
const mpid = 'testMPID';
63+
const mpInstance = getMPInstance();
64+
mpInstance._Store.getNoTargeting = () => false;
65+
mpInstance._Persistence.setCartProducts(buildProducts(mpid));
66+
expect(localStorage.getItem(PRODUCTS_KEY)).toBeTruthy();
67+
});
68+
69+
it('should write mprtcl-prodv4 key when noTargeting is default (false)', () => {
70+
const mpid = 'testMPID';
71+
const mpInstance = getMPInstance();
72+
mpInstance._Persistence.setCartProducts(buildProducts(mpid));
73+
expect(localStorage.getItem(PRODUCTS_KEY)).toBeTruthy();
74+
});
75+
});
76+
});
77+

test/src/tests-persistence.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,65 @@ describe('persistence', () => {
18281828
});
18291829
});
18301830

1831+
describe('products persistence with noTargeting', () => {
1832+
beforeEach(() => {
1833+
mParticle._resetForTests(MPConfig);
1834+
});
1835+
1836+
it('should not write products key when noTargeting is true', async () => {
1837+
mParticle.config.noTargeting = true;
1838+
1839+
mParticle.init(apiKey, mParticle.config);
1840+
await waitForCondition(hasIdentifyReturned);
1841+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1842+
expect(localStorageProducts).to.not.be.ok;
1843+
});
1844+
1845+
it('should clear existing products key on init when noTargeting is true', async () => {
1846+
localStorage.setItem(
1847+
LocalStorageProductsV4WithWorkSpaceName,
1848+
btoa(JSON.stringify({ testMPID: { cp: [{ Name: 'x' }] } }))
1849+
);
1850+
mParticle.config.noTargeting = true;
1851+
mParticle.init(apiKey, mParticle.config);
1852+
await waitForCondition(hasIdentifyReturned);
1853+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1854+
expect(localStorageProducts).to.not.be.ok;
1855+
});
1856+
1857+
it('should write products key when noTargeting is false', async () => {
1858+
mParticle.config.noTargeting = false;
1859+
1860+
mParticle.init(apiKey, mParticle.config);
1861+
await waitForCondition(hasIdentifyReturned);
1862+
const iphone = mParticle.eCommerce.createProduct(
1863+
'iphone',
1864+
'iphonesku',
1865+
599,
1866+
1
1867+
);
1868+
mParticle.eCommerce.Cart.add(iphone, true);
1869+
1870+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1871+
expect(localStorageProducts).to.be.ok;
1872+
});
1873+
1874+
it('should write products key when noTargeting is default (false)', async () => {
1875+
mParticle.init(apiKey, mParticle.config);
1876+
await waitForCondition(hasIdentifyReturned);
1877+
const iphone = mParticle.eCommerce.createProduct(
1878+
'iphone',
1879+
'iphonesku',
1880+
599,
1881+
1
1882+
);
1883+
mParticle.eCommerce.Cart.add(iphone, true);
1884+
1885+
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1886+
expect(localStorageProducts).to.be.ok;
1887+
});
1888+
});
1889+
18311890
it('should only set setFirstSeenTime() once', done => {
18321891
const cookies = JSON.stringify({
18331892
gs: {

0 commit comments

Comments
 (0)