Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0 adapter support for mantis #1840

Merged
merged 3 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions modules/mantisBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import {registerBidder} from 'src/adapters/bidderFactory';

function inIframe() {
try {
return window.self !== window.top && !window.mantis_link;
} catch (e) {
return true;
}
}

function isDesktop(ignoreTouch) {
var supportsTouch = !ignoreTouch && ('ontouchstart' in window || navigator.msMaxTouchPoints);
if (inIframe()) {
return !supportsTouch;
}
var width = window.innerWidth || window.document.documentElement.clientWidth || window.document.body.clientWidth;
return !supportsTouch && (!width || width >= (window.mantis_breakpoint || 768));
}

function storeUuid(uuid) {
if (window.mantis_uuid) {
return false;
}
window.mantis_uuid = uuid;
if (window.localStorage) {
try {
window.localStorage.setItem('mantis:uuid', uuid);
} catch (ex) {
}
}
}

function isSendable(val) {
if (val === null || val === undefined) {
return false;
}
if (typeof val === 'string') {
return !(!val || /^\s*$/.test(val));
}
if (typeof val === 'number') {
return !isNaN(val);
}
return true;
}

function isObject(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}

function isAmp() {
return typeof window.context === 'object' && (window.context.tagName === 'AMP-AD' || window.context.tagName === 'AMP-EMBED');
}

function isSecure() {
return document.location.protocol === 'https:';
}

Copy link
Contributor

@idettman idettman Nov 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: There is a function for testing types included in utils.js beginning on line #332. Line #334 includes an "isArray" method where you can see how the "isA" method could be used for testing types not included such as "[Object]".

function isArray(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}

function jsonToQuery(data, chain, form) {
if (!data) {
return null;
}
var parts = form || [];
for (var key in data) {
var queryKey = key;
if (chain) {
queryKey = chain + '[' + key + ']';
}
var val = data[key];
if (isArray(val)) {
for (var index = 0; index < val.length; index++) {
var akey = queryKey + '[' + index + ']';
var aval = val[index];
if (isObject(aval)) {
jsonToQuery(aval, akey, parts);
} else if (isSendable(aval)) {
parts.push(akey + '=' + encodeURIComponent(aval));
}
}
} else if (isObject(val) && val != data) {
jsonToQuery(val, queryKey, parts);
} else if (isSendable(val)) {
parts.push(queryKey + '=' + encodeURIComponent(val));
}
}
return parts.join('&');
}

function buildMantisUrl(path, data, domain) {
var params = {
referrer: document.referrer,
tz: new Date().getTimezoneOffset(),
buster: new Date().getTime(),
secure: isSecure()
};
if (!inIframe() || isAmp()) {
params.mobile = !isAmp() && isDesktop(true) ? 'false' : 'true';
}
if (window.mantis_uuid) {
params.uuid = window.mantis_uuid;
} else if (window.localStorage) {
var localUuid = window.localStorage.getItem('mantis:uuid');
if (localUuid) {
params.uuid = localUuid;
}
}
if (!inIframe()) {
try {
params.title = window.top.document.title;
params.referrer = window.top.document.referrer;
params.url = window.top.document.location.href;
} catch (ex) {
}
} else {
params.iframe = true;
}
if (isAmp()) {
params.amp = true;
if (!params.url && window.context.canonicalUrl) {
params.url = window.context.canonicalUrl;
}
if (!params.url && window.context.location) {
params.url = window.context.location.href;
}
if (!params.referrer && window.context.referrer) {
params.referrer = window.context.referrer;
}
}
Object.keys(data || {}).forEach(function (key) {
params[key] = data[key];
});
var query = jsonToQuery(params);
return (window.mantis_domain === undefined ? domain || 'https://mantodea.mantisadnetwork.com' : window.mantis_domain) + path + '?' + query;
}

const spec = {
code: 'mantis',
supportedMediaTypes: ['banner', 'video', 'native'],
isBidRequestValid: function (bid) {
return !!(bid.params.property && (bid.params.code || bid.params.zoneId || bid.params.zone));
},
buildRequests: function (validBidRequests) {
var property = null;
validBidRequests.some(function (bid) {
if (bid.params.property) {
property = bid.params.property;
return true;
}
});
const query = {
bids: validBidRequests.map(function (bid) {
return {
bidId: bid.bidId,
config: bid.params,
sizes: bid.sizes.map(function (size) {
return {width: size[0], height: size[1]};
})
};
}),
property: property,
version: 2
};
return {
method: 'GET',
url: buildMantisUrl('/prebid/display', query) + '&foo',
data: ''
};
},
interpretResponse: function (serverResponse) {
storeUuid(serverResponse.uuid);
return serverResponse.body.ads.map(function (ad) {
return {
requestId: ad.bid,
cpm: ad.cpm,
width: ad.width,
height: ad.height,
ad: ad.html,
ttl: 86400,
creativeId: ad.view,
netRevenue: true,
currency: 'USD'
};
});
},
getUserSyncs: function (syncOptions) {
if (syncOptions.iframeEnabled) {
return [{
type: 'iframe',
url: buildMantisUrl('/prebid/iframe')
}];
}
if (syncOptions.pixelEnabled) {
return [{
type: 'image',
url: buildMantisUrl('/prebid/pixel')
}];
}
}
};

export {spec};

registerBidder(spec);
30 changes: 30 additions & 0 deletions modules/mantisBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Overview

Module Name: MANTIS Ad Network Bid Adapter

Module Type: Bidder Adapter

Maintainer: [email protected]

# Description

Module that connects to MANTIS's demand sources

# Test Parameters
```javascript
var adUnits = [
{
code: 'test',
sizes: [[300, 250]],
bids: [
{
bidder: 'mantis',
params: {
property: 'demo',
zone: 'zone'
}
}
]
}
];
```
Loading