Skip to content

Commit

Permalink
teads Bid Adapter: get OpenGraph title (#10439)
Browse files Browse the repository at this point in the history
* teadsBidAdapter: Also get OpenGraph title

* Improv + add tests

* Add tests over title + description payload length

* Add tests for catch case when cross-origin
  • Loading branch information
github-mickael-leclerc authored Sep 12, 2023
1 parent 4f8cf68 commit b8a39f4
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 28 deletions.
12 changes: 7 additions & 5 deletions modules/teadsBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ function getReferrerInfo(bidderRequest) {

function getPageTitle() {
try {
return window.top.document.title || document.title;
const ogTitle = window.top.document.querySelector('meta[property="og:title"]')

return window.top.document.title || (ogTitle && ogTitle.content) || '';
} catch (e) {
return document.title;
const ogTitle = document.querySelector('meta[property="og:title"]')

return document.title || (ogTitle && ogTitle.content) || '';
}
}

Expand All @@ -185,9 +189,7 @@ function getPageDescription() {

try {
element = window.top.document.querySelector('meta[name="description"]') ||
window.top.document.querySelector('meta[property="og:description"]') ||
document.querySelector('meta[name="description"]') ||
document.querySelector('meta[property="og:description"]')
window.top.document.querySelector('meta[property="og:description"]')
} catch (e) {
element = document.querySelector('meta[name="description"]') ||
document.querySelector('meta[property="og:description"]')
Expand Down
122 changes: 99 additions & 23 deletions test/spec/modules/teadsBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import {expect} from 'chai';
import {spec, storage} from 'modules/teadsBidAdapter.js';
import {newBidder} from 'src/adapters/bidderFactory.js';
import {getStorageManager} from 'src/storageManager';

const ENDPOINT = 'https://a.teads.tv/hb/bid-request';
const AD_SCRIPT = '<script type="text/javascript" class="teads" async="true" src="https://a.teads.tv/hb/getAdSettings"></script>"';

describe('teadsBidAdapter', () => {
const adapter = newBidder(spec);
let cookiesAreEnabledStub, getCookieStub;
let sandbox;

beforeEach(function () {
cookiesAreEnabledStub = sinon.stub(storage, 'cookiesAreEnabled');
getCookieStub = sinon.stub(storage, 'getCookie');
sandbox = sinon.sandbox.create();
});

afterEach(function () {
cookiesAreEnabledStub.restore();
getCookieStub.restore();
sandbox.restore();
});

describe('inherited functions', () => {
Expand Down Expand Up @@ -257,20 +254,99 @@ describe('teadsBidAdapter', () => {
expect(payload.pageReferrer).to.deep.equal(document.referrer);
});

it('should add pageTitle info to payload', function () {
const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);
describe('pageTitle', function () {
it('should add pageTitle info to payload based on document title', function () {
const testText = 'This is a title';
sandbox.stub(window.top.document, 'title').value(testText);

const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageTitle).to.exist;
expect(payload.pageTitle).to.deep.equal(testText);
});

it('should add pageTitle info to payload based on open-graph title', function () {
const testText = 'This is a title from open-graph';
sandbox.stub(window.top.document, 'title').value('');
sandbox.stub(window.top.document, 'querySelector').withArgs('meta[property="og:title"]').returns({ content: testText });

const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageTitle).to.exist;
expect(payload.pageTitle).to.deep.equal(testText);
});

it('should add pageTitle info to payload sliced on 300 first characters', function () {
const testText = Array(500).join('a');
sandbox.stub(window.top.document, 'title').value(testText);

const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageTitle).to.exist;
expect(payload.pageTitle).to.have.length(300);
});

it('should add pageTitle info to payload when fallbacking from window.top', function () {
const testText = 'This is a fallback title';
sandbox.stub(window.top.document, 'querySelector').throws();
sandbox.stub(document, 'title').value(testText);

expect(payload.pageTitle).to.exist;
expect(payload.pageTitle).to.deep.equal(window.top.document.title || document.title);
const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageTitle).to.exist;
expect(payload.pageTitle).to.deep.equal(testText);
});
});

it('should add pageDescription info to payload', function () {
const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);
describe('pageDescription', function () {
it('should add pageDescription info to payload based on open-graph description', function () {
const testText = 'This is a description';
sandbox.stub(window.top.document, 'querySelector').withArgs('meta[name="description"]').returns({ content: testText });

const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageDescription).to.exist;
expect(payload.pageDescription).to.deep.equal(testText);
});

it('should add pageDescription info to payload based on open-graph description', function () {
const testText = 'This is a description from open-graph';
sandbox.stub(window.top.document, 'querySelector').withArgs('meta[property="og:description"]').returns({ content: testText });

const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageDescription).to.exist;
expect(payload.pageDescription).to.deep.equal(testText);
});

it('should add pageDescription info to payload sliced on 300 first characters', function () {
const testText = Array(500).join('a');
sandbox.stub(window.top.document, 'querySelector').withArgs('meta[name="description"]').returns({ content: testText });

const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageDescription).to.exist;
expect(payload.pageDescription).to.have.length(300);
});

it('should add pageDescription info to payload when fallbacking from window.top', function () {
const testText = 'This is a fallback description';
sandbox.stub(window.top.document, 'querySelector').throws();
sandbox.stub(document, 'querySelector').withArgs('meta[name="description"]').returns({ content: testText });

expect(payload.pageDescription).to.exist;
expect(payload.pageDescription).to.deep.equal('');
const request = spec.buildRequests(bidRequests, bidderRequestDefault);
const payload = JSON.parse(request.data);

expect(payload.pageDescription).to.exist;
expect(payload.pageDescription).to.deep.equal(testText);
});
});

it('should add timeToFirstByte info to payload', function () {
Expand Down Expand Up @@ -697,7 +773,7 @@ describe('teadsBidAdapter', () => {
describe('First-party cookie Teads ID', function () {
it('should not add firstPartyCookieTeadsId param to payload if cookies are not enabled' +
' and teads user id not available', function () {
cookiesAreEnabledStub.returns(false);
sandbox.stub(storage, 'cookiesAreEnabled').returns(false);

const bidRequest = {
...baseBidRequest,
Expand All @@ -714,8 +790,8 @@ describe('teadsBidAdapter', () => {

it('should not add firstPartyCookieTeadsId param to payload if cookies are enabled ' +
'but first-party cookie and teads user id are not available', function () {
cookiesAreEnabledStub.returns(true);
getCookieStub.withArgs('_tfpvi').returns(undefined);
sandbox.stub(storage, 'cookiesAreEnabled').returns(true);
sandbox.stub(storage, 'getCookie').withArgs('_tfpvi').returns(undefined);

const bidRequest = {
...baseBidRequest,
Expand All @@ -732,8 +808,8 @@ describe('teadsBidAdapter', () => {

it('should add firstPartyCookieTeadsId from cookie if it\'s available ' +
'and teads user id is not', function () {
cookiesAreEnabledStub.returns(true);
getCookieStub.withArgs('_tfpvi').returns('my-teads-id');
sandbox.stub(storage, 'cookiesAreEnabled').returns(true);
sandbox.stub(storage, 'getCookie').withArgs('_tfpvi').returns('my-teads-id');

const bidRequest = {
...baseBidRequest,
Expand All @@ -751,8 +827,8 @@ describe('teadsBidAdapter', () => {

it('should add firstPartyCookieTeadsId from user id module if it\'s available ' +
'even if cookie is available too', function () {
cookiesAreEnabledStub.returns(true);
getCookieStub.withArgs('_tfpvi').returns('my-teads-id');
sandbox.stub(storage, 'cookiesAreEnabled').returns(true);
sandbox.stub(storage, 'getCookie').withArgs('_tfpvi').returns('my-teads-id');

const bidRequest = {
...baseBidRequest,
Expand Down

0 comments on commit b8a39f4

Please sign in to comment.