Skip to content

Commit 5b9c22e

Browse files
Jaissica HoraJaissica Hora
authored andcommitted
feat: disable time tracking when noTargeting is set to true
1 parent 848ae7c commit 5b9c22e

2 files changed

Lines changed: 80 additions & 10 deletions

File tree

src/store.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -700,9 +700,20 @@ export default function Store(
700700
this.SDKConfig[baseUrlKeys] = baseUrls[baseUrlKeys];
701701
}
702702

703+
// Extract privacy flags directly from config into Store BEFORE initializing timers
704+
if (config.hasOwnProperty('noFunctional')) {
705+
this.setNoFunctional(config.noFunctional);
706+
}
707+
708+
if (config.hasOwnProperty('noTargeting')) {
709+
this.setNoTargeting(config.noTargeting);
710+
}
711+
703712
if (workspaceToken) {
704713
this.SDKConfig.workspaceToken = workspaceToken;
705-
mpInstance._timeOnSiteTimer = new ForegroundTimer(workspaceToken);
714+
if (!this.getNoTargeting()) {
715+
mpInstance._timeOnSiteTimer = new ForegroundTimer(workspaceToken);
716+
}
706717
} else {
707718
mpInstance.Logger.warning(
708719
'You should have a workspaceToken on your config object for security purposes.'
@@ -712,15 +723,6 @@ export default function Store(
712723
this.storageName = createMainStorageName(workspaceToken);
713724
this.prodStorageName = createProductStorageName(workspaceToken);
714725

715-
// Extract privacy flags directly from config into Store
716-
if (config.hasOwnProperty('noFunctional')) {
717-
this.setNoFunctional(config.noFunctional);
718-
}
719-
720-
if (config.hasOwnProperty('noTargeting')) {
721-
this.setNoTargeting(config.noTargeting);
722-
}
723-
724726
this.SDKConfig.requiredWebviewBridgeName =
725727
requiredWebviewBridgeName || workspaceToken;
726728

test/jest/foregroundTimeTracker.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import ForegroundTimeTracker from '../../src/foregroundTimeTracker';
2+
import Store, { IStore } from '../../src/store';
3+
import { SDKInitConfig } from '../../src/sdkRuntimeModels';
4+
import { IMParticleWebSDKInstance } from '../../src/mp-instance';
25

36
describe('ForegroundTimeTracker', () => {
47
let foregroundTimeTracker: ForegroundTimeTracker;
@@ -490,4 +493,69 @@ describe('ForegroundTimeTracker', () => {
490493
expect(updatePersistenceSpy).toHaveBeenCalled();
491494
});
492495
});
496+
497+
describe('#noTargeting', () => {
498+
const workspaceToken = 'abcdef';
499+
const tosKey = `mprtcl-tos-${workspaceToken}`;
500+
let mockMPInstance: IMParticleWebSDKInstance;
501+
let store: IStore;
502+
503+
beforeEach(() => {
504+
jest.useFakeTimers({ now: Date.now(), advanceTimers: true });
505+
localStorage.clear();
506+
507+
mockMPInstance = {
508+
_Helpers: {
509+
createMainStorageName: () => `mprtcl-v4_${workspaceToken}`,
510+
createProductStorageName: () => `mprtcl-prodv4_${workspaceToken}`,
511+
Validators: { isFunction: () => true },
512+
extend: Object.assign,
513+
},
514+
_NativeSdkHelpers: { isWebviewEnabled: () => false },
515+
_Persistence: {
516+
update: jest.fn(),
517+
savePersistence: jest.fn(),
518+
getPersistence: () => ({ gs: {} }),
519+
},
520+
Logger: { verbose: jest.fn(), warning: jest.fn(), error: jest.fn() },
521+
Identity: { getCurrentUser: () => ({ getMPID: () => 'mpid' }) },
522+
_timeOnSiteTimer: undefined as any,
523+
} as unknown as IMParticleWebSDKInstance;
524+
525+
store = {} as IStore;
526+
(Store as any).call(store, {} as SDKInitConfig, mockMPInstance, 'apikey');
527+
mockMPInstance._Store = store;
528+
});
529+
530+
afterEach(() => {
531+
jest.useRealTimers();
532+
localStorage.clear();
533+
});
534+
535+
it('should initialize timer when noTargeting is default (false)', () => {
536+
store.processConfig({ workspaceToken } as SDKInitConfig);
537+
expect(mockMPInstance._timeOnSiteTimer).toBeDefined();
538+
if (mockMPInstance._timeOnSiteTimer) {
539+
jest.advanceTimersByTime(1000);
540+
mockMPInstance._timeOnSiteTimer.getTimeInForeground();
541+
}
542+
expect(localStorage.getItem(tosKey)).not.toBeNull();
543+
});
544+
545+
it('should NOT initialize timer when noTargeting is true', () => {
546+
store.processConfig({ workspaceToken, noTargeting: true } as SDKInitConfig);
547+
expect(mockMPInstance._timeOnSiteTimer).toBeUndefined();
548+
expect(localStorage.getItem(tosKey)).toBeNull();
549+
});
550+
551+
it('should initialize timer when noTargeting is false', () => {
552+
store.processConfig({ workspaceToken, noTargeting: false } as SDKInitConfig);
553+
expect(mockMPInstance._timeOnSiteTimer).toBeDefined();
554+
if (mockMPInstance._timeOnSiteTimer) {
555+
jest.advanceTimersByTime(1000);
556+
mockMPInstance._timeOnSiteTimer.getTimeInForeground();
557+
}
558+
expect(localStorage.getItem(tosKey)).not.toBeNull();
559+
});
560+
});
493561
});

0 commit comments

Comments
 (0)