@@ -6,15 +6,19 @@ import * as utils from '../src/utils.js';
6
6
7
7
const emptyUrl = '' ;
8
8
const analyticsType = 'endpoint' ;
9
- const pubxaiAnalyticsVersion = 'v1.0 .0' ;
9
+ const pubxaiAnalyticsVersion = 'v1.1 .0' ;
10
10
const defaultHost = 'api.pbxai.com' ;
11
11
const auctionPath = '/analytics/auction' ;
12
12
const winningBidPath = '/analytics/bidwon' ;
13
13
14
14
let initOptions ;
15
15
let auctionTimestamp ;
16
+ let auctionCache = [ ] ;
16
17
let events = {
17
- bids : [ ]
18
+ bids : [ ] ,
19
+ floorDetail : { } ,
20
+ pageDetail : { } ,
21
+ deviceDetail : { }
18
22
} ;
19
23
20
24
var pubxaiAnalyticsAdapter = Object . assign ( adapter (
@@ -27,14 +31,14 @@ var pubxaiAnalyticsAdapter = Object.assign(adapter(
27
31
if ( eventType === CONSTANTS . EVENTS . BID_TIMEOUT ) {
28
32
args . forEach ( item => { mapBidResponse ( item , 'timeout' ) ; } ) ;
29
33
} else if ( eventType === CONSTANTS . EVENTS . AUCTION_INIT ) {
34
+ events . auctionInit = args ;
30
35
events . floorDetail = { } ;
31
- if ( typeof args . bidderRequests [ 0 ] . bids [ 0 ] !== 'undefined' && typeof args . bidderRequests [ 0 ] . bids [ 0 ] . floorData !== 'undefined' ) {
32
- Object . assign ( events . floorDetail , args . bidderRequests [ 0 ] . bids [ 0 ] . floorData ) ;
36
+ events . bids = [ ] ;
37
+ const floorData = utils . deepAccess ( args , 'bidderRequests.0.bids.0.floorData' ) ;
38
+ if ( typeof floorData !== 'undefined' ) {
39
+ Object . assign ( events . floorDetail , floorData ) ;
33
40
}
34
41
auctionTimestamp = args . timestamp ;
35
- } else if ( eventType === CONSTANTS . EVENTS . BID_REQUESTED ) {
36
- events . bids = [ ] ;
37
- mapBidRequests ( args ) . forEach ( item => { events . bids . push ( item ) } ) ;
38
42
} else if ( eventType === CONSTANTS . EVENTS . BID_RESPONSE ) {
39
43
mapBidResponse ( args , 'response' ) ;
40
44
} else if ( eventType === CONSTANTS . EVENTS . BID_WON ) {
@@ -43,81 +47,48 @@ var pubxaiAnalyticsAdapter = Object.assign(adapter(
43
47
} , 'bidwon' ) ;
44
48
}
45
49
}
46
-
47
50
if ( eventType === CONSTANTS . EVENTS . AUCTION_END ) {
48
51
send ( events , 'auctionEnd' ) ;
49
52
}
50
53
}
51
54
} ) ;
52
55
53
- function mapBidRequests ( params ) {
54
- let arr = [ ] ;
55
- if ( typeof params . bids !== 'undefined' && params . bids . length ) {
56
- params . bids . forEach ( function ( bid ) {
57
- arr . push ( {
58
- bidderCode : bid . bidder ,
59
- bidId : bid . bidId ,
60
- adUnitCode : bid . adUnitCode ,
61
- requestId : bid . bidderRequestId ,
62
- auctionId : bid . auctionId ,
63
- placementId : bid . params . placementId ,
64
- floorData : bid . floorData ,
65
- transactionId : bid . transactionId ,
66
- sizes : utils . parseSizesInput ( bid . mediaTypes . banner . sizes ) . toString ( ) ,
67
- renderStatus : 1 ,
68
- requestTimestamp : params . auctionStart
69
- } ) ;
70
- } ) ;
71
- }
72
- return arr ;
73
- }
74
-
75
56
function mapBidResponse ( bidResponse , status ) {
76
- if ( status !== 'bidwon' ) {
77
- let bid = events . bids . filter ( o => o . bidId === bidResponse . bidId || o . bidId === bidResponse . requestId ) [ 0 ] ;
78
- Object . assign ( bid , {
79
- bidderCode : bidResponse . bidder ,
80
- bidId : status === 'timeout' ? bidResponse . bidId : bidResponse . requestId ,
57
+ if ( typeof bidResponse !== 'undefined' ) {
58
+ let bid = {
81
59
adUnitCode : bidResponse . adUnitCode ,
82
60
auctionId : bidResponse . auctionId ,
83
- creativeId : bidResponse . creativeId ,
84
- transactionId : bidResponse . transactionId ,
85
- currency : bidResponse . currency ,
86
- cpm : bidResponse . cpm ,
87
- netRevenue : bidResponse . netRevenue ,
88
- mediaType : bidResponse . mediaType ,
89
- statusMessage : bidResponse . statusMessage ,
90
- floorData : bidResponse . floorData ,
91
- status : bidResponse . status ,
92
- renderStatus : status === 'timeout' ? 3 : 2 ,
93
- timeToRespond : bidResponse . timeToRespond ,
94
- requestTimestamp : bidResponse . requestTimestamp ,
95
- responseTimestamp : bidResponse . responseTimestamp ,
96
- platform : navigator . platform ,
97
- deviceType : getDeviceType ( )
98
- } ) ;
99
- } else {
100
- return {
101
61
bidderCode : bidResponse . bidder ,
102
- bidId : bidResponse . requestId ,
103
- adUnitCode : bidResponse . adUnitCode ,
104
- auctionId : bidResponse . auctionId ,
62
+ cpm : bidResponse . cpm ,
105
63
creativeId : bidResponse . creativeId ,
106
- transactionId : bidResponse . transactionId ,
107
64
currency : bidResponse . currency ,
108
- cpm : bidResponse . cpm ,
109
- netRevenue : bidResponse . netRevenue ,
110
65
floorData : bidResponse . floorData ,
111
- renderedSize : bidResponse . size ,
112
66
mediaType : bidResponse . mediaType ,
113
- statusMessage : bidResponse . statusMessage ,
114
- status : bidResponse . status ,
115
- renderStatus : 4 ,
116
- timeToRespond : bidResponse . timeToRespond ,
67
+ netRevenue : bidResponse . netRevenue ,
117
68
requestTimestamp : bidResponse . requestTimestamp ,
118
69
responseTimestamp : bidResponse . responseTimestamp ,
119
- platform : navigator . platform ,
120
- deviceType : getDeviceType ( )
70
+ status : bidResponse . status ,
71
+ statusMessage : bidResponse . statusMessage ,
72
+ timeToRespond : bidResponse . timeToRespond ,
73
+ transactionId : bidResponse . transactionId
74
+ } ;
75
+ if ( status !== 'bidwon' ) {
76
+ Object . assign ( bid , {
77
+ bidId : status === 'timeout' ? bidResponse . bidId : bidResponse . requestId ,
78
+ renderStatus : status === 'timeout' ? 3 : 2 ,
79
+ sizes : utils . parseSizesInput ( bidResponse . size ) . toString ( ) ,
80
+ } ) ;
81
+ events . bids . push ( bid ) ;
82
+ } else {
83
+ Object . assign ( bid , {
84
+ bidId : bidResponse . requestId ,
85
+ floorProvider : events . floorDetail ? events . floorDetail . floorProvider : null ,
86
+ isWinningBid : true ,
87
+ placementId : bidResponse . params ? utils . deepAccess ( bidResponse , 'params.0.placementId' ) : null ,
88
+ renderedSize : bidResponse . size ,
89
+ renderStatus : 4
90
+ } ) ;
91
+ return bid ;
121
92
}
122
93
}
123
94
}
@@ -132,6 +103,26 @@ export function getDeviceType() {
132
103
return 'desktop' ;
133
104
}
134
105
106
+ export function getBrowser ( ) {
107
+ if ( / C h r o m e / . test ( navigator . userAgent ) && / G o o g l e I n c / . test ( navigator . vendor ) ) return 'Chrome' ;
108
+ else if ( navigator . userAgent . match ( 'CriOS' ) ) return 'Chrome' ;
109
+ else if ( / F i r e f o x / . test ( navigator . userAgent ) ) return 'Firefox' ;
110
+ else if ( / E d g / . test ( navigator . userAgent ) ) return 'Microsoft Edge' ;
111
+ else if ( / S a f a r i / . test ( navigator . userAgent ) && / A p p l e C o m p u t e r / . test ( navigator . vendor ) ) return 'Safari' ;
112
+ else if ( / T r i d e n t / . test ( navigator . userAgent ) || / M S I E / . test ( navigator . userAgent ) ) return 'Internet Explorer' ;
113
+ else return 'Others' ;
114
+ }
115
+
116
+ export function getOS ( ) {
117
+ if ( navigator . userAgent . indexOf ( 'Android' ) != - 1 ) return 'Android' ;
118
+ if ( navigator . userAgent . indexOf ( 'like Mac' ) != - 1 ) return 'iOS' ;
119
+ if ( navigator . userAgent . indexOf ( 'Win' ) != - 1 ) return 'Windows' ;
120
+ if ( navigator . userAgent . indexOf ( 'Mac' ) != - 1 ) return 'Macintosh' ;
121
+ if ( navigator . userAgent . indexOf ( 'Linux' ) != - 1 ) return 'Linux' ;
122
+ if ( navigator . appVersion . indexOf ( 'X11' ) != - 1 ) return 'Unix' ;
123
+ return 'Others' ;
124
+ }
125
+
135
126
// add sampling rate
136
127
pubxaiAnalyticsAdapter . shouldFireEventRequest = function ( samplingRate = 1 ) {
137
128
return ( Math . floor ( ( Math . random ( ) * samplingRate + 1 ) ) === parseInt ( samplingRate ) ) ;
@@ -140,12 +131,24 @@ pubxaiAnalyticsAdapter.shouldFireEventRequest = function (samplingRate = 1) {
140
131
function send ( data , status ) {
141
132
if ( pubxaiAnalyticsAdapter . shouldFireEventRequest ( initOptions . samplingRate ) ) {
142
133
let location = utils . getWindowLocation ( ) ;
143
- if ( typeof data !== 'undefined' ) {
144
- data . pageDetail = { } ;
145
- Object . assign ( data . pageDetail , { host : location . host , path : location . pathname , search : location . search } ) ;
146
- }
147
134
data . initOptions = initOptions ;
148
-
135
+ if ( typeof data !== 'undefined' && typeof data . auctionInit !== 'undefined' ) {
136
+ Object . assign ( data . pageDetail , {
137
+ host : location . host ,
138
+ path : location . pathname ,
139
+ search : location . search ,
140
+ adUnitCount : data . auctionInit . adUnitCodes ? data . auctionInit . adUnitCodes . length : null
141
+ } ) ;
142
+ data . initOptions . auctionId = data . auctionInit . auctionId ;
143
+ delete data . auctionInit ;
144
+ }
145
+ data . deviceDetail = { } ;
146
+ Object . assign ( data . deviceDetail , {
147
+ platform : navigator . platform ,
148
+ deviceType : getDeviceType ( ) ,
149
+ deviceOS : getOS ( ) ,
150
+ browser : getBrowser ( )
151
+ } ) ;
149
152
let pubxaiAnalyticsRequestUrl = utils . buildUrl ( {
150
153
protocol : 'https' ,
151
154
hostname : ( initOptions && initOptions . hostName ) || defaultHost ,
@@ -156,8 +159,12 @@ function send(data, status) {
156
159
prebidVersion : $$PREBID_GLOBAL$$ . version
157
160
}
158
161
} ) ;
159
-
160
- ajax ( pubxaiAnalyticsRequestUrl , undefined , JSON . stringify ( data ) , { method : 'POST' , contentType : 'text/plain' } ) ;
162
+ if ( status == 'bidwon' ) {
163
+ ajax ( pubxaiAnalyticsRequestUrl , undefined , JSON . stringify ( data ) , { method : 'POST' , contentType : 'text/json' } ) ;
164
+ } else if ( status == 'auctionEnd' && auctionCache . indexOf ( data . initOptions . auctionId ) === - 1 ) {
165
+ ajax ( pubxaiAnalyticsRequestUrl , undefined , JSON . stringify ( data ) , { method : 'POST' , contentType : 'text/json' } ) ;
166
+ auctionCache . push ( data . initOptions . auctionId ) ;
167
+ }
161
168
}
162
169
}
163
170
0 commit comments