Skip to content

Commit 2679ab9

Browse files
Jaissica Horajaissica12
authored andcommitted
feat: disable time tracking when noTargeting is set to true
1 parent 7548f0b commit 2679ab9

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
@@ -728,9 +728,20 @@ export default function Store(
728728
this.SDKConfig[baseUrlKeys] = baseUrls[baseUrlKeys];
729729
}
730730

731+
// Extract privacy flags directly from config into Store BEFORE initializing timers
732+
if (config.hasOwnProperty('noFunctional')) {
733+
this.setNoFunctional(config.noFunctional);
734+
}
735+
736+
if (config.hasOwnProperty('noTargeting')) {
737+
this.setNoTargeting(config.noTargeting);
738+
}
739+
731740
if (workspaceToken) {
732741
this.SDKConfig.workspaceToken = workspaceToken;
733-
mpInstance._timeOnSiteTimer = new ForegroundTimer(workspaceToken);
742+
if (!this.getNoTargeting()) {
743+
mpInstance._timeOnSiteTimer = new ForegroundTimer(workspaceToken);
744+
}
734745
} else {
735746
mpInstance.Logger.warning(
736747
'You should have a workspaceToken on your config object for security purposes.'
@@ -740,15 +751,6 @@ export default function Store(
740751
this.storageName = createMainStorageName(workspaceToken);
741752
this.prodStorageName = createProductStorageName(workspaceToken);
742753

743-
// Extract privacy flags directly from config into Store
744-
if (config.hasOwnProperty('noFunctional')) {
745-
this.setNoFunctional(config.noFunctional);
746-
}
747-
748-
if (config.hasOwnProperty('noTargeting')) {
749-
this.setNoTargeting(config.noTargeting);
750-
}
751-
752754
this.SDKConfig.requiredWebviewBridgeName =
753755
requiredWebviewBridgeName || workspaceToken;
754756

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)