forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad-network-config.js
125 lines (112 loc) · 3.39 KB
/
ad-network-config.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
/**
* 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.
*/
import {
AdSenseAmpAutoAdsHoldoutBranches,
getAdSenseAmpAutoAdsExpBranch,
} from '../../../ads/google/adsense-amp-auto-ads';
import {buildUrl} from '../../../ads/google/a4a/url-builder';
import {dict} from '../../../src/utils/object';
import {documentInfoForDoc} from '../../../src/services';
import {parseUrl} from '../../../src/url';
import {viewportForDoc} from '../../../src/services';
/**
* An interface intended to be implemented by any ad-networks wishing to support
* amp-auto-ads.
* @interface
*/
class AdNetworkConfigDef {
/**
* Indicates whether amp-auto-ads should be enabled on this pageview.
* @param {!Window} unusedWin
* @return {boolean} true if amp-auto-ads should be enabled on this pageview.
*/
isEnabled(unusedWin) {}
/**
* @return {string}
*/
getConfigUrl() {}
/**
* Any attributes derived from either the page or the auto-amp-ads tag that
* should be applied to any ads inserted.
* @return {!JsonObject<string, string>}
*/
getAttributes() {}
/**
* Network specific constraints on the placement of ads on the page.
* @return {!./ad-tracker.AdConstraints}
*/
getAdConstraints() {}
}
/**
* Builds and returns an AdNetworkConfig instance for the given type.
* @param {string} type
* @param {!Element} autoAmpAdsElement
* @return {?AdNetworkConfigDef}
*/
export function getAdNetworkConfig(type, autoAmpAdsElement) {
if (type == 'adsense') {
return new AdSenseNetworkConfig(autoAmpAdsElement);
}
return null;
}
/**
* @implements {AdNetworkConfigDef}
*/
class AdSenseNetworkConfig {
/**
* @param {!Element} autoAmpAdsElement
*/
constructor(autoAmpAdsElement) {
this.autoAmpAdsElement_ = autoAmpAdsElement;
}
/**
* @param {!Window} win
*/
isEnabled(win) {
const branch = getAdSenseAmpAutoAdsExpBranch(win);
return branch != AdSenseAmpAutoAdsHoldoutBranches.CONTROL;
}
/** @override */
getConfigUrl() {
const docInfo = documentInfoForDoc(this.autoAmpAdsElement_);
const canonicalHostname = parseUrl(docInfo.canonicalUrl).hostname;
return buildUrl('//pagead2.googlesyndication.com/getconfig/ama', {
'client': this.autoAmpAdsElement_.getAttribute('data-ad-client'),
'plah': canonicalHostname,
'ama_t': 'amp',
}, 4096);
}
/** @override */
getAttributes() {
return dict({
'type': 'adsense',
'data-ad-client': this.autoAmpAdsElement_.getAttribute('data-ad-client'),
});
}
/** @override */
getAdConstraints() {
const viewportHeight =
viewportForDoc(this.autoAmpAdsElement_).getSize().height;
return {
initialMinSpacing: viewportHeight,
subsequentMinSpacing: [
{adCount: 3, spacing: viewportHeight * 2},
{adCount: 6, spacing: viewportHeight * 3},
],
maxAdCount: 8,
};
}
}