From d6d01cd278f00f68d162990e7fa793de08a39b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matu=CC=81s=CC=8C=20Tomlein?= Date: Mon, 10 Jul 2023 15:56:39 +0200 Subject: [PATCH] Add processUserId callback to transform the userId such as for hashing --- .../browser-plugin-focalmeter/src/index.ts | 15 ++++++----- .../test/request.test.ts | 25 ++++++++++++++----- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/plugins/browser-plugin-focalmeter/src/index.ts b/plugins/browser-plugin-focalmeter/src/index.ts index ee77fd133..ef2c290f8 100644 --- a/plugins/browser-plugin-focalmeter/src/index.ts +++ b/plugins/browser-plugin-focalmeter/src/index.ts @@ -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 = {}; @@ -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); } }); @@ -141,7 +144,7 @@ function sendRequest(url: string, userId: string, LOG: Logger, successCallback: } function getKantarURL(url: string, userId: string): string { - let query: Record = { + const query: Record = { vendor: 'snowplow', cs_fpid: userId, c12: 'not_set', diff --git a/plugins/browser-plugin-focalmeter/test/request.test.ts b/plugins/browser-plugin-focalmeter/test/request.test.ts index 0b62a978a..40ec88ecd 100644 --- a/plugins/browser-plugin-focalmeter/test/request.test.ts +++ b/plugins/browser-plugin-focalmeter/test/request.test.ts @@ -60,11 +60,11 @@ 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); @@ -72,8 +72,21 @@ describe('AdTrackingPlugin', () => { }); }); + 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 @@ -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`); @@ -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( {