Skip to content

Commit

Permalink
Don't load responsive ad if tag height is not AdSense whitelisted height
Browse files Browse the repository at this point in the history
  • Loading branch information
Noran Azmy committed Jan 5, 2018
1 parent 8f0c738 commit bffaa1a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
11 changes: 11 additions & 0 deletions ads/google/adsense.js
Expand Up @@ -15,8 +15,10 @@
*/

import {validateData} from '../../3p/3p';
import {user} from '../../src/log';
import {setStyles} from '../../src/style';
import {camelCaseToDash} from '../../src/string';
import {ADSENSE_RSPV_WHITELISTED_HEIGHT} from './utils';

/**
* Make an adsense iframe.
Expand All @@ -29,6 +31,15 @@ export function adsense(global, data) {
['adClient', 'adSlot', 'adHost', 'adtest', 'tagOrigin', 'experimentId',
'ampSlotIndex', 'adChannel', 'autoFormat', 'fullWidth']);

if (data['autoFormat'] === 'rspv') {
user().assert(
data['height'] === ADSENSE_RSPV_WHITELISTED_HEIGHT,
'Specified height ' + data['height'] +
' in <amp-ad> tag is not equal to the required height of ' +
ADSENSE_RSPV_WHITELISTED_HEIGHT +
' for responsive AdSense ad units.');
}

if (global.context.clientId) {
// Read by GPT for GA/GPT integration.
global.gaGlobal = {
Expand Down
8 changes: 8 additions & 0 deletions ads/google/test/test-adsense.js
Expand Up @@ -71,4 +71,12 @@ describes.realWin('adsenseDelayedFetch', {}, env => {
hid: pageViewId,
});
});

it('throws on invalid responsive ad unit height', () => {
const data = {'autoFormat': 'rspv', 'height': '666'};
expect(() => {
adsense(env.win, data);
}).to.throw(
/Specified height 666 in <amp-ad> tag is not equal to the required/);
});
});
6 changes: 6 additions & 0 deletions ads/google/utils.js
Expand Up @@ -16,6 +16,12 @@

import {user} from '../../src/log';

/**
* Approved height for AdSense full-width responsive ads.
* @const {number}
*/
export const ADSENSE_RSPV_WHITELISTED_HEIGHT = 320;

/**
* Given the amp-ad data attribute containing the multi-size dimensions, and a
* set of primary dimensions, this function will return all valid multi-size
Expand Down
6 changes: 3 additions & 3 deletions examples/a4a-fullwidth.amp.html
Expand Up @@ -76,7 +76,7 @@ <h1>Content!</h1>
ac posuere velit semper.
</p>

<amp-ad width="100vw" height=150
<amp-ad width="100vw" height=320
type="adsense"
data-ad-client="ca-pub-2383777339857329"
data-ad-slot="4121425836"
Expand All @@ -97,7 +97,7 @@ <h1>Content!</h1>


<div class="further-indent">
<amp-ad width="100vw" height=150
<amp-ad width="100vw" height=320
type="adsense"
data-ad-client="ca-pub-2383777339857329"
data-ad-slot="4121425836"
Expand Down Expand Up @@ -142,7 +142,7 @@ <h1>Content!</h1>
nec lectus finibus rutrum vel sed orci.
</p>

<amp-ad width="100vw" height=150
<amp-ad width="100vw" height=320
type="adsense"
data-ad-client="ca-pub-2383777339857329"
data-ad-slot="4121425836"
Expand Down
Expand Up @@ -49,7 +49,7 @@ import {
import {removeElement} from '../../../src/dom';
import {getMode} from '../../../src/mode';
import {stringHash32} from '../../../src/string';
import {dev} from '../../../src/log';
import {dev, user} from '../../../src/log';
import {Services} from '../../../src/services';
import {domFingerprintPlain} from '../../../src/utils/dom-fingerprint';
import {clamp} from '../../../src/utils/math';
Expand All @@ -63,6 +63,7 @@ import {insertAnalyticsElement} from '../../../src/extension-analytics';
import {
getAdSenseAmpAutoAdsExpBranch,
} from '../../../ads/google/adsense-amp-auto-ads';
import {ADSENSE_RSPV_WHITELISTED_HEIGHT} from '../../../ads/google/utils';

/** @const {string} */
const ADSENSE_BASE_URL = 'https://googleads.g.doubleclick.net/pagead/ads';
Expand Down Expand Up @@ -159,6 +160,17 @@ export class AmpAdNetworkAdsenseImpl extends AmpA4A {
return this.autoFormat_ == 'rspv';
}

/**
* Returns true if the height is an approved height for full-width responsive
* ads.
* @return {boolean}
* @private
*/
hasValidResponsiveHeight_() {
return this.element.getAttribute('height') ==
ADSENSE_RSPV_WHITELISTED_HEIGHT;
}

/** @override */
isValidElement() {
/**
Expand All @@ -168,6 +180,15 @@ export class AmpAdNetworkAdsenseImpl extends AmpA4A {
* to an amp-ad-adsense element. Thus, if we are an amp-ad, we can be sure
* that it has been verified.
*/
if (this.isResponsive_() && !this.hasValidResponsiveHeight_()) {
user().error(TAG,
'Specified height ' +
this.element.getAttribute('height') +
' in <amp-ad> tag is not equal to the required height of ' +
ADSENSE_RSPV_WHITELISTED_HEIGHT +
' for responsive AdSense ad units.');
return false;
}
return !!this.element.getAttribute('data-ad-client') &&
this.isAmpAdElement();
}
Expand Down
Expand Up @@ -132,6 +132,18 @@ describes.realWin('amp-ad-network-adsense-impl', {
impl = new AmpAdNetworkAdsenseImpl(element);
expect(impl.isValidElement()).to.be.true;
});
it('should be valid (correct responsive ad height)', () => {
element.setAttribute('data-auto-format', 'rspv');
element.setAttribute('height', '320');
expect(impl.isValidElement()).to.be.true;
});
it('should be NOT valid (wrong responsive ad height)', () => {
element = createAdsenseImplElement(
{'data-auto-format': 'rsvp', 'height': '666'}, doc,
'amp-ad-network-adsense-impl');
impl = new AmpAdNetworkAdsenseImpl(element);
expect(impl.isValidElement()).to.be.false;
});
});

describe('#extractSize', () => {
Expand Down

0 comments on commit bffaa1a

Please sign in to comment.