Skip to content

AdHash Bidder Adapter: update for brand safety #10087

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

Merged
merged 35 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e7d3b8a
AdHash Bidder Adapter: minor changes
wyand-sp Jan 7, 2022
814f3a8
Implemented brand safety
wyand-sp Jan 18, 2022
d5e34de
Fix for GDPR consent
wyand-sp Mar 10, 2022
4eb1553
Merge branch 'prebid:master' into master
wyand-sp Mar 10, 2022
bdc4fad
Ad fraud prevention formula changed
wyand-sp Mar 16, 2022
35f24dc
AdHash brand safety additions
wyand-sp Apr 14, 2022
e681fbd
AdHash brand safety updates
wyand-sp Jun 28, 2022
c837c36
Merge branch 'master' of https://github.com/AdHashProtocol/Prebid.js
wyand-sp Aug 5, 2022
a6c5faf
AdHash Analytics adapter
wyand-sp Aug 5, 2022
fc5809d
Merge branch 'prebid:master' into master
wyand-sp Aug 10, 2022
3fa0e52
Support for recent ads
wyand-sp Aug 17, 2022
66fddd1
Fix for timestamp
wyand-sp Aug 18, 2022
0c7953c
PUB-222
Sep 15, 2022
66d14ba
Unit tests for the analytics adapter
Sep 19, 2022
1c2470a
Removed export causing errors
Sep 19, 2022
6e4148b
Added globalScript parameter
wyand-sp Sep 26, 2022
ae443ae
Merge branch 'prebid:master' into master
wyand-sp Oct 4, 2022
441240d
PUB-227
vsaraminev Oct 4, 2022
a589f12
Merge branch 'prebid:master' into master
wyand-sp Oct 12, 2022
8dacbc9
GEN-964
M1TKO Oct 28, 2022
9cd75bf
GEN-1025
Nov 16, 2022
dbd9f3d
Removing the analytics adaptor
wyand-sp Nov 28, 2022
65a0d26
Fix for regexp match
wyand-sp Nov 28, 2022
e27cbe4
Version change
wyand-sp Nov 28, 2022
d68efac
MINOR
wyand-sp Dec 7, 2022
e8ab5c2
Merge branch 'prebid:master' into master
wyand-sp Feb 7, 2023
78dea30
GEN-1153
Apr 27, 2023
340e473
MINOR
wyand-sp Apr 28, 2023
cc6994b
Merge branch 'prebid:master' into master
wyand-sp Apr 28, 2023
9ee05e2
Removing globalScript flag
wyand-sp May 9, 2023
2d9d9f2
Merge branch 'prebid:master' into master
wyand-sp May 18, 2023
17cce26
Merge branch 'prebid:master' into master
wyand-sp May 18, 2023
c5942e5
Merge branch 'prebid:master' into master
wyand-sp May 19, 2023
e4cf0cd
Merge branch 'prebid:master' into master
wyand-sp May 24, 2023
661f4b6
Brand safety change
wyand-sp May 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 56 additions & 34 deletions modules/adhashBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';
import { includes } from '../src/polyfill.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';

const VERSION = '3.2';
const VERSION = '3.6';
const BAD_WORD_STEP = 0.1;
const BAD_WORD_MIN = 0.2;
const ADHASH_BIDDER_CODE = 'adhash';
Expand All @@ -19,6 +19,8 @@ const ADHASH_BIDDER_CODE = 'adhash';
* @returns boolean flag is the page safe
*/
function brandSafety(badWords, maxScore) {
const delimiter = '~';

/**
* Performs the ROT13 encoding on the string argument and returns the resulting string.
* The Adhash bidder uses ROT13 so that the response is not blocked by:
Expand All @@ -40,17 +42,17 @@ function brandSafety(badWords, maxScore) {
/**
* Calculates the scoring for each bad word with dimishing returns
* @param {integer} points points that this word costs
* @param {integer} occurances number of occurances
* @param {integer} occurrences number of occurrences
* @returns {float} final score
*/
const scoreCalculator = (points, occurances) => {
const scoreCalculator = (points, occurrences) => {
let positive = true;
if (points < 0) {
points *= -1;
positive = false;
}
let result = 0;
for (let i = 0; i < occurances; i++) {
for (let i = 0; i < occurrences; i++) {
result += Math.max(points - i * BAD_WORD_STEP, BAD_WORD_MIN);
}
return positive ? result : -result;
Expand All @@ -60,22 +62,50 @@ function brandSafety(badWords, maxScore) {
* Checks what rule will match in the given array with words
* @param {string} rule rule type (full, partial, starts, ends, regexp)
* @param {string} decodedWord decoded word
* @param {array} wordsToMatch array to find a match
* @param {string} wordsToMatch list of all words on the page separated by delimiters
* @returns {object|boolean} matched rule and occurances. If nothing is matched returns false
*/
const wordsMatchedWithRule = function (rule, decodedWord, wordsToMatch) {
if (rule === 'full' && wordsToMatch && wordsToMatch.includes(decodedWord)) {
return { rule, occurances: wordsToMatch.filter(element => element === decodedWord).length };
} else if (rule === 'partial' && wordsToMatch && wordsToMatch.some(element => element.indexOf(decodedWord) > -1)) {
return { rule, occurances: wordsToMatch.filter(element => element.indexOf(decodedWord) > -1).length };
} else if (rule === 'starts' && wordsToMatch && wordsToMatch.some(word => word.startsWith(decodedWord))) {
return { rule, occurances: wordsToMatch.filter(element => element.startsWith(decodedWord)).length };
} else if (rule === 'ends' && wordsToMatch && wordsToMatch.some(word => word.endsWith(decodedWord))) {
return { rule, occurances: wordsToMatch.filter(element => element.endsWith(decodedWord)).length };
} else if (rule === 'regexp' && wordsToMatch && wordsToMatch.some(element => element.match(new RegExp(decodedWord, 'i')))) {
return { rule, occurances: wordsToMatch.filter(element => element.match(new RegExp(decodedWord, 'i'))).length };
if (!wordsToMatch) {
return false;
}

let occurrences;
let adjustedWordToMatch;
decodedWord = decodedWord.split(' ').join(`${delimiter}${delimiter}`);
switch (rule) {
case 'full':
adjustedWordToMatch = `${delimiter}${decodedWord}${delimiter}`;
break;
case 'partial':
adjustedWordToMatch = decodedWord;
break;
case 'starts':
adjustedWordToMatch = `${delimiter}${decodedWord}`;
break;
case 'ends':
adjustedWordToMatch = `${decodedWord}${delimiter}`;
break;
case 'combo':
const allOccurrences = [];
const paddedWordsToMatch = `${delimiter}${wordsToMatch}${delimiter}`;
const decodedWordsSplit = decodedWord.split(`${delimiter}${delimiter}`);
for (const decodedWordPart of decodedWordsSplit) {
adjustedWordToMatch = `${delimiter}${decodedWordPart}${delimiter}`;
allOccurrences.push(paddedWordsToMatch.split(adjustedWordToMatch).length - 1);
}
occurrences = Math.min(...allOccurrences);
return occurrences > 0 ? { rule, occurrences } : false;
case 'regexp':
occurrences = [...wordsToMatch.matchAll(new RegExp(decodedWord, 'gi'))].length;
return occurrences > 0 ? { rule, occurrences } : false;
default:
return false;
}
return false;

const paddedWordsToMatch = `${delimiter}${wordsToMatch}${delimiter}`;
occurrences = paddedWordsToMatch.split(adjustedWordToMatch).length - 1;
return occurrences > 0 ? { rule, occurrences } : false;
};

// Default parameters if the bidder is unable to send some of them
Expand All @@ -91,11 +121,11 @@ function brandSafety(badWords, maxScore) {
.toLowerCase()
.trim();
const content = window.top.document.body.innerText.toLowerCase();
const contentWords = content.trim().split(/\s+/).length;
// \p{L} matches a single unicode code point in the category 'letter'. Matches any kind of letter from any language.
const regexp = new RegExp('[\\p{L}]+', 'gu');
const words = content.match(regexp);
const wordsInUrl = wordsAndNumbersInUrl.match(regexp);
const wordsMatched = content.match(regexp);
const words = wordsMatched.join(`${delimiter}${delimiter}`);
const wordsInUrl = wordsAndNumbersInUrl.match(regexp).join(`${delimiter}${delimiter}`);

for (const [word, rule, points] of badWords) {
const decodedWord = rot13(word.toLowerCase());
Expand All @@ -110,19 +140,11 @@ function brandSafety(badWords, maxScore) {

// Check if site content's words match any of our brand safety rules
const matchedRule = wordsMatchedWithRule(rule, decodedWord, words);
if (matchedRule.rule === 'full') {
score += scoreCalculator(points, matchedRule.occurances);
} else if (matchedRule.rule === 'partial') {
score += scoreCalculator(points, matchedRule.occurances);
} else if (matchedRule.rule === 'starts') {
score += scoreCalculator(points, matchedRule.occurances);
} else if (matchedRule.rule === 'ends') {
score += scoreCalculator(points, matchedRule.occurances);
} else if (matchedRule.rule === 'regexp') {
score += scoreCalculator(points, matchedRule.occurances);
if (matchedRule !== false) {
score += scoreCalculator(points, matchedRule.occurrences);
}
}
return score < (maxScore * contentWords) / 1000;
return score < (maxScore * wordsMatched.length) / 1000;
} catch (e) {
return true;
}
Expand Down Expand Up @@ -183,8 +205,8 @@ export const spec = {
}

// Needed for the ad density calculation
var adHeight = validBidRequests[i].sizes[index][1];
var adWidth = validBidRequests[i].sizes[index][0];
const adHeight = validBidRequests[i].sizes[index][1];
const adWidth = validBidRequests[i].sizes[index][0];
if (!window.adsCount) {
window.adsCount = 0;
}
Expand Down Expand Up @@ -247,7 +269,7 @@ export const spec = {
const bidderResponse = JSON.stringify({ responseText: JSON.stringify(responseBody) });
const requestData = JSON.stringify(request.data);

var response = {
let response = {
requestId: request.bidRequest.bidId,
cpm: responseBody.creatives[0].costEUR,
width: request.bidRequest.sizes[0][0],
Expand Down
20 changes: 18 additions & 2 deletions test/spec/modules/adhashBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('adhashBidAdapter', function () {
);
expect(result.length).to.equal(1);
expect(result[0].method).to.equal('POST');
expect(result[0].url).to.equal('https://bidder.adhash.com/rtb?version=3.2&prebid=true&publisher=0xc3b09b27e9c6ef73957901aa729b9e69e5bbfbfb');
expect(result[0].url).to.equal('https://bidder.adhash.com/rtb?version=3.6&prebid=true&publisher=0xc3b09b27e9c6ef73957901aa729b9e69e5bbfbfb');
expect(result[0].bidRequest).to.equal(bidRequest);
expect(result[0].data).to.have.property('timezone');
expect(result[0].data).to.have.property('location');
Expand All @@ -104,7 +104,7 @@ describe('adhashBidAdapter', function () {
const result = spec.buildRequests([ bidRequest ], { gdprConsent: { gdprApplies: true, consentString: 'example' } });
expect(result.length).to.equal(1);
expect(result[0].method).to.equal('POST');
expect(result[0].url).to.equal('https://bidder.adhash.com/rtb?version=3.2&prebid=true&publisher=0xc3b09b27e9c6ef73957901aa729b9e69e5bbfbfb');
expect(result[0].url).to.equal('https://bidder.adhash.com/rtb?version=3.6&prebid=true&publisher=0xc3b09b27e9c6ef73957901aa729b9e69e5bbfbfb');
expect(result[0].bidRequest).to.equal(bidRequest);
expect(result[0].data).to.have.property('timezone');
expect(result[0].data).to.have.property('location');
Expand Down Expand Up @@ -152,6 +152,8 @@ describe('adhashBidAdapter', function () {
['дума', 'full', 1],
['старт', 'starts', 1],
['край', 'ends', 1],
['onq jbeq', 'partial', 1],
['dhrra qvrf', 'combo', 2],
],
maxScore: 2
}
Expand Down Expand Up @@ -196,6 +198,13 @@ describe('adhashBidAdapter', function () {
expect(spec.interpretResponse(serverResponse, request).length).to.equal(0);
});

it('should return empty array when there are bad words (partial, compound phrase)', function () {
bodyStub = sinon.stub(window.top.document.body, 'innerText').get(function() {
return 'example text partialbad wordb bad wordb example bad wordbtext' + ' word'.repeat(994);
});
expect(spec.interpretResponse(serverResponse, request).length).to.equal(0);
});

it('should return empty array when there are bad words (starts)', function () {
bodyStub = sinon.stub(window.top.document.body, 'innerText').get(function() {
return 'example text startsWith starts text startsAgain' + ' word'.repeat(994);
Expand Down Expand Up @@ -224,6 +233,13 @@ describe('adhashBidAdapter', function () {
expect(spec.interpretResponse(serverResponse, request).length).to.equal(0);
});

it('should return empty array when there are bad words (combo)', function () {
bodyStub = sinon.stub(window.top.document.body, 'innerText').get(function() {
return 'queen of england dies, the queen dies' + ' word'.repeat(993);
});
expect(spec.interpretResponse(serverResponse, request).length).to.equal(0);
});

it('should return empty array when there are bad words (regexp)', function () {
bodyStub = sinon.stub(window.top.document.body, 'innerText').get(function() {
return 'example text xxxayyy zzxxxAyyyzz text xxxbyyy' + ' word'.repeat(994);
Expand Down