Skip to content

Commit

Permalink
PublinkIdSystem: Pass cached id through for better tracking (#10638)
Browse files Browse the repository at this point in the history
Co-authored-by: johwier <[email protected]>
  • Loading branch information
johnwier and johwier authored Nov 3, 2023
1 parent d74533b commit b4598fa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
42 changes: 25 additions & 17 deletions modules/publinkIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ const MODULE_NAME = 'publinkId';
const GVLID = 24;
const PUBLINK_COOKIE = '_publink';
const PUBLINK_S2S_COOKIE = '_publink_srv';
const PUBLINK_REQUEST_PATH = '/cvx/client/sync/publink';
const PUBLINK_REFRESH_PATH = '/cvx/client/sync/publink/refresh';

export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});

function isHex(s) {
return /^[A-F0-9]+$/i.test(s);
}

function publinkIdUrl(params, consentData) {
let url = parseUrl('https://proc.ad.cpe.dotomi.com/cvx/client/sync/publink');
function publinkIdUrl(params, consentData, storedId) {
let url = parseUrl('https://proc.ad.cpe.dotomi.com' + PUBLINK_REFRESH_PATH);
url.search = {
deh: params.e,
mpn: 'Prebid.js',
mpv: '$prebid.version$',
};
Expand All @@ -36,9 +37,21 @@ function publinkIdUrl(params, consentData) {
url.search.gdpr_consent = consentData.consentString;
}

if (params.site_id) { url.search.sid = params.site_id; }
if (params) {
if (params.e) {
// if there's an email parameter call the request path
url.search.deh = params.e;
url.pathname = PUBLINK_REQUEST_PATH;
}

if (params.site_id) { url.search.sid = params.site_id; }

if (params.api_key) { url.search.apikey = params.api_key; }
}

if (params.api_key) { url.search.apikey = params.api_key; }
if (storedId) {
url.search.publink = storedId;
}

const usPrivacyString = uspDataHandler.getConsentData();
if (usPrivacyString && typeof usPrivacyString === 'string') {
Expand All @@ -48,7 +61,7 @@ function publinkIdUrl(params, consentData) {
return buildUrl(url);
}

function makeCallback(config = {}, consentData) {
function makeCallback(config = {}, consentData, storedId) {
return function(prebidCallback) {
const options = {method: 'GET', withCredentials: true};
let handleResponse = function(responseText, xhr) {
Expand All @@ -59,15 +72,12 @@ function makeCallback(config = {}, consentData) {
}
}
};

if (config.params && config.params.e) {
if (isHex(config.params.e)) {
ajax(publinkIdUrl(config.params, consentData), handleResponse, undefined, options);
} else {
logError('params.e must be a hex string');
}
if ((config.params && config.params.e && isHex(config.params.e)) || storedId) {
ajax(publinkIdUrl(config.params, consentData, storedId), handleResponse, undefined, options);
} else if (config.params.e) {
logError('params.e must be a hex string');
}
};
}
}

function getlocalValue() {
Expand Down Expand Up @@ -137,9 +147,7 @@ export const publinkIdSubmodule = {
if (localValue) {
return {id: localValue};
}
if (!storedId) {
return {callback: makeCallback(config, consentData)};
}
return {callback: makeCallback(config, consentData, storedId)};
},
eids: {
'publinkId': {
Expand Down
43 changes: 38 additions & 5 deletions test/spec/modules/publinkIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,51 @@ describe('PublinkIdSystem', () => {
expect(result.callback).to.be.a('function');
});

it('Use local copy', () => {
const result = publinkIdSubmodule.getId({}, undefined, TEST_COOKIE_VALUE);
expect(result).to.be.undefined;
});

describe('callout for id', () => {
let callbackSpy = sinon.spy();

beforeEach(() => {
callbackSpy.resetHistory();
});

it('Has cached id', () => {
const config = {storage: {type: 'cookie'}};
let submoduleCallback = publinkIdSubmodule.getId(config, undefined, TEST_COOKIE_VALUE).callback;
submoduleCallback(callbackSpy);

const request = server.requests[0];
const parsed = parseUrl(request.url);

expect(parsed.hostname).to.equal('proc.ad.cpe.dotomi.com');
expect(parsed.pathname).to.equal('/cvx/client/sync/publink/refresh');
expect(parsed.search.mpn).to.equal('Prebid.js');
expect(parsed.search.mpv).to.equal('$prebid.version$');
expect(parsed.search.publink).to.equal(TEST_COOKIE_VALUE);

request.respond(200, {}, JSON.stringify(serverResponse));
expect(callbackSpy.calledOnce).to.be.true;
expect(callbackSpy.lastCall.lastArg).to.equal(serverResponse.publink);
});

it('Request path has priority', () => {
const config = {storage: {type: 'cookie'}, params: {e: 'ca11c0ca7', site_id: '102030'}};
let submoduleCallback = publinkIdSubmodule.getId(config, undefined, TEST_COOKIE_VALUE).callback;
submoduleCallback(callbackSpy);

const request = server.requests[0];
const parsed = parseUrl(request.url);

expect(parsed.hostname).to.equal('proc.ad.cpe.dotomi.com');
expect(parsed.pathname).to.equal('/cvx/client/sync/publink');
expect(parsed.search.mpn).to.equal('Prebid.js');
expect(parsed.search.mpv).to.equal('$prebid.version$');
expect(parsed.search.publink).to.equal(TEST_COOKIE_VALUE);

request.respond(200, {}, JSON.stringify(serverResponse));
expect(callbackSpy.calledOnce).to.be.true;
expect(callbackSpy.lastCall.lastArg).to.equal(serverResponse.publink);
});

it('Fetch with consent data', () => {
const config = {storage: {type: 'cookie'}, params: {e: 'ca11c0ca7', site_id: '102030'}};
const consentData = {gdprApplies: 1, consentString: 'myconsentstring'};
Expand Down

0 comments on commit b4598fa

Please sign in to comment.