Skip to content

Commit

Permalink
VIS.X: add first-party id (#10162)
Browse files Browse the repository at this point in the history
  • Loading branch information
vfedoseev authored Aug 9, 2023
1 parent c553226 commit 543544c
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 2 deletions.
52 changes: 51 additions & 1 deletion modules/visxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { INSTREAM as VIDEO_INSTREAM } from '../src/video.js';
import {getStorageManager} from '../src/storageManager.js';
const BIDDER_CODE = 'visx';
const GVLID = 154;
const BASE_URL = 'https://t.visx.net';
Expand All @@ -29,6 +30,7 @@ const LOG_ERROR_MESS = {
videoMissing: 'Bid request videoType property is missing - '
};
const currencyWhiteList = ['EUR', 'USD', 'GBP', 'PLN'];
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
Expand Down Expand Up @@ -117,10 +119,13 @@ export const spec = {
...(payloadSchain && { schain: payloadSchain })
}
};

const vads = _getUserId();
const user = {
ext: {
...(payloadUserEids && { eids: payloadUserEids }),
...(payload.gdpr_consent && { consent: payload.gdpr_consent })
...(payload.gdpr_consent && { consent: payload.gdpr_consent }),
...(vads && { vads })
}
};
const regs = ('gdpr_applies' in payload) && {
Expand Down Expand Up @@ -382,4 +387,49 @@ function _isAdSlotExists(adUnitCode) {
return false;
}

// Generate user id (25 chars) with NanoID
// https://github.com/ai/nanoid/
function _generateUserId() {
for (
var t = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict',
e = new Date().getTime() % 1073741824,
i = '',
o = 0;
o < 5;
o++
) {
i += t[e % 64];
e = Math.floor(e / 64);
}
for (o = 20; o--;) i += t[(64 * Math.random()) | 0];
return i;
}

function _getUserId() {
const USER_ID_KEY = '__vads';
let vads;

if (storage.cookiesAreEnabled()) {
vads = storage.getCookie(USER_ID_KEY);
} else if (storage.localStorageIsEnabled()) {
vads = storage.getDataFromLocalStorage(USER_ID_KEY);
}

if (vads && vads.length) {
return vads;
}

vads = _generateUserId();
if (storage.cookiesAreEnabled()) {
const expires = new Date(Date.now() + 2592e6).toUTCString();
storage.setCookie(USER_ID_KEY, vads, expires);
return vads;
} else if (storage.localStorageIsEnabled()) {
storage.setDataInLocalStorage(USER_ID_KEY, vads);
return vads;
}

return null;
}

registerBidder(spec);
152 changes: 151 additions & 1 deletion test/spec/modules/visxBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { spec } from 'modules/visxBidAdapter.js';
import { spec, storage } from 'modules/visxBidAdapter.js';
import { config } from 'src/config.js';
import { newBidder } from 'src/adapters/bidderFactory.js';
import * as utils from 'src/utils.js';
Expand Down Expand Up @@ -82,6 +82,9 @@ describe('VisxAdapter', function () {
});
return res;
}

let cookiesAreEnabledStub, localStorageIsEnabledStub;

const bidderRequest = {
timeout: 3000,
refererInfo: {
Expand Down Expand Up @@ -180,6 +183,24 @@ describe('VisxAdapter', function () {
'ext': {'bidder': {'uid': 903537}}
}];

before(() => {
$$PREBID_GLOBAL$$.bidderSettings = {
visx: {
storageAllowed: false
}
};
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
cookiesAreEnabledStub = sinon.stub(storage, 'cookiesAreEnabled');
localStorageIsEnabledStub.returns(false);
cookiesAreEnabledStub.returns(false);
});

after(() => {
localStorageIsEnabledStub.restore();
cookiesAreEnabledStub.restore();
$$PREBID_GLOBAL$$.bidderSettings = {};
});

it('should attach valid params to the tag', function () {
const firstBid = bidRequests[0];
const bids = [firstBid];
Expand Down Expand Up @@ -422,6 +443,7 @@ describe('VisxAdapter', function () {
});
return res;
}
let cookiesAreEnabledStub, localStorageIsEnabledStub;
const bidderRequest = {
timeout: 3000,
refererInfo: {
Expand Down Expand Up @@ -449,6 +471,24 @@ describe('VisxAdapter', function () {
}
];

before(() => {
$$PREBID_GLOBAL$$.bidderSettings = {
visx: {
storageAllowed: false
}
};
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
cookiesAreEnabledStub = sinon.stub(storage, 'cookiesAreEnabled');
localStorageIsEnabledStub.returns(false);
cookiesAreEnabledStub.returns(false);
});

after(() => {
localStorageIsEnabledStub.restore();
cookiesAreEnabledStub.restore();
$$PREBID_GLOBAL$$.bidderSettings = {};
});

it('should send requst for banner bid', function () {
const request = spec.buildRequests([bidRequests[0]], bidderRequest);
const payload = parseRequest(request.url);
Expand Down Expand Up @@ -486,6 +526,7 @@ describe('VisxAdapter', function () {
});
return res;
}
let cookiesAreEnabledStub, localStorageIsEnabledStub;
const bidderRequest = {
timeout: 3000,
refererInfo: {
Expand Down Expand Up @@ -529,10 +570,23 @@ describe('VisxAdapter', function () {
documentStub.withArgs('visx-adunit-element-2').returns({
id: 'visx-adunit-element-2'
});

$$PREBID_GLOBAL$$.bidderSettings = {
visx: {
storageAllowed: false
}
};
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
cookiesAreEnabledStub = sinon.stub(storage, 'cookiesAreEnabled');
localStorageIsEnabledStub.returns(false);
cookiesAreEnabledStub.returns(false);
});

after(function() {
sandbox.restore();
localStorageIsEnabledStub.restore();
cookiesAreEnabledStub.restore();
$$PREBID_GLOBAL$$.bidderSettings = {};
});

it('should find ad slot by ad unit code as element id', function () {
Expand Down Expand Up @@ -1323,4 +1377,100 @@ describe('VisxAdapter', function () {
expect(query).to.deep.equal({});
});
});

describe('first party user id', function () {
const USER_ID_KEY = '__vads';
const USER_ID_DUMMY_VALUE_COOKIE = 'dummy_id_cookie';
const USER_ID_DUMMY_VALUE_LOCAL_STORAGE = 'dummy_id_local_storage';

let getDataFromLocalStorageStub, localStorageIsEnabledStub;
let getCookieStub, cookiesAreEnabledStub;

const bidRequests = [
{
'bidder': 'visx',
'params': {
'uid': 903535
},
'adUnitCode': 'adunit-code-1',
'sizes': [[300, 250], [300, 600]],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
'auctionId': '1d1a030790a475',
}
];
const bidderRequest = {
timeout: 3000,
refererInfo: {
page: 'https://example.com'
}
};

beforeEach(() => {
$$PREBID_GLOBAL$$.bidderSettings = {
visx: {
storageAllowed: true
}
};
cookiesAreEnabledStub = sinon.stub(storage, 'cookiesAreEnabled');
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
});

afterEach(() => {
cookiesAreEnabledStub.restore();
localStorageIsEnabledStub.restore();
getCookieStub && getCookieStub.restore();
getDataFromLocalStorageStub && getDataFromLocalStorageStub.restore();
$$PREBID_GLOBAL$$.bidderSettings = {};
});

it('should not pass user id if both cookies and local storage are not available', function () {
cookiesAreEnabledStub.returns(false);
localStorageIsEnabledStub.returns(false);

const request = spec.buildRequests(bidRequests, bidderRequest);

expect(request.data.user).to.be.undefined;
});

it('should get user id from cookie if available', function () {
cookiesAreEnabledStub.returns(true);
localStorageIsEnabledStub.returns(false);
getCookieStub = sinon.stub(storage, 'getCookie');
getCookieStub.withArgs(USER_ID_KEY).returns(USER_ID_DUMMY_VALUE_COOKIE);

const request = spec.buildRequests(bidRequests, bidderRequest);

expect(request.data.user.ext.vads).to.equal(USER_ID_DUMMY_VALUE_COOKIE);
});

it('should get user id from local storage if available', function () {
cookiesAreEnabledStub.returns(false);
localStorageIsEnabledStub.returns(true);
getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
getDataFromLocalStorageStub.withArgs(USER_ID_KEY).returns(USER_ID_DUMMY_VALUE_LOCAL_STORAGE);

const request = spec.buildRequests(bidRequests, bidderRequest);

expect(request.data.user.ext.vads).to.equal(USER_ID_DUMMY_VALUE_LOCAL_STORAGE);
});

it('should create user id and store it in cookies (if user id does not exist)', function () {
cookiesAreEnabledStub.returns(true);
localStorageIsEnabledStub.returns(false);

const request = spec.buildRequests(bidRequests, bidderRequest);
expect(storage.getCookie(USER_ID_KEY)).to.be.a('string');
expect(request.data.user.ext.vads).to.be.a('string');
});

it('should create user id and store it in local storage (if user id does not exist)', function () {
cookiesAreEnabledStub.returns(false);
localStorageIsEnabledStub.returns(true);

const request = spec.buildRequests(bidRequests, bidderRequest);
expect(storage.getDataFromLocalStorage(USER_ID_KEY)).to.be.a('string');
expect(request.data.user.ext.vads).to.be.a('string');
});
});
});

0 comments on commit 543544c

Please sign in to comment.