Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmartyadsBidAdapter: remove usprivacy #11829

Merged
32 changes: 32 additions & 0 deletions libraries/smartyadsUtils/getAdUrlByRegion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const adUrls = {
US_EAST: 'https://n1.smartyads.com/?c=o&m=prebid&secret_key=prebid_js',
EU: 'https://n2.smartyads.com/?c=o&m=prebid&secret_key=prebid_js',
SGP: 'https://n6.smartyads.com/?c=o&m=prebid&secret_key=prebid_js'
};

export function getAdUrlByRegion(bid) {
let adUrl;

if (bid.params.region && adUrls[bid.params.region]) {
adUrl = adUrls[bid.params.region];
} else {
try {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const region = timezone.split('/')[0];

switch (region) {
case 'Europe':
adUrl = adUrls['EU'];
break;
case 'Asia':
adUrl = adUrls['SGP'];
break;
default: adUrl = adUrls['US_EAST'];
}
} catch (err) {
adUrl = adUrls['US_EAST'];
}
}

return adUrl;
};
116 changes: 6 additions & 110 deletions modules/smartyadsBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,15 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { config } from '../src/config.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
import { ajax } from '../src/ajax.js';
import { getAdUrlByRegion } from '../libraries/smartyadsUtils/getAdUrlByRegion.js';
import { interpretResponse, getUserSyncs } from '../libraries/teqblazeUtils/bidderUtils.js';

const BIDDER_CODE = 'smartyads';
const GVLID = 534;
const adUrls = {
US_EAST: 'https://n1.smartyads.com/?c=o&m=prebid&secret_key=prebid_js',
EU: 'https://n2.smartyads.com/?c=o&m=prebid&secret_key=prebid_js',
SGP: 'https://n6.smartyads.com/?c=o&m=prebid&secret_key=prebid_js'
}

const URL_SYNC = 'https://as.ck-ie.com/prebidjs?p=7c47322e527cf8bdeb7facc1bb03387a';

function isBidResponseValid(bid) {
if (!bid.requestId || !bid.cpm || !bid.creativeId ||
!bid.ttl || !bid.currency) {
return false;
}
switch (bid['mediaType']) {
case BANNER:
return Boolean(bid.width && bid.height && bid.ad);
case VIDEO:
return Boolean(bid.vastUrl) || Boolean(bid.vastXml);
case NATIVE:
return Boolean(bid.native && bid.native.title && bid.native.image && bid.native.impressionTrackers);
default:
return false;
}
}

function getAdUrlByRegion(bid) {
let adUrl;

if (bid.params.region && adUrls[bid.params.region]) {
adUrl = adUrls[bid.params.region];
} else {
try {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const region = timezone.split('/')[0];

switch (region) {
case 'Europe':
adUrl = adUrls['EU'];
break;
case 'Asia':
adUrl = adUrls['SGP'];
break;
default: adUrl = adUrls['US_EAST'];
}
} catch (err) {
adUrl = adUrls['US_EAST'];
}
}

return adUrl;
}

export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
Expand All @@ -78,20 +30,15 @@ export const spec = {
let request = {
'deviceWidth': winTop.screen.width,
'deviceHeight': winTop.screen.height,
'language': (navigator && navigator.language) ? navigator.language : '',
'secure': 1,
'host': location?.domain ?? '',
'page': location?.page ?? '',
'coppa': config.getConfig('coppa') === true ? 1 : 0,
Copy link
Collaborator

@patmmccann patmmccann Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will not be accepting tricks to fool the duplication check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patmmccann OK, but how can I fix that? I saw the report and duplication check marks, for example, the isBidResponseValid function is used in lots of adapters and looks almost the same.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out this work in progress from an adapter structured very similar to yours

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patmmccann I'm sorry, but I still don't get it. The previous version of the isBidResponseValid function was flagged as duplicate code by your tool, but it doesn't need any updates, right? And the same function with same code is used in other adapters

Copy link
Collaborator

@patmmccann patmmccann Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You and other modules have duplicate code, we need you to move common functions into libraries before making further updates to your adapter

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#8527 has an example on how to move common functions into a library

'placements': placements,
'eeid': validBidRequests[0]?.userIdAsEids,
'ifa': bidderRequest?.ortb2?.device?.ifa,
};
request.language.indexOf('-') != -1 && (request.language = request.language.split('-')[0])

if (bidderRequest) {
if (bidderRequest.uspConsent) {
request.ccpa = bidderRequest.uspConsent;
}
if (bidderRequest.gdprConsent) {
request.gdpr = bidderRequest.gdprConsent
}
Expand Down Expand Up @@ -128,59 +75,8 @@ export const spec = {
}
},

interpretResponse: (serverResponse) => {
let response = [];
serverResponse = serverResponse.body;
for (let i = 0; i < serverResponse.length; i++) {
let resItem = serverResponse[i];
if (isBidResponseValid(resItem)) {
response.push(resItem);
}
}
return response;
},

getUserSyncs: (syncOptions, serverResponses = [], gdprConsent = {}, uspConsent = '', gppConsent = '') => {
let syncs = [];
let { gdprApplies, consentString = '' } = gdprConsent;

if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: `${URL_SYNC}&gdpr=${gdprApplies ? 1 : 0}&gdpr_consent=${consentString}&type=iframe&us_privacy=${uspConsent}&gpp=${gppConsent}`
});
} else {
syncs.push({
type: 'image',
url: `${URL_SYNC}&gdpr=${gdprApplies ? 1 : 0}&gdpr_consent=${consentString}&type=image&us_privacy=${uspConsent}&gpp=${gppConsent}`
});
}

return syncs
},

onBidWon: function(bid) {
if (bid.winUrl) {
ajax(bid.winUrl, () => {}, JSON.stringify(bid));
} else {
if (bid?.postData && bid?.postData[0] && bid?.postData[0].params && bid?.postData[0].params[0].host == 'prebid') {
ajax('https://et-nd43.itdsmr.com/?c=o&m=prebid&secret_key=prebid_js&winTest=1', () => {}, JSON.stringify(bid));
}
}
},

onTimeout: function(bid) {
if (bid?.postData && bid?.postData[0] && bid?.postData[0].params && bid?.postData[0].params[0].host == 'prebid') {
ajax('https://et-nd43.itdsmr.com/?c=o&m=prebid&secret_key=prebid_js&bidTimeout=1', () => {}, JSON.stringify(bid));
}
},

onBidderError: function(bid) {
if (bid?.postData && bid?.postData[0] && bid?.postData[0].params && bid?.postData[0].params[0].host == 'prebid') {
ajax('https://et-nd43.itdsmr.com/?c=o&m=prebid&secret_key=prebid_js&bidderError=1', () => {}, JSON.stringify(bid));
}
},

interpretResponse,
getUserSyncs: getUserSyncs(URL_SYNC),
};

registerBidder(spec);
89 changes: 6 additions & 83 deletions test/spec/modules/smartyadsBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ describe('SmartyadsAdapter', function () {
it('Returns valid data if array of bids is valid', function () {
let data = serverRequest.data;
expect(data).to.be.an('object');
expect(data).to.have.all.keys('deviceWidth', 'deviceHeight', 'language', 'secure', 'host', 'page', 'placements', 'coppa', 'eeid', 'ifa');
expect(data).to.have.all.keys('deviceWidth', 'deviceHeight', 'host', 'page', 'placements', 'coppa', 'eeid', 'ifa');
expect(data.deviceWidth).to.be.a('number');
expect(data.deviceHeight).to.be.a('number');
expect(data.coppa).to.be.a('number');
expect(data.language).to.be.a('string');
expect(data.secure).to.be.within(0, 1);
expect(data.host).to.be.a('string');
expect(data.page).to.be.a('string');
let placement = data['placements'][0];
Expand Down Expand Up @@ -126,8 +124,8 @@ describe('SmartyadsAdapter', function () {
expect(dataItem.width).to.equal(300);
expect(dataItem.height).to.equal(250);
expect(dataItem.ad).to.equal('Test');
expect(dataItem.meta).to.have.property('advertiserDomains')
expect(dataItem.meta.advertiserDomains).to.deep.equal(['example.com']);
expect(dataItem.meta).to.have.property('advertiserDomains');
expect(dataItem.meta.advertiserDomains).to.be.an('array');
expect(dataItem.ttl).to.equal(120);
expect(dataItem.creativeId).to.equal('2');
expect(dataItem.netRevenue).to.be.true;
Expand All @@ -152,7 +150,7 @@ describe('SmartyadsAdapter', function () {

let dataItem = videoResponses[0];
expect(dataItem).to.have.all.keys('requestId', 'cpm', 'vastUrl', 'ttl', 'creativeId',
'netRevenue', 'currency', 'dealId', 'mediaType');
'netRevenue', 'currency', 'dealId', 'mediaType', 'meta');
expect(dataItem.requestId).to.equal('23fhj33i987f');
expect(dataItem.cpm).to.equal(0.5);
expect(dataItem.vastUrl).to.equal('test.com');
Expand Down Expand Up @@ -183,7 +181,7 @@ describe('SmartyadsAdapter', function () {
expect(nativeResponses).to.be.an('array').that.is.not.empty;

let dataItem = nativeResponses[0];
expect(dataItem).to.have.keys('requestId', 'cpm', 'ttl', 'creativeId', 'netRevenue', 'currency', 'mediaType', 'native');
expect(dataItem).to.have.keys('requestId', 'cpm', 'ttl', 'creativeId', 'netRevenue', 'currency', 'mediaType', 'native', 'meta');
expect(dataItem.native).to.have.keys('clickUrl', 'impressionTrackers', 'title', 'image')
expect(dataItem.requestId).to.equal('23fhj33i987f');
expect(dataItem.cpm).to.equal(0.4);
Expand Down Expand Up @@ -263,7 +261,7 @@ describe('SmartyadsAdapter', function () {
});
});
describe('getUserSyncs', function () {
const syncUrl = 'https://as.ck-ie.com/prebidjs?p=7c47322e527cf8bdeb7facc1bb03387a&gdpr=0&gdpr_consent=&type=iframe&us_privacy=&gpp=';
const syncUrl = 'https://as.ck-ie.com/prebidjs?p=7c47322e527cf8bdeb7facc1bb03387a/iframe?pbjs=1&coppa=0';
const syncOptions = {
iframeEnabled: true
};
Expand All @@ -277,79 +275,4 @@ describe('SmartyadsAdapter', function () {
]);
});
});

describe('onBidWon', function () {
it('should exists', function () {
expect(spec.onBidWon).to.exist.and.to.be.a('function');
});

it('should send a valid bid won notice', function () {
const bid = {
'c': 'o',
'm': 'prebid',
'secret_key': 'prebid_js',
'winTest': '1',
'postData': [{
'bidder': 'smartyads',
'params': [
{'host': 'prebid',
'accountid': '123',
'sourceid': '12345'
}]
}]
};
spec.onBidWon(bid);
expect(server.requests.length).to.equal(1);
});
});

describe('onTimeout', function () {
it('should exists', function () {
expect(spec.onTimeout).to.exist.and.to.be.a('function');
});

it('should send a valid bid timeout notice', function () {
const bid = {
'c': 'o',
'm': 'prebid',
'secret_key': 'prebid_js',
'bidTimeout': '1',
'postData': [{
'bidder': 'smartyads',
'params': [
{'host': 'prebid',
'accountid': '123',
'sourceid': '12345'
}]
}]
};
spec.onTimeout(bid);
expect(server.requests.length).to.equal(1);
});
});

describe('onBidderError', function () {
it('should exists', function () {
expect(spec.onBidderError).to.exist.and.to.be.a('function');
});

it('should send a valid bidder error notice', function () {
const bid = {
'c': 'o',
'm': 'prebid',
'secret_key': 'prebid_js',
'bidderError': '1',
'postData': [{
'bidder': 'smartyads',
'params': [
{'host': 'prebid',
'accountid': '123',
'sourceid': '12345'
}]
}]
};
spec.onBidderError(bid);
expect(server.requests.length).to.equal(1);
});
});
});