Skip to content

Commit

Permalink
Add processUserId callback to transform the userId such as for hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Oct 5, 2023
1 parent a5d93dc commit d6d01cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
15 changes: 9 additions & 6 deletions plugins/browser-plugin-focalmeter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface FocalMeterConfiguration {
kantarEndpoint: string;
/** Whether to store information about the last submitted user ID in local storage to prevent sending it again on next load (defaults not to use local storage) */
useLocalStorage?: boolean;
/** Callback to process user ID before sending it in a request. This may be used to apply hashing to the value. */
processUserId?: (userId: string) => string;
}

const _trackers: Record<string, BrowserTracker> = {};
Expand Down Expand Up @@ -77,21 +79,22 @@ export function FocalMeterPlugin(): BrowserPlugin {
return;
}

let newUserId = payload['duid'] as string;
let { kantarEndpoint, useLocalStorage } = _configurations[trackerId];
const newUserId = payload['duid'] as string;
const { kantarEndpoint, useLocalStorage, processUserId } = _configurations[trackerId];

if (!lastUserId && useLocalStorage && hasLocalStorage()) {
let key = getLocalStorageKey(trackerId);
const key = getLocalStorageKey(trackerId);
lastUserId = attemptGetLocalStorage(key);
}

if (newUserId && newUserId != lastUserId) {
lastUserId = newUserId;
const processedUserId = processUserId !== undefined ? processUserId(newUserId) : newUserId;

sendRequest(kantarEndpoint, newUserId, LOG, () => {
sendRequest(kantarEndpoint, processedUserId, LOG, () => {
// only write in local storage if the request succeeded
if (useLocalStorage && hasLocalStorage()) {
let key = getLocalStorageKey(trackerId);
const key = getLocalStorageKey(trackerId);
attemptWriteLocalStorage(key, newUserId);
}
});
Expand Down Expand Up @@ -141,7 +144,7 @@ function sendRequest(url: string, userId: string, LOG: Logger, successCallback:
}

function getKantarURL(url: string, userId: string): string {
let query: Record<string, string> = {
const query: Record<string, string> = {
vendor: 'snowplow',
cs_fpid: userId,
c12: 'not_set',
Expand Down
25 changes: 19 additions & 6 deletions plugins/browser-plugin-focalmeter/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,33 @@ describe('AdTrackingPlugin', () => {
});

it('makes a request to Kantar endpoint with user ID', async () => {
let tracker = createTrackerWithPlugin();
const tracker = createTrackerWithPlugin();
enableFocalMeterIntegration({ kantarEndpoint: domain });

tracker?.trackPageView();
let userId = tracker?.getDomainUserId();
const userId = tracker?.getDomainUserId();

await checkMock(() => {
expect(xhrOpenMock).toHaveBeenCalledTimes(2);
expect(xhrOpenMock).toHaveBeenLastCalledWith('GET', `${domain}?vendor=snowplow&cs_fpid=${userId}&c12=not_set`);
});
});

it('makes a request to Kantar endpoint with processed user ID', async () => {
const tracker = createTrackerWithPlugin();
enableFocalMeterIntegration({ kantarEndpoint: domain, processUserId: (userId) => userId + '-processed' });

tracker?.trackPageView();
const userId = tracker?.getDomainUserId();

await checkMock(() => {
expect(xhrOpenMock).toHaveBeenCalledTimes(2);
expect(xhrOpenMock).toHaveBeenLastCalledWith('GET', `${domain}?vendor=snowplow&cs_fpid=${userId}-processed&c12=not_set`);
});
});

it('makes a request to Kantar endpoint when user ID changes', async () => {
let tracker = createTrackerWithPlugin();
const tracker = createTrackerWithPlugin();
enableFocalMeterIntegration({ kantarEndpoint: domain });

// Doesn't make a request if anonymous tracking
Expand All @@ -86,7 +99,7 @@ describe('AdTrackingPlugin', () => {
// Makes a request when disabling anonymous tracking
tracker?.disableAnonymousTracking();
tracker?.trackPageView();
let userId = tracker?.getDomainUserId();
const userId = tracker?.getDomainUserId();
await checkMock(() => {
expect(xhrOpenMock).toHaveBeenCalledTimes(2);
expect(xhrOpenMock).toHaveBeenLastCalledWith('GET', `${domain}?vendor=snowplow&cs_fpid=${userId}&c12=not_set`);
Expand All @@ -100,8 +113,8 @@ describe('AdTrackingPlugin', () => {
});

it('can work with multiple trackers', async () => {
let tracker1 = createTrackerWithPlugin();
let tracker2 = createTrackerWithPlugin();
const tracker1 = createTrackerWithPlugin();
const tracker2 = createTrackerWithPlugin();

enableFocalMeterIntegration(
{
Expand Down

0 comments on commit d6d01cd

Please sign in to comment.