Skip to content

Commit

Permalink
proxistore bid adapter: delay request to server by 5 min if there wer…
Browse files Browse the repository at this point in the history
…e no bids (#5379)

* delay request to server by 5 min if we no ads

* fix testing issue

* use storeManager.js

* change var to const

* add unit test

* remove line and check if user authorizes use of local storage
  • Loading branch information
vincentproxistore authored Jul 6, 2020
1 parent 31bed50 commit 9d4f8ff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
71 changes: 54 additions & 17 deletions modules/proxistoreBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const { registerBidder } = require('../src/adapters/bidderFactory.js');

import { registerBidder } from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';
const BIDDER_CODE = 'proxistore';
const storage = getStorageManager();
const PROXISTORE_VENDOR_ID = 418;

function _createServerRequest(bidRequests, bidderRequest) {
const sizeIds = [];
bidRequests.forEach(bid => {
const sizeId = {id: bid.bidId, sizes: bid.sizes.map(size => { return { width: size[0], height: size[1] } })};

bidRequests.forEach(function (bid) {
const sizeId = {
id: bid.bidId,
sizes: bid.sizes.map(function (size) {
return {
width: size[0],
height: size[1]
};
})
};
sizeIds.push(sizeId);
});
const payload = {
Expand All @@ -18,22 +30,22 @@ function _createServerRequest(bidRequests, bidderRequest) {
applies: false
}
};

const options = {
contentType: 'application/json',
withCredentials: true
};

if (bidderRequest && bidderRequest.gdprConsent) {
if ((typeof bidderRequest.gdprConsent.gdprApplies === 'boolean') && bidderRequest.gdprConsent.gdprApplies) {
if (typeof bidderRequest.gdprConsent.gdprApplies === 'boolean' && bidderRequest.gdprConsent.gdprApplies) {
payload.gdpr.applies = true;
}
if ((typeof bidderRequest.gdprConsent.consentString === 'string') && bidderRequest.gdprConsent.consentString) {

if (typeof bidderRequest.gdprConsent.consentString === 'string' && bidderRequest.gdprConsent.consentString) {
payload.gdpr.consentString = bidderRequest.gdprConsent.consentString;
}
if (bidderRequest.gdprConsent.vendorData && bidderRequest.gdprConsent.vendorData.vendorConsents &&
typeof bidderRequest.gdprConsent.vendorData.vendorConsents[PROXISTORE_VENDOR_ID.toString(10)] !== 'undefined') {
payload.gdpr.consentGiven = !!(bidderRequest.gdprConsent.vendorData.vendorConsents[PROXISTORE_VENDOR_ID.toString(10)]);

if (bidderRequest.gdprConsent.vendorData && bidderRequest.gdprConsent.vendorData.vendorConsents && typeof bidderRequest.gdprConsent.vendorData.vendorConsents[PROXISTORE_VENDOR_ID.toString(10)] !== 'undefined') {
payload.gdpr.consentGiven = !!bidderRequest.gdprConsent.vendorData.vendorConsents[PROXISTORE_VENDOR_ID.toString(10)];
}
}

Expand All @@ -59,17 +71,31 @@ function _createBidResponse(response) {
vastUrl: response.vastUrl,
vastXml: response.vastXml,
dealId: response.dealId
}
};
}

/**
* Determines whether or not the given bid request is valid.
*
* @param bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/

function isBidRequestValid(bid) {
return !!(bid.params.website && bid.params.language);
const hasNoAd = function() {
if (!storage.hasLocalStorage()) {
return false;
}
const pxNoAds = storage.getDataFromLocalStorage(`PX_NoAds_${bid.params.website}`);
if (!pxNoAds) {
return false;
} else {
const storedDate = new Date(pxNoAds);
const now = new Date();
const diff = Math.abs(storedDate.getTime() - now.getTime()) / 60000;
return diff <= 5;
}
}
return !!(bid.params.website && bid.params.language) && !hasNoAd();
}

/**
Expand All @@ -79,38 +105,51 @@ function isBidRequestValid(bid) {
* @param bidderRequest
* @return ServerRequest Info describing the request to the server.
*/

function buildRequests(bidRequests, bidderRequest) {
var request = _createServerRequest(bidRequests, bidderRequest);
const request = _createServerRequest(bidRequests, bidderRequest);
return request;
}

/**
* Unpack the response from the server into a list of bids.
*
* @param serverResponse A successful response from the server.
* @param bidRequest Request original server request
* @return An array of bids which were nested inside the server.
*/

function interpretResponse(serverResponse, bidRequest) {
const itemName = `PX_NoAds_${websiteFromBidRequest(bidRequest)}`;
if (serverResponse.body.length > 0) {
storage.removeDataFromLocalStorage(itemName, true);
return serverResponse.body.map(_createBidResponse);
} else {
storage.setDataInLocalStorage(itemName, new Date());
return [];
}
}

const websiteFromBidRequest = function(bidR) {
if (bidR.data) {
return JSON.parse(bidR.data).website
} else if (bidR.params.website) {
return bidR.params.website;
}
}

/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param syncOptions Which user syncs are allowed?
* @param serverResponses List of server's responses.
* @return The user syncs which should be dropped.
*/

function getUserSyncs(syncOptions, serverResponses) {
return [];
}

const spec = {
export const spec = {
code: BIDDER_CODE,
isBidRequestValid: isBidRequestValid,
buildRequests: buildRequests,
Expand All @@ -119,5 +158,3 @@ const spec = {
};

registerBidder(spec);

module.exports = spec;
24 changes: 21 additions & 3 deletions test/spec/modules/proxistoreBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
let spec = require('modules/proxistoreBidAdapter');
let { spec } = require('modules/proxistoreBidAdapter');

const BIDDER_CODE = 'proxistore';
describe('ProxistoreBidAdapter', function () {
Expand Down Expand Up @@ -28,7 +28,21 @@ describe('ProxistoreBidAdapter', function () {
transactionId: 511916005
};
describe('isBidRequestValid', function () {
it('it should be true if required params are presents', function () {
it('it should be true if required params are presents and there is no info in the local storage', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});

it('it should be false if the value in the localstorage is less than 5minutes of the actual time', function() {
const date = new Date();
date.setMinutes(date.getMinutes() - 1)
localStorage.setItem(`PX_NoAds_${bid.params.website}`, date)
expect(spec.isBidRequestValid(bid)).to.equal(false);
});

it('it should be true if the value in the localstorage is more than 5minutes of the actual time', function() {
const date = new Date();
date.setMinutes(date.getMinutes() - 10)
localStorage.setItem(`PX_NoAds_${bid.params.website}`, date)
expect(spec.isBidRequestValid(bid)).to.equal(true);
});
});
Expand Down Expand Up @@ -97,7 +111,11 @@ describe('ProxistoreBidAdapter', function () {
expect(interpretedResponse.requestId).equal('923756713');
expect(interpretedResponse.netRevenue).to.be.true;
expect(interpretedResponse.netRevenue).to.be.true;
})
});
it('should have a value in the local storage if the response is empty', function() {
spec.interpretResponse(badResponse, bid);
expect(localStorage.getItem(`PX_NoAds_${bid.params.website}`)).to.be.string;
});
});

describe('interpretResponse', function () {
Expand Down

0 comments on commit 9d4f8ff

Please sign in to comment.