-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathmobileAndAmpRender.js
239 lines (216 loc) · 7.7 KB
/
mobileAndAmpRender.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
import { getCreativeCommentMarkup, triggerPixel, createTrackPixelHtml, loadScript, getCreativeComment, writeAdUrl, transformAuctionTargetingData, sendRequest, getUUID } from './utils';
import { isSafeFrame, isMobileApp } from './environment';
import { insertElement } from './domHelper';
import { writeAdHtml } from './postscribeRender';
const DEFAULT_CACHE_HOST = 'prebid.adnxs.com';
const DEFAULT_CACHE_PATH = '/pbc/v1/cache';
/**
* Render mobile or amp ad
* @param {string} cacheHost Cache host
* @param {string} cachePath Cache path
* @param {string} uuid id to render response from cache endpoint
* @param {string} size size of the creative
* @param {string} hbPb final price of the winning bid
* @param {Bool} isMobileApp flag to detect mobile app
*/
export function renderAmpOrMobileAd(dataObject) {
const targetingData = transformAuctionTargetingData(dataObject);
let { cacheHost, cachePath, uuid, size, hbPb } = targetingData;
uuid = uuid || '';
// For MoPub, creative is stored in localStorage via SDK.
let search = 'Prebid_';
if (uuid.substr(0, search.length) === search) {
loadFromLocalCache(uuid);
//register creative right away to not miss initial geom-update
updateIframe(size);
} else {
let adUrl = `${getCacheEndpoint(cacheHost, cachePath)}?uuid=${uuid}`;
//register creative right away to not miss initial geom-update
updateIframe(size);
sendRequest(adUrl, responseCallback(isMobileApp(targetingData.env), hbPb));
}
}
/**
* Load response from localStorage. In case of MoPub, sdk caches response
* @param {string} cacheId
*/
function loadFromLocalCache(cacheId) {
let bid = window.localStorage.getItem(cacheId);
let displayFn = responseCallback(true);
displayFn(bid);
}
/**
* update iframe by using size string to resize
* @param {string} size
*/
function updateIframe(size) {
if (size) {
const sizeArr = size.split('x').map(Number);
resizeIframe(sizeArr[0], sizeArr[1]);
} else {
console.log('Targeting key hb_size not found to resize creative');
}
}
/**
* Resize container iframe
* @param {Number} width width of creative
* @param {Number} height height of creative
*/
function resizeIframe(width, height) {
if (isSafeFrame(window)) {
const iframeWidth = window.innerWidth;
const iframeHeight = window.innerHeight;
function resize(status) {
let newWidth = width - iframeWidth;
let newHeight = height - iframeHeight;
window.$sf.ext.expand({ r: newWidth, b: newHeight, push: true });
}
if (iframeWidth !== width || iframeHeight !== height) {
window.$sf.ext.register(width, height, resize);
// we need to resize the DFP container as well
window.parent.postMessage({
sentinel: 'amp',
type: 'embed-size',
width: width,
height: height
}, '*');
}
}
}
/**
* Returns cache endpoint concatenated with cache path
* @param {string} cacheHost Cache Endpoint host
* @param {string} cachePath Cache Endpoint path
*/
function getCacheEndpoint(cacheHost, cachePath) {
let host = (typeof cacheHost === 'undefined' || cacheHost === "") ? DEFAULT_CACHE_HOST : cacheHost;
let path = (typeof cachePath === 'undefined' || cachePath === "") ? DEFAULT_CACHE_PATH : cachePath;
return `https://${host}${path}`;
}
/**
* Cache request Callback to display creative
* @param {Bool} isMobileApp
* @param {string} hbPb final price of the winning bid
* @returns {function} a callback function that parses response
*/
function responseCallback(isMobileApp, hbPb) {
return function (response) {
let bidObject = parseResponse(response);
let auctionPrice = bidObject.price || hbPb;
let ad = getCreativeCommentMarkup(bidObject);
let width = (bidObject.width) ? bidObject.width : bidObject.w;
let height = (bidObject.height) ? bidObject.height : bidObject.h;
// When Prebid Universal Creative reads from Prebid Cache, we need to have it check for the existence of the wurl parameter. If it exists, hit it.
if (bidObject.wurl) {
triggerPixel(decodeURIComponent(bidObject.wurl));
}
if (bidObject.adm) {
if (auctionPrice) { // replace ${AUCTION_PRICE} macro with the bidObject.price or hb_pb.
bidObject.adm = bidObject.adm.replace('${AUCTION_PRICE}', auctionPrice);
} else {
/*
From OpenRTB spec 2.5: If the source value is an optional parameter that was not specified, the macro will simply be removed (i.e., replaced with a zero-length string).
*/
bidObject.adm = bidObject.adm.replace('${AUCTION_PRICE}', '');
}
ad += (isMobileApp) ? constructMarkup(bidObject.adm, width, height) : bidObject.adm;
if (bidObject.nurl) {
ad += createTrackPixelHtml(decodeURIComponent(bidObject.nurl));
}
if (bidObject.burl) {
let triggerBurl = function () { triggerPixel(bidObject.burl); };
if (isMobileApp) {
let mraidScript = loadScript(window, 'mraid.js',
function () { // Success loading MRAID
let result = registerMRAIDViewableEvent(triggerBurl);
if (!result) {
triggerBurl(); // Error registering event
}
},
triggerBurl // Error loading MRAID
);
} else {
triggerBurl(); // Not a mobile app
}
}
writeAdHtml(ad);
} else if (bidObject.nurl) {
if (isMobileApp) {
let adhtml = loadScript(window, bidObject.nurl);
ad += constructMarkup(adhtml.outerHTML, width, height);
writeAdHtml(ad);
} else {
let nurl = bidObject.nurl;
let commentElm = getCreativeComment(bidObject);
insertElement(commentElm, document, 'body');
writeAdUrl(nurl, width, height);
}
}
}
};
/**
* Parse response
* @param {string} response
* @returns {Object} bidObject parsed response
*/
function parseResponse(response) {
let bidObject;
try {
bidObject = JSON.parse(response);
} catch (error) {
console.log(`Error parsing response from cache host: ${error}`);
}
return bidObject;
}
/**
* Wrap mobile app creative in div
* @param {string} ad html for creative
* @param {Number} width width of creative
* @param {Number} height height of creative
* @returns {string} creative markup
*/
function constructMarkup(ad, width, height) {
let id = getUUID();
return `<div id="${id}" style="border-style: none; position: absolute; width:100%; height:100%;">
<div id="${id}_inner" style="margin: 0 auto; width:${width}px; height:${height}px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">${ad}</div>
</div>`;
}
function registerMRAIDViewableEvent(callback) {
function exposureChangeListener(exposure) {
if (exposure > 0) {
mraid.removeEventListener('exposureChange', exposureChangeListener);
callback();
}
}
function viewableChangeListener(viewable) {
if (viewable) {
mraid.removeEventListener('viewableChange', viewableChangeListener);
callback();
}
}
function registerViewableChecks() {
if (window.MRAID_ENV && parseFloat(window.MRAID_ENV.version) >= 3) {
mraid.addEventListener('exposureChange', exposureChangeListener);
} else if (window.MRAID_ENV && parseFloat(window.MRAID_ENV.version) < 3) {
if (mraid.isViewable()) {
callback();
} else {
mraid.addEventListener('viewableChange', viewableChangeListener);
}
}
}
function readyListener() {
mraid.removeEventListener('ready', readyListener);
registerViewableChecks();
}
if (window.mraid && window.MRAID_ENV) {
if (mraid.getState() == 'loading') {
mraid.addEventListener('ready', readyListener);
} else {
registerViewableChecks();
}
return true;
} else {
return false;
}
}