Skip to content

Commit

Permalink
Trion Interactive 1.0 adapter update (#1712)
Browse files Browse the repository at this point in the history
* Adding files associated with the trion adapter update to the newest prebid version(1.0).

* Updating pull request with safer checks for user sync and general clean up/consistency of tests.

* removing a call to bidder code for pull request review.  also removing the test that requires it
  • Loading branch information
grohm33 authored and Matt Kendall committed Nov 27, 2017
1 parent 23db393 commit 440fa77
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 302 deletions.
245 changes: 134 additions & 111 deletions modules/trionBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,152 @@
var CONSTANTS = require('src/constants.json');
var utils = require('src/utils.js');
var adloader = require('src/adloader.js');
var bidmanager = require('src/bidmanager.js');
var bidfactory = require('src/bidfactory.js');
var Adapter = require('src/adapter.js').default;
var adaptermanager = require('src/adaptermanager');

const BID_REQUEST_BASE_URL = 'https://in-appadvertising.com/api/bidRequest?';
const USER_SYNC_URL = 'https://in-appadvertising.com/api/userSync.js';

function TrionAdapter() {
var baseAdapter = new Adapter('trion');
var userTag = null;

baseAdapter.callBids = function (params) {
var bids = params.bids || [];

if (!bids.length) {
return;
import * as utils from 'src/utils';
import {registerBidder} from 'src/adapters/bidderFactory';

const BID_REQUEST_BASE_URL = 'https://in-appadvertising.com/api/bidRequest';
const USER_SYNC_URL = 'https://in-appadvertising.com/api/userSync.html';
const BIDDER_CODE = 'trion';
const BASE_KEY = '_trion_';

export const spec = {
code: BIDDER_CODE,
isBidRequestValid: function (bid) {
return !!(bid && bid.params && bid.params.pubId && bid.params.sectionId);
},
buildRequests: function (validBidRequests) {
var bidRequests = [];

for (var i = 0; i < validBidRequests.length; i++) {
var bid = validBidRequests[i];

var trionUrlParams = buildTrionUrlParams(bid);

bidRequests.push({
method: 'GET',
url: BID_REQUEST_BASE_URL,
bidRequest: bid,
data: trionUrlParams
});
}
return bidRequests;
},

if (!window.TRION_INT) {
adloader.loadScript(USER_SYNC_URL, function () {
userTag = window.TRION_INT || {};
userTag.pubId = utils.getBidIdParameter('pubId', bids[0].params);
userTag.sectionId = utils.getBidIdParameter('sectionId', bids[0].params);
if (!userTag.to) {
getBids(bids);
} else {
setTimeout(function () {
getBids(bids);
}, userTag.to);
}
}, true);
} else {
userTag = window.TRION_INT;
getBids(bids);
}
};
interpretResponse: function (trionResponseObj, request) {
var bid = {};
var bidResponses = [];
var bidRequest = request.bidRequest;

if (trionResponseObj && trionResponseObj.bidId && bidRequest) {
var result = trionResponseObj.result;

if (result && result.cpm && result.placeBid && result.ad) {
var cpm = parseInt(result.cpm, 10) / 100;

function getBids(bids) {
if (!userTag.int_t) {
userTag.int_t = window.TR_INT_T || -1;
bid.requestId = bidRequest.bidId;
bid.cpm = cpm;
bid.ad = result.ad;
bid.width = result.width;
bid.height = result.height;
bidResponses.push(bid);
}
}

for (var i = 0; i < bids.length; i++) {
var bidRequest = bids[i];
var bidId = bidRequest.bidId;
adloader.loadScript(buildTrionUrl(bidRequest, bidId));
return bidResponses;
},
getUserSyncs: function getUserSyncs(syncOptions) {
if (syncOptions.iframeEnabled) {
handlePostMessage();
return [{
type: 'iframe',
url: getSyncUrl()
}];
}
}

function buildTrionUrl(bid, bidId) {
var pubId = utils.getBidIdParameter('pubId', bid.params);
var sectionId = utils.getBidIdParameter('sectionId', bid.params);
var re = utils.getBidIdParameter('re', bid.params);
var url = utils.getTopWindowUrl();
var sizes = utils.parseSizesInput(bid.sizes).join(',');

var trionUrl = BID_REQUEST_BASE_URL;

trionUrl = utils.tryAppendQueryString(trionUrl, 'callback', '$$PREBID_GLOBAL$$.handleTrionCB');
trionUrl = utils.tryAppendQueryString(trionUrl, 'bidId', bidId);
trionUrl = utils.tryAppendQueryString(trionUrl, 'pubId', pubId);
trionUrl = utils.tryAppendQueryString(trionUrl, 'sectionId', sectionId);
trionUrl = utils.tryAppendQueryString(trionUrl, 're', re);
trionUrl = utils.tryAppendQueryString(trionUrl, 'slot', bid.placementCode);
if (url) {
trionUrl += 'url=' + url + '&';
}
if (sizes) {
trionUrl += 'sizes=' + sizes + '&';
}
if (userTag) {
trionUrl += 'tag=' + encodeURIComponent(JSON.stringify(userTag)) + '&';
}
};
registerBidder(spec);

// remove the trailing "&"
if (trionUrl.lastIndexOf('&') === trionUrl.length - 1) {
trionUrl = trionUrl.substring(0, trionUrl.length - 1);
}
function getSyncUrl() {
var unParsedPubAndSection = getStorageData(BASE_KEY + 'lps') || ':';
var pubSectionArray = unParsedPubAndSection.split(':') || [];
var pubId = pubSectionArray[0] || -1;
var sectionId = pubSectionArray[1] || -1;
var url = utils.getTopWindowUrl();
return USER_SYNC_URL + `?p=${pubId}&s=${sectionId}&u=${url}`;
}

return trionUrl;
function buildTrionUrlParams(bid) {
var pubId = utils.getBidIdParameter('pubId', bid.params);
var sectionId = utils.getBidIdParameter('sectionId', bid.params);
var re = utils.getBidIdParameter('re', bid.params);
var url = utils.getTopWindowUrl();
var sizes = utils.parseSizesInput(bid.sizes).join(',');

var int_t = window.TR_INT_T && window.TR_INT_T != -1 ? window.TR_INT_T : null;
if (!int_t) {
int_t = getStorageData(BASE_KEY + 'int_t');
}
if (int_t) {
setStorageData(BASE_KEY + 'int_t', int_t)
}
setStorageData(BASE_KEY + 'lps', pubId + ':' + sectionId);
var trionUrl = '';

trionUrl = utils.tryAppendQueryString(trionUrl, 'bidId', bid.bidId);
trionUrl = utils.tryAppendQueryString(trionUrl, 'pubId', pubId);
trionUrl = utils.tryAppendQueryString(trionUrl, 'sectionId', sectionId);
trionUrl = utils.tryAppendQueryString(trionUrl, 're', re);
if (url) {
trionUrl += 'url=' + url + '&';
}
if (sizes) {
trionUrl += 'sizes=' + sizes + '&';
}
if (int_t) {
trionUrl = utils.tryAppendQueryString(trionUrl, 'int_t', encodeURIComponent(int_t));
}

// expose the callback to the global object:
$$PREBID_GLOBAL$$.handleTrionCB = function (trionResponseObj) {
var bid;
var bidObj = {};
var placementCode = '';

if (trionResponseObj && trionResponseObj.bidId) {
var bidCode;
var bidId = trionResponseObj.bidId;
var result = trionResponseObj && trionResponseObj.result;

bidObj = utils.getBidRequest(bidId);
if (bidObj) {
bidCode = bidObj.bidder;
placementCode = bidObj.placementCode;
}
// remove the trailing "&"
if (trionUrl.lastIndexOf('&') === trionUrl.length - 1) {
trionUrl = trionUrl.substring(0, trionUrl.length - 1);
}
return trionUrl;
}

if (result && result.cpm && result.placeBid && result.ad) {
var cpm = parseInt(result.cpm, 10) / 100;
bid = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bidObj);
bid.bidderCode = bidCode;
bid.cpm = cpm;
bid.ad = result.ad;
bid.width = result.width;
bid.height = result.height;
}
function handlePostMessage() {
try {
if (window.addEventListener) {
window.addEventListener('message', acceptPostMessage);
}
if (!bid) {
bid = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bidObj);
} catch (e) {
}
}

export function getStorageData(key) {
var item = null;
try {
if (window.localStorage) {
item = window.localStorage.getItem(key);
}
bidmanager.addBidResponse(placementCode, bid);
};

return Object.assign(this, {
callBids: baseAdapter.callBids,
setBidderCode: baseAdapter.setBidderCode,
buildTrionUrl: buildTrionUrl
});
} catch (e) {
}
return item;
}

adaptermanager.registerBidAdapter(new TrionAdapter(), 'trion');
export function setStorageData(key, item) {
try {
if (window.localStorage) {
window.localStorage.setItem(key, item);
}
} catch (e) {
}
}

module.exports = TrionAdapter;
export function acceptPostMessage(e) {
var message = e.data || '';
if (message.indexOf(BASE_KEY + 'userId') !== 0) {
return;
}
var int_t = message.split(BASE_KEY + 'userId=')[1];
if (int_t) {
setStorageData(BASE_KEY + 'int_t', int_t);
}
}
33 changes: 33 additions & 0 deletions modules/trionBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Overview

**Module Name**: Trion Interactive Bidder Adapter
**Module Type**: Bidder Adapter
**Maintainer**: mgroh@trioninteractive.com
**Publisher Contact**: publishers@trioninteractive.com

# Description

This module connects to Trion's demand sources. It supports display, outstream, and rich media formats.
Trion will provide ``pubId`` and ``sectionId`` that are specific to your ad type.
Please reach out to ``publishers@trioninteractive.com`` to set up a trion account and above ids.
Use bidder code ```trion``` for all Trion traffic.

# Test Parameters
```
var adUnits = [
{
code: 'ad-div',
sizes: [[300, 250]], // a display size
bids: [
{
bidder: 'trion',
params: {
pubId: '12345',
sectionId: '1',
re : 'http://clicktrackingurl.com?re='// optional
}
}
]
}
];
```
Loading

0 comments on commit 440fa77

Please sign in to comment.