forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubgears.js
177 lines (126 loc) · 4.63 KB
/
pubgears.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
var bidfactory = require('../bidfactory.js')
var bidmanager = require('../bidmanager.js')
var consts = require('../constants.json')
var d = document
var SCRIPT = 'script'
var PARAMS = 'params'
var SIZES = 'sizes'
var SIZE = 'size'
var CPM = 'cpm'
var AD = 'ad'
var WIDTH = 'width'
var HEIGHT = 'height'
var PUB_ZONE = 'pub_zone'
var GROSS_PRICE = 'gross_price'
var RESOURCE = 'resource'
var DETAIL = 'detail'
var DATA = 'data'
var BIDDER_CODE_RESPONSE_KEY = 'bidderCode'
var BIDDER_CODE = 'pubgears'
var SCRIPT_ID = 'pg-header-tag'
var ATTRIBUTE_PREFIX = 'data-bsm-'
var SLOT_LIST_ATTRIBUTE = 'slot-list'
var PUBLISHER_ATTRIBUTE = 'pub'
var FLAG_ATTRIBUTE = 'flag'
var AD_UNIT_CODE = 'adUnitCode'
var PUBLISHER_PARAM = 'publisherName'
var PUB_ZONE_PARAM = 'pubZone'
var BID_RECEIVED_EVENT_NAME = 'onBidResponse'
var SLOT_READY_EVENT_NAME = 'onResourceComplete'
var CREATIVE_TEMPLATE = decodeURIComponent("%3Cscript%3E%0A(function(define)%7B%0Adefine(function(a)%7B%0A%09var%20id%3D%20%22pg-ad-%22%20%2B%20Math.floor(Math.random()%20*%201e10)%2C%20d%3D%20document%0A%09d.write(\'%3Cdiv%20id%3D%22\'%2Bid%2B\'%22%3E%3C%2Fdiv%3E\')%0A%09a.push(%7B%0A%09%09pub%3A%20\'%25%25PUBLISHER_NAME%25%25\'%2C%0A%09%09pub_zone%3A%20\'%25%25PUB_ZONE%25%25\'%2C%0A%09%09sizes%3A%20%5B\'%25%25SIZE%25%25\'%5D%2C%0A%09%09flag%3A%20true%2C%0A%09%09container%3A%20d.getElementById(id)%2C%0A%09%7D)%3B%0A%7D)%7D)(function(f)%7Bvar%20key%3D\'uber_imps\'%2Ca%3Dthis%5Bkey%5D%3Dthis%5Bkey%5D%7C%7C%5B%5D%3Bf(a)%3B%7D)%3B%0A%3C%2Fscript%3E%0A%3Cscript%20src%3D%22%2F%2Fc.pubgears.com%2Ftags%2Fb%22%3E%3C%2Fscript%3E%0A")
var TAG_URL = '//c.pubgears.com/tags/h'
var publisher = ''
module.exports = PubGearsAdapter
function PubGearsAdapter() {
var proxy = null
var pendingSlots = {}
var initialized = false
this['callBids'] = callBids
function callBids(params) {
var bids = params[consts.JSON_MAPPING.PL_BIDS]
var slots = bids.map(getSlotFromBidParam)
publisher = bids[0][PARAMS][PUBLISHER_PARAM]
bids.forEach(function(bid) {
var name = getSlotFromBidParam(bid)
pendingSlots[ name ] = bid[AD_UNIT_CODE]
})
proxy = proxy || getScript(SCRIPT_ID) || makeScript(slots, publisher, SCRIPT_ID, TAG_URL)
if(!initialized)
registerEventListeners(proxy)
initialized = true
}
function loadScript(script) {
var anchor = (function(scripts) {
return scripts[ scripts.length - 1 ]
})(d.getElementsByTagName(SCRIPT));
return anchor.parentNode.insertBefore(script, anchor)
}
function getSlotFromBidParam(bid) {
var size = bid[SIZES][0].join('x')
var params = bid[PARAMS]
var slotName = params[PUB_ZONE_PARAM]
return [ slotName, size ].join('@')
}
function getSlotFromResource(resource) {
var size = resource[SIZE]
var key = [ resource[PUB_ZONE], size ].join('@')
}
function makeScript(slots, publisher, id, url) {
var script = d.createElement(SCRIPT)
script.src = url
script.id = id
script.setAttribute(ATTRIBUTE_PREFIX + SLOT_LIST_ATTRIBUTE, slots.join(' '))
script.setAttribute(ATTRIBUTE_PREFIX + FLAG_ATTRIBUTE, 'true')
script.setAttribute(ATTRIBUTE_PREFIX + PUBLISHER_ATTRIBUTE, publisher)
return loadScript(script)
}
function getScript(id) {
return d.getElementById(id)
}
function registerEventListeners(script) {
script.addEventListener(BID_RECEIVED_EVENT_NAME, onBid, true)
script.addEventListener(SLOT_READY_EVENT_NAME, onComplete, true)
}
function onBid(event) {
var data = event[DETAIL][DATA]
var slotKey = getSlotFromResource(data[RESOURCE])
var adUnitCode = pendingSlots[slotKey]
if(adUnitCode)
bidmanager.addBidResponse(adUnitCode, buildResponse(data))
}
function buildResponse(eventData) {
var resource = eventData[RESOURCE]
var dims = resource[SIZE].split('x')
var price = Number(eventData[GROSS_PRICE])
var status = isNaN(price) || price < 0 ? 2 : 1
var response = bidfactory.createBid(status)
response[BIDDER_CODE_RESPONSE_KEY] = BIDDER_CODE
if(status != 1)
return response
response[AD] = getCreative(resource)
response[CPM] = price / 1e3
response[WIDTH] = dims[0]
response[HEIGHT] = dims[1]
return response
}
function getCreative(resource) {
var bookends = '%%'
var creative = CREATIVE_TEMPLATE
var replacementValues = {
PUBLISHER_NAME: publisher,
PUB_ZONE: resource[PUB_ZONE],
SIZE: resource[SIZE]
}
Object.keys(replacementValues).forEach(function(token) {
var find = ['', token, ''].join(bookends)
var value = replacementValues[token]
creative = creative.replace(find, value)
})
return creative
}
function onComplete(event) {
var data = event[DETAIL][DATA]
var slotKey = getSlotFromResource(data[RESOURCE])
delete pendingSlots[slotKey]
}
}