-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js'; | ||
import adapterManager from '../src/adapterManager.js'; | ||
import CONSTANTS from '../src/constants.json'; | ||
import { ajax } from '../src/ajax.js'; | ||
|
||
const { | ||
AUCTION_INIT, | ||
AUCTION_END, | ||
BID_WON, | ||
BID_TIMEOUT, | ||
BIDDER_ERROR, | ||
BID_REJECTED, | ||
BID_REQUESTED, | ||
AD_RENDER_FAILED, | ||
AD_RENDER_SUCCEEDED, | ||
AUCTION_TIMEOUT | ||
} = CONSTANTS.EVENTS; | ||
|
||
const URL = 'https://ps.itdsmr.com'; | ||
const ANALYTICS_TYPE = 'endpoint'; | ||
const BIDDER_CODE = 'smartyads'; | ||
const GVLID = 534; | ||
|
||
let smartyParams = {}; | ||
|
||
let smartyadsAdapter = Object.assign({}, | ||
adapter({ | ||
url: URL, | ||
analyticsType: ANALYTICS_TYPE, | ||
}), | ||
{ | ||
track({ eventType, args }) { | ||
switch (eventType) { | ||
case AUCTION_INIT: | ||
case AUCTION_TIMEOUT: | ||
case AUCTION_END: | ||
auctionHandler(eventType, args); | ||
break; | ||
case BID_REQUESTED: | ||
if (args.bidderCode === BIDDER_CODE) { | ||
for (const bid of args.bids) { | ||
const bidParams = bid.params?.length ? bid.params[0] : bid.params; | ||
smartyParams[bid.bidId] = bidParams; | ||
} | ||
}; | ||
break; | ||
case BID_WON: | ||
case BID_TIMEOUT: | ||
case BID_REJECTED: | ||
bidHandler(eventType, args); | ||
break; | ||
case BIDDER_ERROR: | ||
onBidderError(args); | ||
break; | ||
case AD_RENDER_FAILED: | ||
case AD_RENDER_SUCCEEDED: | ||
onAdRender(eventType, args); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
); | ||
|
||
const sendDataToServer = (data) => { | ||
ajax(URL, () => {}, JSON.stringify(data)); | ||
} | ||
|
||
const auctionHandler = (eventType, data) => { | ||
const auctionData = { | ||
auctionId: data.auctionId, | ||
status: eventType, | ||
timeout: data.timeout, | ||
metrics: data.metrics, | ||
bidderRequests: data.bidderRequests?.map(bidderRequest => { | ||
delete bidderRequest.gdprConsent; | ||
delete bidderRequest.refererInfo; | ||
return bidderRequest; | ||
}).filter(request => request.bidderCode === BIDDER_CODE), | ||
} | ||
|
||
sendDataToServer({ eventType, auctionData }); | ||
} | ||
|
||
const bidHandler = (eventType, bid) => { | ||
let bids = bid.length ? bid : [ bid ]; | ||
|
||
for (const bidObj of bids) { | ||
let bidToSend; | ||
|
||
if (bidObj.bidderCode != BIDDER_CODE) { | ||
if (eventType === BID_WON) { | ||
bidToSend = { | ||
cpm: bidObj.cpm, | ||
auctionId: bidObj.auctionId | ||
}; | ||
} else continue; | ||
} | ||
|
||
bidToSend = bidObj; | ||
|
||
if (eventType === BID_REJECTED) { | ||
bidToSend.params = smartyParams[bid.requestId]; | ||
} | ||
|
||
sendDataToServer({ eventType, bid: bidToSend }); | ||
} | ||
} | ||
|
||
const onBidderError = (data) => { | ||
sendDataToServer({ | ||
eventType: BIDDER_ERROR, | ||
error: data.error, | ||
bidderRequests: data?.bidderRequests?.length | ||
? data.bidderRequests.filter(request => request.bidderCode === BIDDER_CODE) | ||
: [ data.bidderRequest ] | ||
}); | ||
} | ||
|
||
const onAdRender = (eventType, data) => { | ||
if (data?.bid?.bidderCode === BIDDER_CODE) { | ||
sendDataToServer({ eventType, renderData: data }); | ||
} | ||
} | ||
|
||
adapterManager.registerAnalyticsAdapter({ | ||
adapter: smartyadsAdapter, | ||
code: BIDDER_CODE, | ||
gvlid: GVLID | ||
}) | ||
|
||
export default smartyadsAdapter; |