forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update glimpse adapter and test spec (prebid#7476)
- Capture network ids and GDPR consent choice - Retrieve and set vault JWT - Increase code coverage - General refactor / tidy
- Loading branch information
1 parent
0f7a146
commit 9f5398a
Showing
4 changed files
with
13,595 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,181 @@ | ||
import { registerBidder } from '../src/adapters/bidderFactory.js'; | ||
import { BANNER } from '../src/mediaTypes.js'; | ||
import { BANNER } from '../src/mediaTypes.js' | ||
import { getStorageManager } from '../src/storageManager.js' | ||
import { isArray } from '../src/utils.js' | ||
import { registerBidder } from '../src/adapters/bidderFactory.js' | ||
|
||
const BIDDER_CODE = 'glimpse'; | ||
const storageManager = getStorageManager() | ||
|
||
function transformEachBidResponse(glimpseBid) { | ||
const bid = glimpseBid; | ||
bid.meta = { advertiserDomains: [] }; | ||
|
||
if (glimpseBid.adomain) { | ||
bid.meta.advertiserDomains = glimpseBid.adomain; | ||
} | ||
|
||
return bid; | ||
const BIDDER_CODE = 'glimpse' | ||
const ENDPOINT = 'https://api.glimpsevault.io/ads/serving/public/v1/prebid' | ||
const LOCAL_STORAGE_KEY = { | ||
glimpse: { | ||
jwt: 'gp_vault_jwt', | ||
}, | ||
} | ||
|
||
export const spec = { | ||
code: BIDDER_CODE, | ||
url: 'https://api.glimpseprotocol.io/cloud/v1/vault/prebid', | ||
supportedMediaTypes: [BANNER], | ||
|
||
/** | ||
* Determines whether or not the given bid request is valid | ||
* @param bid {BidRequest} The bid to validate | ||
* @return {boolean} | ||
*/ | ||
isBidRequestValid: (bid) => { | ||
try { | ||
const { placementId } = bid.params; | ||
return typeof placementId === 'string' && placementId.length > 0; | ||
} catch (error) { | ||
return false; | ||
} | ||
return ( | ||
hasValue(bid) && | ||
hasValue(bid.params) && | ||
hasStringValue(bid.params.placementId) | ||
) | ||
}, | ||
|
||
/** | ||
* Builds http request for Glimpse bids | ||
* @param validBidRequests {BidRequest[]} | ||
* @param bidderRequest {BidderRequest} | ||
* @returns {ServerRequest} | ||
*/ | ||
buildRequests: (validBidRequests, bidderRequest) => { | ||
const { url, code: bidderCode } = spec; | ||
|
||
const bids = validBidRequests.map((request) => { | ||
delete request.mediaTypes; | ||
return request; | ||
}); | ||
const networkId = window.networkId || -1 | ||
const bids = validBidRequests.map(processBidRequest) | ||
const referer = getReferer(bidderRequest) | ||
const gdprConsent = getGdprConsentChoice(bidderRequest) | ||
const jwt = getVaultJwt() | ||
|
||
const sdk = { | ||
source: 'pbjs', | ||
version: '$prebid.version$', | ||
}; | ||
|
||
const payload = { | ||
...bidderRequest, | ||
bidderCode, | ||
bids, | ||
sdk, | ||
}; | ||
const data = { | ||
auth: jwt, | ||
data: { | ||
bidderCode: spec.code, | ||
networkId, | ||
bids, | ||
referer, | ||
gdprConsent, | ||
} | ||
} | ||
|
||
return { | ||
method: 'POST', | ||
url, | ||
data: JSON.stringify(payload), | ||
}; | ||
url: ENDPOINT, | ||
data: JSON.stringify(data), | ||
options: {}, | ||
} | ||
}, | ||
|
||
interpretResponse: (serverResponse, _) => { | ||
let bids = []; | ||
try { | ||
const { body } = serverResponse; | ||
bids = body.map(transformEachBidResponse); | ||
} catch (error) {} | ||
/** | ||
* Parse response from Glimpse server | ||
* @param bidResponse {ServerResponse} | ||
* @param bidRequest {BidRequest} | ||
* @returns {Bid[]} | ||
*/ | ||
interpretResponse: (bidResponse, bidRequest) => { | ||
const isValidResponse = isValidBidResponse(bidResponse) | ||
|
||
if (isValidResponse) { | ||
const {auth, data} = bidResponse.body | ||
setVaultJwt(auth) | ||
return data.bids | ||
} | ||
|
||
return bids; | ||
return [] | ||
}, | ||
}; | ||
} | ||
|
||
function processBidRequest(bidRequest) { | ||
const sizes = normalizeSizes(bidRequest.sizes) | ||
const keywords = bidRequest.params.keywords || [] | ||
|
||
return { | ||
bidId: bidRequest.bidId, | ||
placementId: bidRequest.params.placementId, | ||
unitCode: bidRequest.adUnitCode, | ||
sizes, | ||
keywords, | ||
} | ||
} | ||
|
||
function normalizeSizes(sizes) { | ||
const isSingleSize = | ||
isArray(sizes) && | ||
sizes.length === 2 && | ||
!isArray(sizes[0]) && | ||
!isArray(sizes[1]) | ||
|
||
if (isSingleSize) { | ||
return [sizes] | ||
} | ||
|
||
return sizes | ||
} | ||
|
||
function getReferer(bidderRequest) { | ||
const hasReferer = | ||
hasValue(bidderRequest) && | ||
hasValue(bidderRequest.refererInfo) && | ||
hasStringValue(bidderRequest.refererInfo.referer) | ||
|
||
if (hasReferer) { | ||
return bidderRequest.refererInfo.referer | ||
} | ||
|
||
return '' | ||
} | ||
|
||
function getGdprConsentChoice(bidderRequest) { | ||
const hasGdprConsent = | ||
hasValue(bidderRequest) && | ||
hasValue(bidderRequest.gdprConsent) | ||
|
||
if (hasGdprConsent) { | ||
return bidderRequest.gdprConsent | ||
} | ||
|
||
return { | ||
consentString: '', | ||
vendorData: {}, | ||
gdprApplies: false, | ||
} | ||
} | ||
|
||
function setVaultJwt(auth) { | ||
storageManager.setDataInLocalStorage(LOCAL_STORAGE_KEY.glimpse.jwt, auth) | ||
} | ||
|
||
function getVaultJwt() { | ||
return storageManager.getDataFromLocalStorage(LOCAL_STORAGE_KEY.glimpse.jwt) || '' | ||
} | ||
|
||
function isValidBidResponse(bidResponse) { | ||
return ( | ||
hasValue(bidResponse) && | ||
hasValue(bidResponse.body) && | ||
hasValue(bidResponse.body.data) && | ||
hasArrayValue(bidResponse.body.data.bids) && | ||
hasStringValue(bidResponse.body.auth) | ||
) | ||
} | ||
|
||
function hasValue(value) { | ||
return ( | ||
value !== undefined && | ||
value !== null | ||
) | ||
} | ||
|
||
function hasStringValue(value) { | ||
return ( | ||
hasValue(value) && | ||
typeof value === 'string' && | ||
value.length > 0 | ||
) | ||
} | ||
|
||
function hasArrayValue(value) { | ||
return ( | ||
hasValue(value) && | ||
isArray(value) && | ||
value.length > 0 | ||
) | ||
} | ||
|
||
registerBidder(spec); | ||
registerBidder(spec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.