Skip to content

Commit

Permalink
Merge pull request #6 from washingtonpost/integration
Browse files Browse the repository at this point in the history
Renaming target.position for old creatives
  • Loading branch information
maddoxnelson authored Jul 16, 2018
2 parents f56d128 + fee21f3 commit 4d47c2b
Show file tree
Hide file tree
Showing 9 changed files with 11,981 additions and 96 deletions.
1,953 changes: 1,952 additions & 1 deletion dist/arcads.js

Large diffs are not rendered by default.

9,920 changes: 9,920 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "rm -rf dist && webpack --env.production",
"dev": "rm -rf dist && webpack --env.development --watch",
"docs": "rm -rf docs && ./node_modules/.bin/esdoc && open docs/index.html",
"debug": "node debugging.js"
"debug": "node debugging.js",
"mock": "webpack-dev-server --progress --hot --inline && npm run test"
},
"repository": {
"type": "git",
Expand All @@ -24,7 +25,7 @@
"headerbidding",
"hb"
],
"author": "James Ives",
"author": "Arc",
"license": "MIT",
"bugs": {
"url": "https://github.com/washingtonpost/arcads/issues"
Expand All @@ -45,11 +46,13 @@
"eslint-plugin-import": "^2.8.0",
"jest": "^22.0.4",
"uglifyjs-webpack-plugin": "^1.1.6",
"webpack": "^3.10.0"
"webpack": "^3.10.0",
"webpack-cli": "^3.0.8"
},
"dependencies": {
"esdoc": "^1.0.4",
"esdoc-standard-plugin": "^1.0.0"
"esdoc-standard-plugin": "^1.0.0",
"promise-polyfill": "^8.0.0"
},
"homepage": "https://github.com/washingtonpost/arcads#readme"
}
4 changes: 2 additions & 2 deletions src/__tests__/arcads.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('arcads', () => {
enabled: true
}
}
})
});

describe('#constructor', () => {
it('should initialize arc ads', () => {
Expand All @@ -24,7 +24,7 @@ describe('arcads', () => {
it('should initialize googletag', () => {
const { googletag } = global;
expect(googletag).toBeDefined();
})
});

it('should initialize header bidding serivces', () => {
const { arcBiddingReady } = global;
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { renamePositionKey } from '../util/customTargeting';

describe('The CustomTargeting.js functions', () => {
it('should take targeting and position value, and rename the key as posn', () => {
const targeting = {
position: {
as: 'posn'
}
};

const positionValue = 2;
const updatedTargeting = renamePositionKey(targeting, positionValue);
const newTargeting = {
posn: positionValue
};

expect(updatedTargeting).toEqual(newTargeting);
});
});
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import './util/polyfills';
import { MobileDetection } from './util/mobile';
import { renamePositionKey } from './util/customTargeting';
import { fetchBids, initializeBiddingServices } from './services/headerbidding';
import { initializeGPT, queueGoogletagCommand, refreshSlot, dfpSettings, setTargeting, determineSlotName } from './services/gpt';
import { queuePrebidCommand, addUnit } from './services/prebid';
Expand All @@ -14,7 +16,7 @@ export class ArcAds {
window.isMobile = MobileDetection;

if (this.dfpId === '') {
console.warn(`ArcAds: DFP id is missing from the arcads initialization script.
console.warn(`ArcAds: DFP id is missing from the arcads initialization script.
Documentation: https://github.com/wapopartners/arc-ads#getting-started`);
} else {
initializeGPT();
Expand All @@ -32,12 +34,17 @@ export class ArcAds {

/* If positional targeting doesn't exist it gets assigned a numeric value
based on the order and type of the advertisement. This logic is skipped if adType is not defined. */
if ((!targeting || !targeting.hasOwnProperty('position')) && adType !== false) {

if ((!targeting.hasOwnProperty('position') || typeof targeting.position === 'object') && adType !== false) {
const position = this.positions[adType] + 1 || 1;
this.positions[adType] = position;

const positionParam = Object.assign(targeting, { position });
Object.assign(params, { targeting: positionParam });
if (typeof targeting.position === 'object' && targeting.position.as) {
Object.assign(position, renamePositionKey(targeting, position));
} else {
const positionParam = Object.assign(targeting, { position });
Object.assign(params, { targeting: positionParam });
}
}

if ((isMobile.any() && display === 'mobile') || (!isMobile.any() && display === 'desktop') || (display === 'all')) {
Expand Down
14 changes: 14 additions & 0 deletions src/util/customTargeting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @desc If a different key is required to serve position targeting for older creatives, rename it here.
* @param {object} targeting - Targeting object passed in from the ad object.
* @param {number} positionValue - The nth number of adType included.
* @return - Returns the targeting object with the old position value stripped out, and the new one with the desired key in its place.
**/
export function renamePositionKey(targeting, positionValue) {
const newTargetingObject = targeting;
const keyName = targeting.position.as;
delete newTargetingObject.position;
newTargetingObject[keyName] = positionValue;
Object.assign(targeting, newTargetingObject);
return targeting;
}
41 changes: 41 additions & 0 deletions src/util/polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Promise from 'promise-polyfill';

if (!window.Promise) {
window.Promise = Promise;
}

/* eslint-disable */

// source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
/* Object.assign() for IE11 (obviously) */
if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];

if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}

/* eslint-enable */
Loading

0 comments on commit 4d47c2b

Please sign in to comment.