1
1
import * as utils from '../src/utils.js' ;
2
2
import { registerBidder } from '../src/adapters/bidderFactory.js' ;
3
- import find from 'core-js-pure/features/array/find.js' ;
4
3
const BIDDER_CODE = 'h12media' ;
5
4
const DEFAULT_URL = 'https://bidder.h12-media.com/prebid/' ;
6
5
const DEFAULT_CURRENCY = 'USD' ;
@@ -16,21 +15,31 @@ export const spec = {
16
15
} ,
17
16
18
17
buildRequests : function ( validBidRequests , bidderRequest ) {
19
- const requestUrl = validBidRequests [ 0 ] . params . endpointdom || DEFAULT_URL ;
20
- const isiframe = ! ( ( window . self === window . top ) || window . frameElement ) ;
18
+ const isiframe = utils . inIframe ( ) ;
21
19
const screenSize = getClientDimensions ( ) ;
22
20
const docSize = getDocumentDimensions ( ) ;
23
21
24
- const bidrequests = validBidRequests . map ( ( bidRequest ) => {
22
+ return validBidRequests . map ( ( bidRequest ) => {
25
23
const bidderParams = bidRequest . params ;
26
- const adUnitElement = document . getElementById ( bidRequest . adUnitCode ) ;
24
+ const requestUrl = bidderParams . endpointdom || DEFAULT_URL ;
25
+ let pubsubid = bidderParams . pubsubid || '' ;
26
+ if ( pubsubid && pubsubid . length > 32 ) {
27
+ utils . logError ( 'Bidder param \'pubsubid\' should be not more than 32 chars.' ) ;
28
+ pubsubid = '' ;
29
+ }
30
+ const pubcontainerid = bidderParams . pubcontainerid ;
31
+ const adUnitElement = document . getElementById ( pubcontainerid || bidRequest . adUnitCode ) ;
27
32
const ishidden = ! isVisible ( adUnitElement ) ;
28
- const coords = {
33
+ const framePos = getFramePos ( ) ;
34
+ const coords = isiframe ? {
35
+ x : framePos [ 0 ] ,
36
+ y : framePos [ 1 ] ,
37
+ } : {
29
38
x : adUnitElement && adUnitElement . getBoundingClientRect ( ) . x ,
30
39
y : adUnitElement && adUnitElement . getBoundingClientRect ( ) . y ,
31
40
} ;
32
41
33
- return {
42
+ const bidrequest = {
34
43
bidId : bidRequest . bidId ,
35
44
transactionId : bidRequest . transactionId ,
36
45
adunitId : bidRequest . adUnitCode ,
@@ -40,63 +49,75 @@ export const spec = {
40
49
adunitSize : bidRequest . mediaTypes . banner . sizes || [ ] ,
41
50
coords,
42
51
ishidden,
52
+ pubsubid,
53
+ pubcontainerid,
43
54
} ;
44
- } ) ;
45
55
46
- return {
47
- method : 'POST' ,
48
- url : requestUrl ,
49
- options : { withCredentials : false } ,
50
- data : {
51
- gdpr : utils . deepAccess ( bidderRequest , 'gdprConsent.gdprApplies' ) ? Boolean ( bidderRequest . gdprConsent . gdprApplies & 1 ) : false ,
52
- gdpr_cs : utils . deepAccess ( bidderRequest , 'gdprConsent.gdprApplies' ) ? bidderRequest . gdprConsent . consentString : '' ,
53
- topLevelUrl : window . top . location . href ,
54
- refererUrl : bidderRequest . refererInfo ? bidderRequest . refererInfo . referer : '' ,
55
- isiframe,
56
- version : '$prebid.version$' ,
57
- visitorInfo : {
58
- localTime : getLocalDateFormatted ( ) ,
59
- dayOfWeek : new Date ( ) . getDay ( ) ,
60
- screenWidth : screenSize [ 0 ] ,
61
- screenHeight : screenSize [ 1 ] ,
62
- docWidth : docSize [ 0 ] ,
63
- docHeight : docSize [ 1 ] ,
64
- scrollbarx : window . scrollX ,
65
- scrollbary : window . scrollY ,
56
+ let windowTop ;
57
+ try {
58
+ windowTop = window . top ;
59
+ } catch ( e ) {
60
+ utils . logMessage ( e ) ;
61
+ windowTop = window ;
62
+ }
63
+
64
+ return {
65
+ method : 'POST' ,
66
+ url : requestUrl ,
67
+ options : { withCredentials : true } ,
68
+ data : {
69
+ gdpr : ! ! utils . deepAccess ( bidderRequest , 'gdprConsent.gdprApplies' , false ) ,
70
+ gdpr_cs : utils . deepAccess ( bidderRequest , 'gdprConsent.consentString' , '' ) ,
71
+ usp : ! ! utils . deepAccess ( bidderRequest , 'uspConsent' , false ) ,
72
+ usp_cs : utils . deepAccess ( bidderRequest , 'uspConsent' , '' ) ,
73
+ topLevelUrl : utils . deepAccess ( bidderRequest , 'refererInfo.referer' , '' ) ,
74
+ refererUrl : windowTop . document . referrer ,
75
+ isiframe,
76
+ version : '$prebid.version$' ,
77
+ ExtUserIDs : bidRequest . userId ,
78
+ visitorInfo : {
79
+ localTime : getLocalDateFormatted ( ) ,
80
+ dayOfWeek : new Date ( ) . getDay ( ) ,
81
+ screenWidth : screenSize [ 0 ] ,
82
+ screenHeight : screenSize [ 1 ] ,
83
+ docWidth : docSize [ 0 ] ,
84
+ docHeight : docSize [ 1 ] ,
85
+ scrollbarx : windowTop . scrollX ,
86
+ scrollbary : windowTop . scrollY ,
87
+ } ,
88
+ bidrequest,
66
89
} ,
67
- bidrequests,
68
- } ,
69
- } ;
90
+ } ;
91
+ } ) ;
70
92
} ,
71
93
72
94
interpretResponse : function ( serverResponse , bidRequests ) {
73
95
let bidResponses = [ ] ;
74
96
try {
75
97
const serverBody = serverResponse . body ;
76
98
if ( serverBody ) {
77
- if ( serverBody . bids ) {
78
- serverBody . bids . forEach ( bidBody => {
79
- const bidRequest = find ( bidRequests . data . bidrequests , bid => bid . bidId === bidBody . bidId ) ;
80
- const bidResponse = {
81
- currency : serverBody . currency || DEFAULT_CURRENCY ,
82
- netRevenue : serverBody . netRevenue || DEFAULT_NET_REVENUE ,
83
- ttl : serverBody . ttl || DEFAULT_TTL ,
84
- requestId : bidBody . bidId ,
85
- cpm : bidBody . cpm ,
86
- width : bidBody . width ,
87
- height : bidBody . height ,
88
- creativeId : bidBody . creativeId ,
89
- ad : bidBody . ad ,
90
- meta : bidBody . meta ,
91
- mediaType : 'banner' ,
92
- } ;
93
- if ( bidRequest ) {
94
- bidResponse . pubid = bidRequest . pubid ;
95
- bidResponse . placementid = bidRequest . placementid ;
96
- bidResponse . size = bidRequest . size ;
97
- }
98
- bidResponses . push ( bidResponse ) ;
99
- } ) ;
99
+ if ( serverBody . bid ) {
100
+ const bidBody = serverBody . bid ;
101
+ const bidRequest = bidRequests . data . bidrequest ;
102
+ const bidResponse = {
103
+ currency : serverBody . currency || DEFAULT_CURRENCY ,
104
+ netRevenue : serverBody . netRevenue || DEFAULT_NET_REVENUE ,
105
+ ttl : serverBody . ttl || DEFAULT_TTL ,
106
+ requestId : bidBody . bidId ,
107
+ cpm : bidBody . cpm ,
108
+ width : bidBody . width ,
109
+ height : bidBody . height ,
110
+ creativeId : bidBody . creativeId ,
111
+ ad : bidBody . ad ,
112
+ meta : bidBody . meta ,
113
+ mediaType : 'banner' ,
114
+ } ;
115
+ if ( bidRequest ) {
116
+ bidResponse . pubid = bidRequest . pubid ;
117
+ bidResponse . placementid = bidRequest . placementid ;
118
+ bidResponse . size = bidRequest . size ;
119
+ }
120
+ bidResponses . push ( bidResponse ) ;
100
121
}
101
122
}
102
123
return bidResponses ;
@@ -105,47 +126,50 @@ export const spec = {
105
126
}
106
127
} ,
107
128
108
- getUserSyncs : function ( syncOptions , serverResponses , gdprConsent ) {
109
- const serverBody = serverResponses [ 0 ] . body ;
129
+ getUserSyncs : function ( syncOptions , serverResponses , gdprConsent , usPrivacy ) {
110
130
const syncs = [ ] ;
131
+ const uspApplies = ! ! utils . deepAccess ( usPrivacy , 'uspConsent' , false ) ;
132
+ const uspString = utils . deepAccess ( usPrivacy , 'uspConsent' , '' ) ;
111
133
gdprConsent = gdprConsent || {
112
134
gdprApplies : false , consentString : '' ,
113
135
} ;
114
136
115
- if ( serverBody ) {
116
- if ( serverBody . bids ) {
117
- serverBody . bids . forEach ( bidBody => {
118
- const userSyncUrls = bidBody . usersync || [ ] ;
119
- const userSyncUrlProcess = url => {
120
- return url
121
- . replace ( '{gdpr}' , gdprConsent . gdprApplies )
122
- . replace ( '{gdpr_cs}' , gdprConsent . consentString ) ;
123
- }
137
+ const userSyncUrlProcess = url => {
138
+ return url
139
+ . replace ( '{gdpr}' , gdprConsent . gdprApplies )
140
+ . replace ( '{gdpr_cs}' , gdprConsent . consentString )
141
+ . replace ( '{usp}' , uspApplies )
142
+ . replace ( '{usp_cs}' , uspString ) ;
143
+ }
124
144
125
- userSyncUrls . forEach ( sync => {
126
- if ( syncOptions . iframeEnabled && sync . type === 'iframe' && sync . url ) {
127
- syncs . push ( {
128
- type : 'iframe' ,
129
- url : userSyncUrlProcess ( sync . url ) ,
130
- } ) ;
131
- }
132
- if ( syncOptions . pixelEnabled && sync . type === 'image' && sync . url ) {
133
- syncs . push ( {
134
- type : 'image' ,
135
- url : userSyncUrlProcess ( sync . url ) ,
136
- } ) ;
137
- }
145
+ serverResponses . forEach ( serverResponse => {
146
+ const userSyncUrls = serverResponse . body . usersync || [ ] ;
147
+ userSyncUrls . forEach ( sync => {
148
+ if ( syncOptions . iframeEnabled && sync . type === 'iframe' && sync . url ) {
149
+ syncs . push ( {
150
+ type : 'iframe' ,
151
+ url : userSyncUrlProcess ( sync . url ) ,
138
152
} ) ;
139
- } ) ;
140
- }
141
- }
153
+ }
154
+ if ( syncOptions . pixelEnabled && sync . type === 'image' && sync . url ) {
155
+ syncs . push ( {
156
+ type : 'image' ,
157
+ url : userSyncUrlProcess ( sync . url ) ,
158
+ } ) ;
159
+ }
160
+ } )
161
+ } ) ;
142
162
143
163
return syncs ;
144
164
} ,
145
165
}
146
166
147
167
function getContext ( elem ) {
148
- return elem && window . document . body . contains ( elem ) ? window : ( window . top . document . body . contains ( elem ) ? top : undefined ) ;
168
+ try {
169
+ return elem && window . document . body . contains ( elem ) ? window : ( window . top . document . body . contains ( elem ) ? top : undefined ) ;
170
+ } catch ( e ) {
171
+ return undefined ;
172
+ }
149
173
}
150
174
151
175
function isDefined ( val ) {
@@ -206,4 +230,24 @@ function getLocalDateFormatted() {
206
230
return `${ d . getFullYear ( ) } -${ two ( d . getMonth ( ) + 1 ) } -${ two ( d . getDate ( ) ) } ${ two ( d . getHours ( ) ) } :${ two ( d . getMinutes ( ) ) } :${ two ( d . getSeconds ( ) ) } ` ;
207
231
}
208
232
233
+ function getFramePos ( ) {
234
+ let t = window ;
235
+ let m = 0 ;
236
+ let frmLeft = 0 ;
237
+ let frmTop = 0 ;
238
+ do {
239
+ m = m + 1 ;
240
+ try {
241
+ if ( m > 1 ) {
242
+ t = t . parent
243
+ }
244
+ frmLeft = frmLeft + t . frameElement . getBoundingClientRect ( ) . left ;
245
+ frmTop = frmTop + t . frameElement . getBoundingClientRect ( ) . top ;
246
+ } catch ( o ) { /* keep looping */
247
+ }
248
+ } while ( ( m < 100 ) && ( t . parent !== t . self ) )
249
+
250
+ return [ frmLeft , frmTop ] ;
251
+ }
252
+
209
253
registerBidder ( spec ) ;
0 commit comments