-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
amp-ad-network-adsense-impl.js
512 lines (466 loc) · 16.6 KB
/
amp-ad-network-adsense-impl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Because AdSense and DoubleClick are both operated by Google and their A4A
// implementations share some behavior in common, part of the logic for this
// implementation is located in the ads/google/a4a directory rather than here.
// Most other ad networks will want to put their A4A code entirely in the
// extensions/amp-ad-network-${NETWORK_NAME}-impl directory.
import {AmpA4A} from '../../amp-a4a/0.1/amp-a4a';
import {
isInManualExperiment,
} from '../../../ads/google/a4a/traffic-experiments';
import {isExperimentOn} from '../../../src/experiments';
import {
additionalDimensions,
googleAdUrl,
isReportingEnabled,
extractAmpAnalyticsConfig,
addCsiSignalsToAmpAnalyticsConfig,
getCsiAmpAnalyticsConfig,
getCsiAmpAnalyticsVariables,
QQID_HEADER,
maybeAppendErrorParameter,
getEnclosingContainerTypes,
ValidAdContainerTypes,
getIdentityToken,
} from '../../../ads/google/a4a/utils';
import {
googleLifecycleReporterFactory,
setGoogleLifecycleVarsFromHeaders,
} from '../../../ads/google/a4a/google-data-reporter';
import {
installAnchorClickInterceptor,
} from '../../../src/anchor-click-interceptor';
import {removeElement} from '../../../src/dom';
import {getMode} from '../../../src/mode';
import {stringHash32} from '../../../src/string';
import {dev, user} from '../../../src/log';
import {Services} from '../../../src/services';
import {domFingerprintPlain} from '../../../src/utils/dom-fingerprint';
import {clamp} from '../../../src/utils/math';
import {
computedStyle,
setStyle,
setStyles,
} from '../../../src/style';
import {AdsenseSharedState} from './adsense-shared-state';
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';
/** @const {string} */
const TAG = 'amp-ad-network-adsense-impl';
/**
* Shared state for AdSense ad slots. This is used primarily for ad request url
* parameters that depend on previous slots.
* @const {!AdsenseSharedState}
*/
const sharedState = new AdsenseSharedState();
/** @visibleForTesting */
export function resetSharedState() {
sharedState.reset();
}
/** @final */
export class AmpAdNetworkAdsenseImpl extends AmpA4A {
/**
* @param {!Element} element
*/
constructor(element) {
super(element);
/**
* @type {!../../../ads/google/a4a/performance.GoogleAdLifecycleReporter}
*/
this.lifecycleReporter_ = this.lifecycleReporter_ ||
this.initLifecycleReporter();
/**
* A unique identifier for this slot.
* Not initialized until getAdUrl() is called; updated upon each invocation
* of getAdUrl().
* @private {?string}
*/
this.uniqueSlotId_ = null;
/**
* Config to generate amp-analytics element for active view reporting.
* @type {?JsonObject}
* @private
*/
this.ampAnalyticsConfig_ = null;
/** @private {!../../../src/service/extensions-impl.Extensions} */
this.extensions_ = Services.extensionsFor(this.win);
/** @private {?({width, height}|../../../src/layout-rect.LayoutRectDef)} */
this.size_ = null;
/**
* amp-analytics element generated based on this.ampAnalyticsConfig_
* @private {?Element}
*/
this.ampAnalyticsElement_ = null;
/** @private {?string} */
this.qqid_ = null;
/**
* For full-width responsive ads: whether the element has already been
* aligned to the edges of the viewport.
* @private {boolean}
*/
this.responsiveAligned_ = false;
/**
* The contents of the data-auto-format attribute, or empty string if the
* attribute was not set.
* @private {?string}
*/
this.autoFormat_ = null;
/** @private {?Promise<!../../../ads/google/a4a/utils.IdentityToken>} */
this.identityTokenPromise_ = null;
/**
* @private {?boolean} whether preferential rendered AMP creative, null
* indicates no creative render.
*/
this.isAmpCreative_ = null;
}
/**
* @return {boolean}
* @private
*/
isResponsive_() {
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() {
/**
* isValidElement used to also check that we are in a valid A4A environment,
* however this is not necessary as that is checked by adsenseIsA4AEnabled,
* which is always called as part of the upgrade path from an amp-ad element
* 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();
}
/** @override */
delayAdRequestEnabled() {
return true;
}
/** @override */
buildCallback() {
super.buildCallback();
this.identityTokenPromise_ = Services.viewerForDoc(this.getAmpDoc())
.whenFirstVisible()
.then(() => getIdentityToken(this.win, this.getAmpDoc()));
this.autoFormat_ =
this.element.getAttribute('data-auto-format') || '';
if (this.isResponsive_()) {
// Attempt to resize to the correct height. The width should already be
// 100vw, but is fixed here so that future resizes of the viewport don't
// affect it.
const viewportSize = this.getViewport().getSize();
return this.attemptChangeSize(
AmpAdNetworkAdsenseImpl.getResponsiveHeightForContext_(
viewportSize),
viewportSize.width);
}
}
/** @override */
getAdUrl() {
// TODO: Check for required and allowed parameters. Probably use
// validateData, from 3p/3p/js, after moving it someplace common.
const startTime = Date.now();
const global = this.win;
let adClientId = this.element.getAttribute('data-ad-client');
// Ensure client id format: lower case with 'ca-' prefix.
adClientId = adClientId.toLowerCase();
if (adClientId.substring(0, 3) != 'ca-') {
adClientId = 'ca-' + adClientId;
}
const adTestOn = this.element.getAttribute('data-adtest') ||
isInManualExperiment(this.element);
const width = Number(this.element.getAttribute('width'));
const height = Number(this.element.getAttribute('height'));
// Need to ensure these are numbers since width can be set to 'auto'.
// Checking height just in case.
const useDefinedSizes = isExperimentOn(this.win, 'as-use-attr-for-format')
&& !this.isResponsive_()
&& !isNaN(width) && width > 0
&& !isNaN(height) && height > 0;
this.size_ = useDefinedSizes
? {width, height}
: this.getIntersectionElementLayoutBox();
const format = `${this.size_.width}x${this.size_.height}`;
const slotId = this.element.getAttribute('data-amp-slot-index');
// data-amp-slot-index is set by the upgradeCallback method of amp-ad.
// TODO(bcassels): Uncomment the assertion, fixing the tests.
// But not all tests arrange to call upgradeCallback.
// dev().assert(slotId != undefined);
const adk = this.adKey_(format);
this.uniqueSlotId_ = slotId + adk;
const slotname = this.element.getAttribute('data-ad-slot');
const sharedStateParams = sharedState.addNewSlot(
format, this.uniqueSlotId_, adClientId, slotname);
const viewportSize = this.getViewport().getSize();
this.win['ampAdGoogleIfiCounter'] = this.win['ampAdGoogleIfiCounter'] || 1;
const enclosingContainers = getEnclosingContainerTypes(this.element);
const pfx = enclosingContainers.includes(
ValidAdContainerTypes['AMP-FX-FLYING-CARPET']) ||
enclosingContainers.includes(ValidAdContainerTypes['AMP-STICKY-AD']);
const parameters = {
'client': adClientId,
format,
'w': this.size_.width,
'h': this.size_.height,
'iu': slotname,
'adtest': adTestOn ? 'on' : null,
adk,
'output': 'html',
'bc': global.SVGElement && global.document.createElementNS ? '1' : null,
'ctypes': this.getCtypes_(),
'host': this.element.getAttribute('data-ad-host'),
'hl': this.element.getAttribute('data-language'),
'to': this.element.getAttribute('data-tag-origin'),
'pv': sharedStateParams.pv,
'channel': this.element.getAttribute('data-ad-channel'),
'wgl': global['WebGLRenderingContext'] ? '1' : '0',
'asnt': this.sentinel,
'dff': computedStyle(this.win, this.element)['font-family'],
'prev_fmts': sharedStateParams.prevFmts || null,
'prev_slotnames': sharedStateParams.prevSlotnames || null,
'brdim': additionalDimensions(this.win, viewportSize),
'ifi': this.win['ampAdGoogleIfiCounter']++,
'rc': this.fromResumeCallback ? 1 : null,
'rafmt': this.isResponsive_() ? 13 : null,
'pfx': pfx ? '1' : '0',
};
const experimentIds = [];
const ampAutoAdsBranch = getAdSenseAmpAutoAdsExpBranch(this.win);
if (ampAutoAdsBranch) {
experimentIds.push(ampAutoAdsBranch);
}
const identityPromise = Services.timerFor(this.win)
.timeoutPromise(1000, this.identityTokenPromise_)
.catch(unusedErr => {
// On error/timeout, proceed.
return /**@type {!../../../ads/google/a4a/utils.IdentityToken}*/(
{});
});
return identityPromise.then(identity => {
return googleAdUrl(
this, ADSENSE_BASE_URL, startTime, Object.assign(
{
adsid: identity.token || null,
jar: identity.jar || null,
pucrd: identity.pucrd || null,
},
parameters), experimentIds);
});
}
/** @override */
onNetworkFailure(error, adUrl) {
dev().info(TAG, 'network error, attempt adding of error parameter', error);
return {adUrl: maybeAppendErrorParameter(adUrl, 'n')};
}
/** @override */
extractSize(responseHeaders) {
setGoogleLifecycleVarsFromHeaders(responseHeaders, this.lifecycleReporter_);
this.ampAnalyticsConfig_ = extractAmpAnalyticsConfig(this, responseHeaders);
this.qqid_ = responseHeaders.get(QQID_HEADER);
if (this.ampAnalyticsConfig_) {
// Load amp-analytics extensions
this.extensions_./*OK*/installExtensionForDoc(
this.getAmpDoc(), 'amp-analytics');
}
return this.size_;
}
/**
* @param {string} format
* @return {string} The ad unit hash key string.
* @private
*/
adKey_(format) {
const element = this.element;
const slot = element.getAttribute('data-ad-slot') || '';
const string = `${slot}:${format}:${domFingerprintPlain(element)}`;
return stringHash32(string);
}
/**
* @return {?string}
* @private
*/
getCtypes_() {
if (!getMode().localDev) {
return null;
}
const ctypesReMatch = /[?&]force_a4a_ctypes=([^&]+)/.exec(
this.win.location.search);
// If the RE passes, then length is necessarily > 1.
if (ctypesReMatch) {
return ctypesReMatch[1];
}
return null;
}
/** @override */
emitLifecycleEvent(eventName, opt_extraVariables) {
if (opt_extraVariables) {
this.lifecycleReporter_.setPingParameters(opt_extraVariables);
}
this.lifecycleReporter_.sendPing(eventName);
}
/**
* @return {!../../../ads/google/a4a/performance.BaseLifecycleReporter}
*/
initLifecycleReporter() {
return googleLifecycleReporterFactory(this);
}
/** @override */
onCreativeRender(creativeMetaData) {
super.onCreativeRender(creativeMetaData);
this.isAmpCreative_ = !!creativeMetaData;
if (creativeMetaData &&
!creativeMetaData.customElementExtensions.includes('amp-ad-exit')) {
// Capture phase click handlers on the ad if amp-ad-exit not present
// (assume it will handle capture).
dev().assert(this.iframe);
installAnchorClickInterceptor(
this.getAmpDoc(), this.iframe.contentWindow);
}
if (this.ampAnalyticsConfig_) {
dev().assert(!this.ampAnalyticsElement_);
if (isReportingEnabled(this)) {
addCsiSignalsToAmpAnalyticsConfig(
this.win,
this.element,
this.ampAnalyticsConfig_,
this.qqid_,
!!creativeMetaData,
this.lifecycleReporter_.getDeltaTime(),
this.lifecycleReporter_.getInitTime());
}
this.ampAnalyticsElement_ =
insertAnalyticsElement(this.element, this.ampAnalyticsConfig_, true);
}
this.lifecycleReporter_.addPingsForVisibility(this.element);
setStyles(dev().assertElement(this.iframe), {
width: `${this.size_.width}px`,
height: `${this.size_.height}px`,
});
}
/** @override */
unlayoutCallback() {
switch (this.postAdResponseExperimentFeatures['unlayout_exp']) {
case 'all':
// Ensure all creatives are removed.
this.isAmpCreative_ = false;
break;
case 'remain':
if (this.qqid_ && this.isAmpCreative_ === null) {
// Ad response received but not yet rendered. Note that no fills
// would fall into this case even if layoutCallback has executed.
// Assume high probability of continued no fill therefore do not
// tear down.
dev().info(TAG, 'unlayoutCallback - unrendered creative can remain');
return false;
}
}
if (this.isAmpCreative_) {
// Allow AMP creatives to remain in case SERP viewer swipe back.
return false;
}
const superResult = super.unlayoutCallback();
this.element.setAttribute('data-amp-slot-index',
this.win.ampAdSlotIdCounter++);
this.lifecycleReporter_ = this.initLifecycleReporter();
if (this.uniqueSlotId_) {
sharedState.removeSlot(this.uniqueSlotId_);
}
if (this.ampAnalyticsElement_) {
removeElement(this.ampAnalyticsElement_);
this.ampAnalyticsElement_ = null;
}
this.ampAnalyticsConfig_ = null;
this.qqid_ = null;
this.isAmpCreative_ = null;
return superResult;
}
/** @override */
onLayoutMeasure() {
super.onLayoutMeasure();
if (this.isResponsive_() && !this.responsiveAligned_) {
this.responsiveAligned_ = true;
const layoutBox = this.getLayoutBox();
// Nudge into the correct horizontal position by changing side margin.
this.getVsync().run({
measure: state => {
state.direction =
computedStyle(this.win, this.element)['direction'];
},
mutate: state => {
if (state.direction == 'rtl') {
setStyle(this.element, 'marginRight', layoutBox.left, 'px');
} else {
setStyle(this.element, 'marginLeft', -layoutBox.left, 'px');
}
},
}, {direction: ''});
}
}
/** @override */
getPreconnectUrls() {
return ['https://googleads.g.doubleclick.net'];
}
/** @override */
getA4aAnalyticsVars(analyticsTrigger) {
return getCsiAmpAnalyticsVariables(analyticsTrigger, this, this.qqid_);
}
/** @override */
getA4aAnalyticsConfig() {
return getCsiAmpAnalyticsConfig();
}
/**
* Calculates the appropriate height for a full-width responsive ad of the
* given width.
* @param {!{width: number, height: number}} viewportSize
* @return {number}
* @private
*/
static getResponsiveHeightForContext_(viewportSize) {
const minHeight = 100;
const maxHeight = Math.min(300, viewportSize.height);
// We aim for a 6:5 aspect ratio.
const idealHeight = Math.round(viewportSize.width / 1.2);
return clamp(idealHeight, minHeight, maxHeight);
}
}
AMP.extension(TAG, '0.1', AMP => {
AMP.registerElement(TAG, AmpAdNetworkAdsenseImpl);
});