diff --git a/extensions/amp-ad-network-smartadserver-impl/0.1/amp-ad-network-smartadserver-impl.js b/extensions/amp-ad-network-smartadserver-impl/0.1/amp-ad-network-smartadserver-impl.js index e078b82ed7a5..74b2079dcd51 100644 --- a/extensions/amp-ad-network-smartadserver-impl/0.1/amp-ad-network-smartadserver-impl.js +++ b/extensions/amp-ad-network-smartadserver-impl/0.1/amp-ad-network-smartadserver-impl.js @@ -16,6 +16,7 @@ import {buildUrl} from '#ads/google/a4a/shared/url-builder'; +import {intersectionEntryToJson} from '#core/dom/layout/intersection'; import {getPageLayoutBoxBlocking} from '#core/dom/layout/page-layout-box'; import {hasOwn} from '#core/types/object'; import {tryParseJson} from '#core/types/object/json'; @@ -25,7 +26,11 @@ import {Services} from '#service'; import {dev} from '#utils/log'; import {getOrCreateAdCid} from '../../../src/ad-cid'; -import {getConsentPolicyInfo} from '../../../src/consent'; +import { + getConsentDataToForward, + getConsentPolicyInfo, +} from '../../../src/consent'; +import {getContextMetadata} from '../../../src/iframe-attributes'; import {AmpA4A, XORIGIN_MODE} from '../../amp-a4a/0.1/amp-a4a'; /** @type {string} */ @@ -60,6 +65,29 @@ export class AmpAdNetworkSmartadserverImpl extends AmpA4A { this.addListener(); } + /** @override */ + renderViaIframeGet_(adUrl) { + this.maybeTriggerAnalyticsEvent_('renderCrossDomainStart'); + return getConsentDataToForward(this.element, this.getConsentPolicy()).then( + (consentData) => { + const contextMetadata = getContextMetadata( + this.win, + this.element, + this.sentinel, + {'consentSharedData': consentData} + ); + + const intersection = this.element.getIntersectionChangeEntry(); + contextMetadata['_context']['initialIntersection'] = + intersectionEntryToJson(intersection); + return this.iframeRenderHelper_({ + 'src': Services.xhrFor(this.win).getCorsUrl(this.win, adUrl), + 'name': JSON.stringify(contextMetadata), + }); + } + ); + } + /** @override */ getAdUrl(opt_consentTuple, opt_rtcResponsesPromise) { return Promise.any([ diff --git a/extensions/amp-ad-network-smartadserver-impl/0.1/test/test-amp-ad-network-smartadserver-impl.js b/extensions/amp-ad-network-smartadserver-impl/0.1/test/test-amp-ad-network-smartadserver-impl.js index 046af00a339b..55e3709eb913 100644 --- a/extensions/amp-ad-network-smartadserver-impl/0.1/test/test-amp-ad-network-smartadserver-impl.js +++ b/extensions/amp-ad-network-smartadserver-impl/0.1/test/test-amp-ad-network-smartadserver-impl.js @@ -23,6 +23,8 @@ import {Services} from '#service'; import {createIframeWithMessageStub} from '#testing/iframe'; +import * as consent from '../../../../src/consent'; +import * as iframe from '../../../../src/iframe-attributes'; import {XORIGIN_MODE} from '../../../amp-a4a/0.1/amp-a4a'; import {AmpAdNetworkSmartadserverImpl} from '../amp-ad-network-smartadserver-impl'; @@ -173,6 +175,51 @@ describes.realWin('amp-ad-network-smartadserver-impl', realWinConfig, (env) => { }); }); + describe('renderViaIframeGet_', () => { + let getContextMetadataStub; + beforeEach(() => { + getContextMetadataStub = env.sandbox + .stub(iframe, 'getContextMetadata') + .returns({ + _context: {}, + }); + env.sandbox + .stub(consent, 'getConsentDataToForward') + .resolves({consentString: 'constent', gdprApplies: true}); + element = createElementWithAttributes(doc, 'amp-ad', { + width: '300', + height: '250', + type: 'smartadserver', + }); + element.getIntersectionChangeEntry = () => ({ + rootBounds: {}, + intersectionRect: {}, + boundingClientRect: {}, + }); + impl = new AmpAdNetworkSmartadserverImpl(element); + env.sandbox.stub(impl, 'iframeRenderHelper_'); + }); + afterEach(() => { + env.sandbox.restore(); + }); + it('should call maybeTriggerAnalyticsEvent_', async () => { + const spy = env.sandbox.spy(impl, 'maybeTriggerAnalyticsEvent_'); + expect(spy.called).to.be.false; + impl.renderViaIframeGet_('fakeURL').then(() => { + expect(spy.called).to.be.true; + }); + }); + it('should call getContextMetadata with a consent data', async () => { + expect(getContextMetadataStub.called).to.be.false; + impl.renderViaIframeGet_('fakeURL').then(() => { + expect(getContextMetadataStub.called).to.be.true; + expect(getContextMetadataStub.getCall(0).args[3]).to.be.deep.equal({ + 'consentSharedData': {consentString: 'constent', gdprApplies: true}, + }); + }); + }); + }); + describe('getAdUrl', () => { it('should return proper url with vendor(default) data', async () => { element = createElementWithAttributes(doc, 'amp-ad', {