Skip to content

Commit

Permalink
Merge pull request #94 from theonion/article-position
Browse files Browse the repository at this point in the history
add article positition to ad call
  • Loading branch information
brycedorn authored Nov 14, 2018
2 parents e01baad + 8dcfb8f commit 8cedb9e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/helpers/TargetingPairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,48 @@ var TargetingPairs = {
return (tagMeta.tags || '').split(',');
},

/**
* Gets index of article position in reading list
*
* @param HTMLElement adContainer the ad container currently being filled
* @param object kinjaMeta kinja-specific post meta
*/
getArticlePosition: function (adContainer, kinjaMeta) {
if (document && adContainer && kinjaMeta.pageType === "permalink" && !(kinjaMeta.post && kinjaMeta.post.isFeatured)) {
var readingListPostIds = Array.from(document.getElementsByClassName('js_reading-list-item')).map(post => post.dataset.postId);

if (readingListPostIds.length > 0) {
var readingListItem = adContainer.closest('.js_reading-list-item');
if (readingListItem) {
var currentPostId = readingListItem.dataset.postId;
return readingListPostIds.indexOf(currentPostId) + 2;
}
}
}

return 1;
},

/**
* Constructs associative array of targeting pairs from post meta
*
* @param Window scope the window to use for the kinja meta info.
*
*/
buildTargetingPairs: function (scope, positionTargeting) {
buildTargetingPairs: function (scope, positionTargeting, adContainer) {
var kinjaMeta = scope.kinja.meta,
post = scope.kinja.postMeta || {},
content = scope.kinja.postContentRatings || [],
experimentVariation = Experiments.getExperimentVariation(scope),
experimentId = Experiments.getExperimentId(scope),
socialTargeting, key,
targeting = {};
targeting = {},
articlePosition = TargetingPairs.getArticlePosition(adContainer, kinjaMeta);

// Begin slot level params AKA 'scp'
targeting.slotOptions = {
// Standard targeting options
article_position: articlePosition,
pos: positionTargeting || null,
postId: post ? post.postId : null,
socialReferrer: SocialReferrer.getSocialReferrer(),
Expand Down Expand Up @@ -78,17 +102,18 @@ var TargetingPairs = {
*
* @param string forcedAdZone forced ad zone, if any
* @param Window scope the window to use for the kinja meta info.
* @param element adContainer optional ad container to determine positioning
*
*/
getTargetingPairs: function (forcedAdZone, positionTargeting) {
getTargetingPairs: function (forcedAdZone, positionTargeting, adContainer) {
if (forcedAdZone && !window.kinja) {
return { pageOptions : { forcedAdZone : forcedAdZone} };
}
if (!window.kinja) {
return {};
}

var targetingOptions = this.buildTargetingPairs(window, positionTargeting);
var targetingOptions = this.buildTargetingPairs(window, positionTargeting, adContainer);

targetingOptions.pageOptions.forcedAdZone = forcedAdZone || false;

Expand Down
15 changes: 15 additions & 0 deletions src/helpers/TargetingPairs.spec.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,20 @@ describe('TargetingPairs', function() {
expect(spy).not.toHaveBeenCalled();
});
});

describe('article positioning', function() {
beforeEach(function() {
window.document.body.innerHTML = `
<div class='js_reading-list-item' data-post-id='1' >
<div class='ad-container' />
</div>
`;
});

it('returns the index of the article in reading list', function() {
var childElement = document.body.getElementsByClassName('ad-container')[0];
expect(TargetingPairs.getArticlePosition(childElement, {})).toEqual(1);
});
});
});
});
2 changes: 1 addition & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ AdManager.prototype.slotInfo = function () {
AdManager.prototype.setSlotTargeting = function (element, slot, adUnitConfig) {
var slotTargeting = element.dataset.targeting ? JSON.parse(element.dataset.targeting) : {};
slotTargeting.pos = slotTargeting.pos || adUnitConfig.pos || adUnitConfig.slotName || element.dataset.adUnit;
var kinjaPairs = TargetingPairs.getTargetingPairs(AdZone.forcedAdZone(), slotTargeting.pos).slotOptions;
var kinjaPairs = TargetingPairs.getTargetingPairs(AdZone.forcedAdZone(), slotTargeting.pos, element).slotOptions;

slotTargeting = utils.extend(kinjaPairs, slotTargeting);

Expand Down

0 comments on commit 8cedb9e

Please sign in to comment.