diff --git a/modules/visxBidAdapter.js b/modules/visxBidAdapter.js index a066d93d69e..c1bb626a39c 100644 --- a/modules/visxBidAdapter.js +++ b/modules/visxBidAdapter.js @@ -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'; @@ -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, @@ -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) && { @@ -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); diff --git a/test/spec/modules/visxBidAdapter_spec.js b/test/spec/modules/visxBidAdapter_spec.js index 9a486cd6c34..139349ceead 100755 --- a/test/spec/modules/visxBidAdapter_spec.js +++ b/test/spec/modules/visxBidAdapter_spec.js @@ -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'; @@ -82,6 +82,9 @@ describe('VisxAdapter', function () { }); return res; } + + let cookiesAreEnabledStub, localStorageIsEnabledStub; + const bidderRequest = { timeout: 3000, refererInfo: { @@ -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]; @@ -422,6 +443,7 @@ describe('VisxAdapter', function () { }); return res; } + let cookiesAreEnabledStub, localStorageIsEnabledStub; const bidderRequest = { timeout: 3000, refererInfo: { @@ -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); @@ -486,6 +526,7 @@ describe('VisxAdapter', function () { }); return res; } + let cookiesAreEnabledStub, localStorageIsEnabledStub; const bidderRequest = { timeout: 3000, refererInfo: { @@ -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 () { @@ -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'); + }); + }); });