Skip to content

Commit

Permalink
Capture target demand provider (#7636)
Browse files Browse the repository at this point in the history
- Use the provided storage manager
- Capture target demand provider & account id
- Add flag to trigger randomly-generated demo bid response
  • Loading branch information
samueldobbie authored Nov 9, 2021
1 parent 5ad61fc commit 4253497
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
47 changes: 31 additions & 16 deletions modules/glimpseBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { BANNER } from '../src/mediaTypes.js'
import { config } from '../src/config.js'
import { getStorageManager } from '../src/storageManager.js'
import { isArray } from '../src/utils.js'
import { registerBidder } from '../src/adapters/bidderFactory.js'

const storageManager = getStorageManager()

const GVLID = 1012
const BIDDER_CODE = 'glimpse'
const ENDPOINT = 'https://api.glimpsevault.io/ads/serving/public/v1/prebid'
const LOCAL_STORAGE_KEY = {
glimpse: {
vault: {
jwt: 'gp_vault_jwt',
},
}

export const spec = {
gvlid: GVLID,
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],

Expand All @@ -37,20 +40,28 @@ export const spec = {
* @returns {ServerRequest}
*/
buildRequests: (validBidRequests, bidderRequest) => {
const networkId = window.networkId || -1
const bids = validBidRequests.map(processBidRequest)
const demo = config.getConfig('glimpse.demo') || false
const account = config.getConfig('glimpse.account') || -1
const demand = config.getConfig('glimpse.demand') || 'glimpse'
const keywords = config.getConfig('glimpse.keywords') || {}

const auth = getVaultJwt()
const referer = getReferer(bidderRequest)
const gdprConsent = getGdprConsentChoice(bidderRequest)
const jwt = getVaultJwt()
const bids = validBidRequests.map((bidRequest) => {
return processBidRequest(bidRequest, keywords)
})

const data = {
auth: jwt,
auth,
data: {
bidderCode: spec.code,
networkId,
bids,
demo,
account,
demand,
referer,
gdprConsent,
bids,
}
}

Expand All @@ -65,10 +76,9 @@ export const spec = {
/**
* Parse response from Glimpse server
* @param bidResponse {ServerResponse}
* @param bidRequest {BidRequest}
* @returns {Bid[]}
*/
interpretResponse: (bidResponse, bidRequest) => {
interpretResponse: (bidResponse) => {
const isValidResponse = isValidBidResponse(bidResponse)

if (isValidResponse) {
Expand All @@ -81,16 +91,20 @@ export const spec = {
},
}

function processBidRequest(bidRequest) {
function processBidRequest(bidRequest, globalKeywords) {
const sizes = normalizeSizes(bidRequest.sizes)
const keywords = bidRequest.params.keywords || []
const bidKeywords = bidRequest.params.keywords || {}
const keywords = {
...globalKeywords,
...bidKeywords,
}

return {
unitCode: bidRequest.adUnitCode,
bidId: bidRequest.bidId,
placementId: bidRequest.params.placementId,
unitCode: bidRequest.adUnitCode,
sizes,
keywords,
sizes,
}
}

Expand Down Expand Up @@ -124,7 +138,8 @@ function getReferer(bidderRequest) {
function getGdprConsentChoice(bidderRequest) {
const hasGdprConsent =
hasValue(bidderRequest) &&
hasValue(bidderRequest.gdprConsent)
hasValue(bidderRequest.gdprConsent) &&
hasStringValue(bidderRequest.gdprConsent.consentString)

if (hasGdprConsent) {
return bidderRequest.gdprConsent
Expand All @@ -138,11 +153,11 @@ function getGdprConsentChoice(bidderRequest) {
}

function setVaultJwt(auth) {
storageManager.setDataInLocalStorage(LOCAL_STORAGE_KEY.glimpse.jwt, auth)
storageManager.setDataInLocalStorage(LOCAL_STORAGE_KEY.vault.jwt, auth)
}

function getVaultJwt() {
return storageManager.getDataFromLocalStorage(LOCAL_STORAGE_KEY.glimpse.jwt) || ''
return storageManager.getDataFromLocalStorage(LOCAL_STORAGE_KEY.vault.jwt) || ''
}

function isValidBidResponse(bidResponse) {
Expand Down
33 changes: 33 additions & 0 deletions test/spec/modules/glimpseBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BANNER } from '../../../src/mediaTypes'
import { expect } from 'chai'
import { newBidder } from 'src/adapters/bidderFactory.js'
import { spec } from 'modules/glimpseBidAdapter.js'
Expand Down Expand Up @@ -79,6 +80,20 @@ function getDeepCopy(object) {
describe('GlimpseProtocolAdapter', () => {
const glimpseAdapter = newBidder(spec)

describe('spec', () => {
it('Has defined the glimpse gvlid', () => {
expect(spec.gvlid).to.equal(1012)
})

it('Has defined glimpse as the bidder', () => {
expect(spec.code).to.equal('glimpse')
})

it('Has defined valid mediaTypes', () => {
expect(spec.supportedMediaTypes).to.deep.equal([BANNER])
})
})

describe('Inherited functions', () => {
it('Functions exist and are valid types', () => {
expect(glimpseAdapter.callBids).to.exist.and.to.be.a('function')
Expand Down Expand Up @@ -163,6 +178,24 @@ describe('GlimpseProtocolAdapter', () => {
const bidRequests = [getBidRequest()]
const bidderRequest = getBidderRequest()

it('Adds a demo flag', () => {
const request = spec.buildRequests(bidRequests, bidderRequest)
const payload = JSON.parse(request.data)
expect(payload.data.demo).to.be.false
})

it('Adds an account id', () => {
const request = spec.buildRequests(bidRequests, bidderRequest)
const payload = JSON.parse(request.data)
expect(payload.data.account).to.equal(-1)
})

it('Adds a demand provider', () => {
const request = spec.buildRequests(bidRequests, bidderRequest)
const payload = JSON.parse(request.data)
expect(payload.data.demand).to.equal('glimpse')
})

it('Adds GDPR consent', () => {
const request = spec.buildRequests(bidRequests, bidderRequest)
const payload = JSON.parse(request.data)
Expand Down

0 comments on commit 4253497

Please sign in to comment.