forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadventive.js
154 lines (141 loc) · 4.49 KB
/
adventive.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
/**
* Copyright 2015 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 {addParamsToUrl} from '../src/url.js';
import {dict, hasOwn} from '../src/utils/object';
import {endsWith, startsWith} from '../src/string';
import {loadScript, validateData, writeScript} from '../3p/3p';
/**
* @param {!Window} global
* @param {!Object} data
*/
export function adventive(global, data) {
if (hasOwn(data, 'isDev')) {
adventive_(global, data);
} else {
validateData(data, ['src'], ['isDev']);
writeScript(global, `${data.src}&isAmp=1`);
}
}
const adv = {
addInstance: () => {},
args: {},
isLibLoaded: false,
mode: {
dev: false,
live: false,
localDev: false,
preview: false,
prod: false,
testing: false,
},
},
requiredData = ['pid'],
optionalData = ['click', 'async', 'isDev'],
sld = {true: 'adventivedev', false: 'adventive'},
thld = {true: 'amp', false: 'ads'},
cacheTime = 5;
/**
* Future data:
* - async
* - click
* - height
* - isDev
* - width
* - pid
*
* Considerations:
* - Recipe reuse for multi-placed Ads.
* - Reduce request to only what is needed
* - Mitigate risk of data corruption
*
* @todo implement multi-size handling for multi-slotted ads. @see doubleclick
*
* @param {!Window} global
* @param {!Object} data
*/
function adventive_(global, data) {
validateData(data, requiredData, optionalData);
if (!hasOwn(global, 'adventive')) { global.adventive = adv; }
const ns = global.adventive;
if (!hasOwn(ns, 'context')) { ns.context = global.context; }
if (!Object.isFrozen(ns.mode)) {
updateMode(ns.mode, global.context.location.hostname);
}
const cb = callback.bind(this, data.pid, ns),
url = getUrl(global.context, data, ns);
url ?
(hasOwn(data, 'async') ? loadScript : writeScript)(global, url, cb) : cb();
}
/**
* @param {!Object} mode
* @param {string} hostname
*/
function updateMode(mode, hostname) {
mode.localDev = hostname === 'localhost';
mode.dev = !mode.localDev && endsWith(hostname, `${sld[false]}.com`);
mode.prod = !mode.localDev && endsWith(hostname, `${sld[true]}.com`);
mode.preview = (mode.dev || mode.prod) && startsWith(hostname, '/ad');
mode.testing = (mode.dev || mode.prod) && startsWith(hostname, '/testing');
mode.live = (mode.testing || !mode.preview) && !mode.localDev;
Object.freeze(mode);
}
/**
* @param {string} id
* @param {!Object} ns
*/
function callback(id, ns) { ns.addInstance(id); }
/**
* @param {!Object} context
* @param {!Object} data
* @param {!Object} ns
* @return {string|boolean} if a search query is generated, a full url is
* provided, otherwise false
*/
function getUrl(context, data, ns) {
const {mode} = ns,
isDev = hasOwn(data, 'isDev'),
sld_ = sld[!mode.dev],
thld_ = thld[isDev && !mode.live],
search = reduceSearch(ns, data.pid, data.click, context.referrer),
url = search ?
addParamsToUrl(`https://${thld_}.${sld_}.com/ad`, search) : false;
return url;
}
/**
* @todo determine if we can reduce the request to nothing & return false
* @todo usage of RTC may be applicable here for macros
* @todo check if click-macros can be offloaded to amp-analytics (i.e recipe)
*
* @param {!Object} ns
* @param {string} placementId
* @param {string} click
* @param {string} referrer
* @return {JsonObject} if no more data is needed, false, otherwise JSON
* representation of the url search query.
*/
function reduceSearch(ns, placementId, click, referrer) {
const isRecipeLoaded = hasOwn(ns.args, 'placementId'),
isRecipeStale = !isRecipeLoaded ? true :
(Date.now() - ns.args[placementId].requestTime) > (60 * cacheTime),
needsRequest = !isRecipeLoaded || isRecipeStale;
return !needsRequest ? null : dict({
'click': click,
'referrer': referrer,
'isAmp': '1',
'lib': !ns.isLibLoaded ? '1' : '', // may be prefetchable via _config
'pid': needsRequest ? placementId : '',
});
}