Skip to content

Commit

Permalink
Doubleclick Fast Fetch SRA Beta and AMP creative handling (#10160)
Browse files Browse the repository at this point in the history
* Allow for beta SRA doubleclick; ensure AMP creatives removed on SRA if non-AMP present
  • Loading branch information
keithwrightbos authored and muxin committed Jun 27, 2017
1 parent a56f22c commit 044f1b9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
10 changes: 9 additions & 1 deletion extensions/amp-a4a/0.1/amp-a4a.js
Expand Up @@ -951,9 +951,17 @@ export class AmpA4A extends AMP.BaseElement {
return super.attemptChangeSize(newHeight, newWidth).catch(() => {});
}

/**
* @return {boolean} where AMP creatives rendered via FIE should
* be removed as part of unlayoutCallback. By default they remain.
*/
shouldUnlayoutAmpCreatives() {
return false;
}

/** @override */
unlayoutCallback() {
if (this.friendlyIframeEmbed_) {
if (this.friendlyIframeEmbed_ && !this.shouldUnlayoutAmpCreatives()) {
return false;
}
// Increment promiseId to cause any pending promise to cancel.
Expand Down
Expand Up @@ -213,11 +213,14 @@ export class AmpAdNetworkDoubleclickImpl extends AmpA4A {
this.adKey_ = 0;

// TODO(keithwrightbos) - how can pub enable?
/** @private @const {boolean} */
this.useSra_ = (getMode().localDev && /(\?|&)force_sra=true(&|$)/.test(
this.win.location.search)) ||
/** @protected @const {boolean} */
this.useSra = getMode().localDev && /(\?|&)force_sra=true(&|$)/.test(
this.win.location.search) ||
!!this.win.document.querySelector(
'meta[name=amp-ad-doubleclick-sra]') ||
experimentFeatureEnabled(this.win, DOUBLECLICK_EXPERIMENT_FEATURE.SRA);


const sraInitializer = this.initializeSraPromise_();
/** @protected {?function(?../../../src/service/xhr-impl.FetchResponse)} */
this.sraResponseResolver = sraInitializer.resolver;
Expand Down Expand Up @@ -327,7 +330,7 @@ export class AmpAdNetworkDoubleclickImpl extends AmpA4A {
/** @override */
getAdUrl() {
if (this.iframe) {
dev().warn(TAG, `Frame already exists, sra: ${this.useSra_}`);
dev().warn(TAG, `Frame already exists, sra: ${this.useSra}`);
return '';
}
// TODO(keithwrightbos): SRA blocks currently unnecessarily generate full
Expand Down Expand Up @@ -376,6 +379,15 @@ export class AmpAdNetworkDoubleclickImpl extends AmpA4A {
this.lifecycleReporter_.sendPing(eventName);
}

/** @override */
shouldUnlayoutAmpCreatives() {
// If using SRA, remove AMP creatives if we have at least one non-AMP
// creative present.
return this.useSra && !!this.win.document.querySelector(
'amp-ad[data-a4a-upgrade-type="amp-ad-network-doubleclick-impl"] ' +
'iframe[src]');
}

/** @override */
unlayoutCallback() {
super.unlayoutCallback();
Expand Down Expand Up @@ -469,7 +481,7 @@ export class AmpAdNetworkDoubleclickImpl extends AmpA4A {

/** @override */
sendXhrRequest(adUrl) {
if (!this.useSra_) {
if (!this.useSra) {
return super.sendXhrRequest(adUrl);
}
// Wait for SRA request which will call response promise when this block's
Expand Down
Expand Up @@ -553,6 +553,80 @@ describes.sandboxed('amp-ad-network-doubleclick-impl', {}, () => {
});
});

describe('#SRA enabled', () => {
let fixture;

beforeEach(() => {
return createIframePromise().then(f => {
setupForAdTesting(f);
fixture = f;
});
});

it('should be disabled by default', () => {
const element = createElementWithAttributes(
fixture.doc, 'amp-ad', {
type: 'doubleclick',
height: 320,
width: 50,
});
fixture.doc.body.appendChild(element);
const impl = new AmpAdNetworkDoubleclickImpl(element);
expect(impl.useSra).to.be.false;
});

it('should be enabled if meta tag present', () => {
const metaElement = createElementWithAttributes(fixture.doc, 'meta', {
name: 'amp-ad-doubleclick-sra',
});
fixture.doc.head.appendChild(metaElement);
const element = createElementWithAttributes(
fixture.doc, 'amp-ad', {
type: 'doubleclick',
height: 320,
width: 50,
});
fixture.doc.body.appendChild(element);
const impl = new AmpAdNetworkDoubleclickImpl(element);
expect(impl.useSra).to.be.true;
});
});

describe('#SRA AMP creative unlayoutCallback', () => {
let impl;

beforeEach(() => {
return createIframePromise().then(f => {
setupForAdTesting(f);
const element = createElementWithAttributes(
f.doc, 'amp-ad', {
type: 'doubleclick',
height: 320,
width: 50,
'data-a4a-upgrade-type': 'amp-ad-network-doubleclick-impl',
});
f.doc.body.appendChild(element);
const iframe = createElementWithAttributes(
f.doc, 'iframe', {
src: 'https://foo.com',
height: 320,
width: 50,
});
element.appendChild(iframe);
impl = new AmpAdNetworkDoubleclickImpl(element);
});
});

it('should not remove if not SRA', () => {
expect(impl.shouldUnlayoutAmpCreatives()).to.be.false;
});

it('should remove if SRA and has frame', () => {
impl.useSra = true;
expect(impl.shouldUnlayoutAmpCreatives()).to.be.true;
});
});

describe('#constructSRABlockParameters', () => {
let fixture;

Expand Down Expand Up @@ -644,7 +718,7 @@ describes.sandboxed('amp-ad-network-doubleclick-impl', {}, () => {
element.getIntersectionChangeEntry = () => {return null;};
doc.body.appendChild(element);
const impl = new AmpAdNetworkDoubleclickImpl(element);
impl.useSra_ = true;
impl.useSra = true;
return impl;
}

Expand Down

0 comments on commit 044f1b9

Please sign in to comment.