Skip to content

Commit

Permalink
✨[amp-ad smartadserver] Include consent data in iframe name (#39965)
Browse files Browse the repository at this point in the history
* SADR-5675 include consent for GPT

* SADR-5675 include consent for GPT update

* SADR-5675 fixes for prettier and eslint
  • Loading branch information
eszponder authored Apr 23, 2024
1 parent 810bc94 commit 199d1b3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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} */
Expand Down Expand Up @@ -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([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit 199d1b3

Please sign in to comment.