Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kargo Bid Adapter: add gdpr support #6447

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions modules/kargoBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import {config} from '../src/config.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';

const storage = getStorageManager();
const BIDDER_CODE = 'kargo';
const HOST = 'https://krk.kargo.com';
const SYNC = 'https://crb.kargo.com/api/v1/initsyncrnd/{UUID}?seed={SEED}&idx={INDEX}';
const SYNC = 'https://crb.kargo.com/api/v1/initsyncrnd/{UUID}?seed={SEED}&idx={INDEX}&gdpr={GDPR}&gdpr_consent={GDPR_CONSENT}&us_privacy={US_PRIVACY}';
const SYNC_COUNT = 5;
const GVLID = 972;
const storage = getStorageManager(GVLID, BIDDER_CODE);

let sessionId,
lastPageUrl,
requestCounter;

export const spec = {
gvlid: GVLID,
code: BIDDER_CODE,
isBidRequestValid: function(bid) {
if (!bid || !bid.params) {
Expand Down Expand Up @@ -48,7 +50,7 @@ export const spec = {
bidIDs: bidIds,
bidSizes: bidSizes,
prebidRawBidRequests: validBidRequests
}, spec._getAllMetadata(tdid, bidderRequest.uspConsent));
}, spec._getAllMetadata(tdid, bidderRequest.uspConsent, bidderRequest.gdprConsent));
const encodedParams = encodeURIComponent(JSON.stringify(transformedParams));
return Object.assign({}, bidderRequest, {
method: 'GET',
Expand Down Expand Up @@ -85,15 +87,25 @@ export const spec = {
}
return bidResponses;
},
getUserSyncs: function(syncOptions) {
getUserSyncs: function(syncOptions, responses, gdprConsent, usPrivacy) {
const syncs = [];
const seed = spec._generateRandomUuid();
const clientId = spec._getClientId();
var gdpr = (gdprConsent && gdprConsent.gdprApplies) ? 1 : 0;
var gdprConsentString = (gdprConsent && gdprConsent.consentString) ? gdprConsent.consentString : '';
// don't sync if opted out via usPrivacy
if (typeof usPrivacy == 'string' && usPrivacy.length == 4 && usPrivacy[0] == 1 && usPrivacy[2] == 'Y') {
return syncs;
}
if (syncOptions.iframeEnabled && seed && clientId) {
for (let i = 0; i < SYNC_COUNT; i++) {
syncs.push({
type: 'iframe',
url: SYNC.replace('{UUID}', clientId).replace('{SEED}', seed).replace('{INDEX}', i)
url: SYNC.replace('{UUID}', clientId).replace('{SEED}', seed)
.replace('{INDEX}', i)
.replace('{GDPR}', gdpr)
.replace('{GDPR_CONSENT}', gdprConsentString)
.replace('{US_PRIVACY}', usPrivacy || '')
});
}
}
Expand Down Expand Up @@ -183,7 +195,7 @@ export const spec = {
}
},

_getUserIds(tdid, usp) {
_getUserIds(tdid, usp, gdpr) {
const crb = spec._getCrb();
const userIds = {
kargoID: crb.userId,
Expand All @@ -192,6 +204,16 @@ export const spec = {
optOut: crb.optOut,
usp: usp
};

try {
if (gdpr) {
userIds['gdpr'] = {
consent: gdpr.consentString || '',
applies: !!gdpr.gdprApplies,
}
}
} catch (e) {
}
if (tdid) {
userIds.tdID = tdid;
}
Expand All @@ -203,9 +225,9 @@ export const spec = {
return crb.clientId;
},

_getAllMetadata(tdid, usp) {
_getAllMetadata(tdid, usp, gdpr) {
return {
userIDs: spec._getUserIds(tdid, usp),
userIDs: spec._getUserIds(tdid, usp, gdpr),
krux: spec._getKrux(),
pageURL: window.location.href,
rawCRB: spec._readCookie('krg_crb'),
Expand Down
65 changes: 56 additions & 9 deletions test/spec/modules/kargoBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ describe('kargo adapter tests', function () {
noAdServerCurrency = true;
}

function generateGDPR(applies, haveConsent) {
var data = {
consentString: 'gdprconsentstring',
gdprApplies: applies,
};
return data;
}

function generateGDPRExpect(applies, haveConsent) {
return {
consent: 'gdprconsentstring',
applies: applies,
};
}

function initializeKruxUser() {
setLocalStorageItem('kxkar_user', 'rsgr9pnij');
}
Expand Down Expand Up @@ -221,7 +236,7 @@ describe('kargo adapter tests', function () {
return spec._getSessionId();
}

function getExpectedKrakenParams(excludeUserIds, excludeKrux, expectedRawCRB, expectedRawCRBCookie) {
function getExpectedKrakenParams(excludeUserIds, excludeKrux, expectedRawCRB, expectedRawCRBCookie, expectedGDPR) {
var base = {
timeout: 200,
requestCount: requestCount++,
Expand Down Expand Up @@ -299,6 +314,10 @@ describe('kargo adapter tests', function () {
rawCRBLocalStorage: expectedRawCRB
};

if (expectedGDPR) {
base.userIDs['gdpr'] = expectedGDPR;
}

if (excludeUserIds === true) {
base.userIDs = {
crbIDs: {},
Expand All @@ -317,12 +336,16 @@ describe('kargo adapter tests', function () {
return base;
}

function testBuildRequests(excludeTdid, expected) {
function testBuildRequests(excludeTdid, expected, gdpr) {
var clonedBids = JSON.parse(JSON.stringify(bids));
if (excludeTdid) {
delete clonedBids[0].userId.tdid;
}
var request = spec.buildRequests(clonedBids, {timeout: 200, uspConsent: '1---', foo: 'bar'});
var payload = { timeout: 200, uspConsent: '1---', foo: 'bar' };
if (gdpr) {
payload['gdprConsent'] = gdpr
}
var request = spec.buildRequests(clonedBids, payload);
expected.sessionId = getSessionId();
sessionIds.push(expected.sessionId);
var krakenParams = JSON.parse(decodeURIComponent(request.data.slice(5)));
Expand Down Expand Up @@ -431,6 +454,15 @@ describe('kargo adapter tests', function () {
initializeKrgCrb();
testBuildRequests(false, getExpectedKrakenParams(undefined, undefined, getKrgCrb(), getKrgCrbOldStyle()));
});

it('sends gdpr consent', function () {
initializeKruxUser();
initializeKruxSegments();
initializeKrgCrb();
testBuildRequests(false, getExpectedKrakenParams(undefined, undefined, getKrgCrb(), getKrgCrbOldStyle(), generateGDPRExpect(true, true)), generateGDPR(true, true));
testBuildRequests(false, getExpectedKrakenParams(undefined, undefined, getKrgCrb(), getKrgCrbOldStyle(), generateGDPRExpect(false, true)), generateGDPR(false, true));
testBuildRequests(false, getExpectedKrakenParams(undefined, undefined, getKrgCrb(), getKrgCrbOldStyle(), generateGDPRExpect(false, false)), generateGDPR(false, false));
});
});

describe('response handler', function() {
Expand Down Expand Up @@ -558,8 +590,8 @@ describe('kargo adapter tests', function () {
});
});

function getUserSyncsWhenAllowed() {
return spec.getUserSyncs({iframeEnabled: true});
function getUserSyncsWhenAllowed(gdprConsent, usPrivacy) {
return spec.getUserSyncs({iframeEnabled: true}, null, gdprConsent, usPrivacy);
}

function getUserSyncsWhenForbidden() {
Expand All @@ -574,17 +606,17 @@ describe('kargo adapter tests', function () {
shouldSimulateOutdatedBrowser = true;
}

function getSyncUrl(index) {
function getSyncUrl(index, gdprApplies, gdprConsentString, usPrivacy) {
return {
type: 'iframe',
url: `https://crb.kargo.com/api/v1/initsyncrnd/${clientId}?seed=3205e885-8d37-4139-b47e-f82cff268000&idx=${index}`
url: `https://crb.kargo.com/api/v1/initsyncrnd/${clientId}?seed=3205e885-8d37-4139-b47e-f82cff268000&idx=${index}&gdpr=${gdprApplies}&gdpr_consent=${gdprConsentString}&us_privacy=${usPrivacy}`
};
}

function getSyncUrls() {
function getSyncUrls(gdprApplies, gdprConsentString, usPrivacy) {
var syncs = [];
for (var i = 0; i < 5; i++) {
syncs[i] = getSyncUrl(i);
syncs[i] = getSyncUrl(i, gdprApplies || 0, gdprConsentString || '', usPrivacy || '');
}
return syncs;
}
Expand All @@ -606,6 +638,21 @@ describe('kargo adapter tests', function () {
safelyRun(() => expect(getUserSyncsWhenAllowed()).to.be.an('array').that.is.empty);
});

it('no user syncs when there is no us privacy consent', function() {
turnOnClientId();
safelyRun(() => expect(getUserSyncsWhenAllowed(null, '1YYY')).to.be.an('array').that.is.empty);
});

it('pass through us privacy consent', function() {
turnOnClientId();
safelyRun(() => expect(getUserSyncsWhenAllowed(null, '1YNY')).to.deep.equal(getSyncUrls(0, '', '1YNY')));
});

it('pass through gdpr consent', function() {
turnOnClientId();
safelyRun(() => expect(getUserSyncsWhenAllowed({ gdprApplies: true, consentString: 'consentstring' })).to.deep.equal(getSyncUrls(1, 'consentstring', '')));
});

it('no user syncs when there is outdated browser', function() {
turnOnClientId();
simulateOutdatedBrowser();
Expand Down