Skip to content

Commit

Permalink
Merge pull request #5 from Impactify-AdTech/feature/local_storage
Browse files Browse the repository at this point in the history
Impactify Bid Adapter: add handling of localstorage
  • Loading branch information
thomasdseao authored Oct 11, 2023
2 parents 2a4f279 + 498d4b4 commit 11fe4d6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
44 changes: 33 additions & 11 deletions modules/impactifyBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { deepAccess, deepSetValue, generateUUID } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { ajax } from '../src/ajax.js';
import { createEidsArray } from './userId/eids.js';
import { getStorageManager } from '../src/storageManager.js';
import { createEidsArray } from 'modules/userId/eids.js';

const BIDDER_CODE = 'impactify';
const BIDDER_ALIAS = ['imp'];
Expand All @@ -13,10 +14,12 @@ const DEFAULT_VIDEO_WIDTH = 640;
const DEFAULT_VIDEO_HEIGHT = 360;
const ORIGIN = 'https://sonic.impactify.media';
const LOGGER_URI = 'https://logger.impactify.media';
const AUCTIONURI = '/bidder';
const COOKIESYNCURI = '/static/cookie_sync.html';
const GVLID = 606;
const GETCONFIG = config.getConfig;
const AUCTION_URI = '/bidder';
const COOKIE_SYNC_URI = '/static/cookie_sync.html';
const GVL_ID = 606;
const GET_CONFIG = config.getConfig;
const STORAGE = getStorageManager({gvlid: GVL_ID, bidderCode: BIDDER_CODE});
const STORAGE_KEY = '_im_str'

/**
* Helpers object
Expand Down Expand Up @@ -48,6 +51,7 @@ const helpers = {

return ext;
},

getDeviceType() {
// OpenRTB Device type
if ((/ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(navigator.userAgent.toLowerCase()))) {
Expand All @@ -58,6 +62,7 @@ const helpers = {
}
return 2;
},

createOrtbImpBannerObj(bid, size) {
let sizes = size.split('x');

Expand All @@ -69,6 +74,7 @@ const helpers = {
}]
}
},

createOrtbImpVideoObj(bid) {
return {
id: 'video-' + bid.bidId,
Expand All @@ -77,6 +83,7 @@ const helpers = {
mimes: ['video/mp4'],
}
},

getFloor(bid) {
const floorInfo = bid.getFloor({
currency: DEFAULT_CURRENCY,
Expand All @@ -87,7 +94,12 @@ const helpers = {
return parseFloat(floorInfo.floor);
}
return null;
},

getImStrFromLocalStorage() {
return STORAGE.localStorageIsEnabled(false) ? STORAGE.getDataFromLocalStorage(STORAGE_KEY, false) : '';
}

}

/**
Expand Down Expand Up @@ -154,7 +166,7 @@ function createOpenRtbRequest(validBidRequests, bidderRequest) {
this.syncStore.uspConsent = bidderRequest.uspConsent;
}

if (GETCONFIG('coppa') == true) deepSetValue(request, 'regs.coppa', 1);
if (GET_CONFIG('coppa') == true) deepSetValue(request, 'regs.coppa', 1);

if (bidderRequest.uspConsent) {
deepSetValue(request, 'regs.ext.us_privacy', bidderRequest.uspConsent);
Expand Down Expand Up @@ -205,9 +217,10 @@ function createOpenRtbRequest(validBidRequests, bidderRequest) {
*/
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
gvlid: GVL_ID,
supportedMediaTypes: ['video', 'banner'],
aliases: BIDDER_ALIAS,
storageAllowed: true,

/**
* Determines whether or not the given bid request is valid.
Expand Down Expand Up @@ -247,11 +260,20 @@ export const spec = {
buildRequests: function (validBidRequests, bidderRequest) {
// Create a clean openRTB request
let request = createOpenRtbRequest(validBidRequests, bidderRequest);
const imStr = helpers.getImStrFromLocalStorage();
const options = {}

if (imStr) {
options.customHeaders = {
'x-impact': imStr
};
}

return {
method: 'POST',
url: ORIGIN + AUCTIONURI,
url: ORIGIN + AUCTION_URI,
data: JSON.stringify(request),
options
};
},

Expand Down Expand Up @@ -341,7 +363,7 @@ export const spec = {

return [{
type: 'iframe',
url: ORIGIN + COOKIESYNCURI + params
url: ORIGIN + COOKIE_SYNC_URI + params
}];
},

Expand All @@ -350,7 +372,7 @@ export const spec = {
* @param {Bid} The bid that won the auction
*/
onBidWon: function(bid) {
ajax(`${LOGGER_URI}/log/bidder/won`, null, JSON.stringify(bid), {
ajax(`${LOGGER_URI}/prebid/won`, null, JSON.stringify(bid), {
method: 'POST',
contentType: 'application/json'
});
Expand All @@ -363,7 +385,7 @@ export const spec = {
* @param {data} Containing timeout specific data
*/
onTimeout: function(data) {
ajax(`${LOGGER_URI}/log/bidder/timeout`, null, JSON.stringify(data[0]), {
ajax(`${LOGGER_URI}/prebid/timeout`, null, JSON.stringify(data[0]), {
method: 'POST',
contentType: 'application/json'
});
Expand Down
14 changes: 11 additions & 3 deletions modules/impactifyBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ Maintainer: [email protected]

Module that connects to the Impactify solution.
The impactify bidder need 3 parameters:
- appId : This is your unique publisher identifier
- format : This is the ad format needed, can be : screen or display (Only for video media type)
- style : This is the ad style needed, can be : inline, impact or static (Only for video media type)
- appId : This is your unique publisher identifier
- format : This is the ad format needed, can be : screen or display (Only for video media type)
- style : This is the ad style needed, can be : inline, impact or static (Only for video media type)

Note : Impactify adapter need storage access to work properly (Do not forget to set storageAllowed to true).

# Test Parameters
```
pbjs.bidderSettings = {
impactify: {
storageAllowed: true // Mandatory
}
};
var adUnitsVideo = [{
code: 'your-slot-div-id-video', // This is your slot div id
mediaTypes: {
Expand Down
48 changes: 46 additions & 2 deletions test/spec/modules/impactifyBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { spec } from 'modules/impactifyBidAdapter.js';
import { spec, storage } from 'modules/impactifyBidAdapter.js';
import * as utils from 'src/utils.js';
import sinon from 'sinon';

const BIDDER_CODE = 'impactify';
const BIDDER_ALIAS = ['imp'];
Expand All @@ -19,6 +20,15 @@ var gdprData = {
};

describe('ImpactifyAdapter', function () {
beforeEach(function () {
$$PREBID_GLOBAL$$.bidderSettings = {
impactify: {
storageAllowed: true
}
};
sinon.stub(document.body, 'appendChild');
});

describe('isBidRequestValid', function () {
let validBids = [
{
Expand Down Expand Up @@ -173,6 +183,9 @@ describe('ImpactifyAdapter', function () {
});
});
describe('buildRequests', function () {
let getLocalStorageStub;
let localStorageIsEnabledStub;

let videoBidRequests = [
{
bidder: 'impactify',
Expand Down Expand Up @@ -221,11 +234,42 @@ describe('ImpactifyAdapter', function () {
referer: 'https://impactify.io'
}
};

afterEach(function() {
localStorageIsEnabledStub.restore();
getLocalStorageStub.restore();
});
it('sends video bid request to ENDPOINT via POST', function () {
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
localStorageIsEnabledStub.returns(true);

getLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
getLocalStorageStub.returns('testValue');

const request = spec.buildRequests(videoBidRequests, videoBidderRequest);

expect(request.url).to.equal(ORIGIN + AUCTIONURI);
expect(request.method).to.equal('POST');
expect(request.options.customHeaders['x-impact']).to.equal('testValue');
});

it('should set header value from localstorage correctly', function () {
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
localStorageIsEnabledStub.returns(true);

getLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
getLocalStorageStub.returns('testValue');
const request = spec.buildRequests(videoBidRequests, videoBidderRequest);

expect(request.options.customHeaders['x-impact']).to.equal('testValue');
});

it('should set header value to empty if localstorage is not enabled', function () {
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
localStorageIsEnabledStub.returns(false);

const request = spec.buildRequests(videoBidRequests, videoBidderRequest);

expect(request.options.customHeaders['x-impact']).to.equal('');
});
});
describe('interpretResponse', function () {
Expand Down

0 comments on commit 11fe4d6

Please sign in to comment.