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

Support native click tracking #1691

Merged
merged 3 commits into from
Oct 17, 2017
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
1 change: 1 addition & 0 deletions modules/appnexusAstBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ function newBid(serverBid, rtbBid) {
image: nativeAd.main_img && nativeAd.main_img.url,
icon: nativeAd.icon && nativeAd.icon.url,
clickUrl: nativeAd.link.url,
clickTrackers: nativeAd.link.click_trackers,
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should be validating the different bid types and objects... food for later thought.

impressionTrackers: nativeAd.impression_trackers,
};
} else {
Expand Down
9 changes: 2 additions & 7 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { uniques, flatten, adUnitsFilter, getBidderRequest } from './utils';
import { getPriceBucketString } from './cpmBucketManager';
import { NATIVE_KEYS, nativeBidIsValid } from './native';
import { nativeBidIsValid, setNativeTargeting } from './native';
import { isValidVideoBid } from './video';
import { getCacheUrl, store } from './videoCache';
import { Renderer } from 'src/Renderer';
Expand Down Expand Up @@ -275,13 +275,8 @@ function getKeyValueTargetingPairs(bidderCode, custBidObj) {
custBidObj.sendStandardTargeting = defaultBidderSettingsMap[bidderCode].sendStandardTargeting;
}

// set native key value targeting
if (custBidObj['native']) {
Object.keys(custBidObj['native']).forEach(asset => {
const key = NATIVE_KEYS[asset];
const value = custBidObj['native'][asset];
if (key) { keyValues[key] = value; }
});
keyValues = Object.assign({}, keyValues, setNativeTargeting(custBidObj));
}

return keyValues;
Expand Down
65 changes: 58 additions & 7 deletions src/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export function nativeBidIsValid(bid) {
return false;
}

// all native bid responses must defined a landing page url
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: define

if (!deepAccess(bid, 'native.clickUrl')) {
return false;
}

const requestedAssets = bidRequest.nativeParams;
if (!requestedAssets) {
return true;
Expand All @@ -94,14 +99,60 @@ export function nativeBidIsValid(bid) {
}

/*
* Native responses may have impression trackers. This retrieves the
* impression tracker urls for the given ad object and fires them.
* Native responses may have associated impression or click trackers.
* This retrieves the appropriate tracker urls for the given ad object and
* fires them. As a native creatives may be in a cross-origin frame, it may be
* necessary to invoke this function via postMessage. secureCreatives is
* configured to fire this function when it receives a `message` of 'Prebid Native'
* and an `adId` with the value of the `bid.adId`. When a message is posted with
* these parameters, impression trackers are fired. To fire click trackers, the
* message should contain an `action` set to 'click'.
*
* // Native creative template example usage
* <a href="%%CLICK_URL_UNESC%%%%PATTERN:hb_native_linkurl%%"
* target="_blank"
* onclick="fireTrackers('click')">
* %%PATTERN:hb_native_title%%
* </a>
*
* <script>
* function fireTrackers(action) {
* var message = {message: 'Prebid Native', adId: '%%PATTERN:hb_adid%%'};
* if (action === 'click') {message.action = 'click';} // fires click trackers
* window.parent.postMessage(JSON.stringify(message), '*');
* }
* fireTrackers(); // fires impressions when creative is loaded
* </script>
*/
export function fireNativeImpressions(adObject) {
const impressionTrackers =
adObject['native'] && adObject['native'].impressionTrackers;
export function fireNativeTrackers(message, adObject) {
let trackers;

if (message.action === 'click') {
trackers = adObject['native'] && adObject['native'].clickTrackers;
} else {
trackers = adObject['native'] && adObject['native'].impressionTrackers;
}

(trackers || []).forEach(triggerPixel);

(impressionTrackers || []).forEach(tracker => {
triggerPixel(tracker);
return trackers;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I'd build tests around this return value... what you really want is to make sure that triggerPixel gets called with the expected args. Since this is a separate code path, there's no guarantee that it stays in sync with triggerPixel calls. Code may get added/removed over time.

I know it sucks for testing, but... it seems more robust to set a stub for triggerPixel and make sure that it gets called with the expected args.

}

/**
* Sets native targeting key-value paris
* @param {Object} bid
* @return {Object} targeting
*/
export function setNativeTargeting(bid) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably rename to getNativeTargeting, parseNativeTargeting, or something like that.

set pretty universally means "this function changes some state." In this case, you're building a transformed copy of some existing state without changing it.

let keyValues = {};

Object.keys(bid['native']).forEach(asset => {
const key = NATIVE_KEYS[asset];
const value = bid['native'][asset];
if (key) {
keyValues[key] = value;
}
});

return keyValues;
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to return this anymore

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Eh?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I'm sorry... had a brain fart and thought this was the return trackers line. Nevermind.

}
4 changes: 2 additions & 2 deletions src/secureCreatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import events from './events';
import { fireNativeImpressions } from './native';
import { fireNativeTrackers } from './native';
import { EVENTS } from './constants';

const BID_WON = EVENTS.BID_WON;
Expand Down Expand Up @@ -42,7 +42,7 @@ function receiveMessage(ev) {
// adId: '%%PATTERN:hb_adid%%'
// }), '*');
if (data.message === 'Prebid Native') {
fireNativeImpressions(adObject);
fireNativeTrackers(data, adObject);
$$PREBID_GLOBAL$$._winningBids.push(adObject);
events.emit(BID_WON, adObject);
}
Expand Down
5 changes: 4 additions & 1 deletion test/spec/bidmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,10 @@ describe('bidmanager.js', function () {
{
bidderCode: 'appnexusAst',
mediaType: 'native',
native: {title: 'foo'}
native: {
title: 'foo',
clickUrl: 'example.link'
}
}
);

Expand Down
33 changes: 33 additions & 0 deletions test/spec/native_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from 'chai';
import { fireNativeTrackers, setNativeTargeting } from 'src/native';

const bid = {
native: {
title: 'Native Creative',
body: 'Cool description great stuff',
cta: 'Do it',
sponsoredBy: 'AppNexus',
clickUrl: 'https://www.link.example',
clickTrackers: ['https://tracker.example'],
impressionTrackers: ['https://impression.example'],
}
};

describe('native.js', () => {
it('sets native targeting keys', () => {
const targeting = setNativeTargeting(bid);
expect(targeting.hb_native_title).to.equal(bid.native.title);
expect(targeting.hb_native_body).to.equal(bid.native.body);
expect(targeting.hb_native_linkurl).to.equal(bid.native.clickUrl);
});

it('fires impression trackers', () => {
const fired = fireNativeTrackers({}, bid);
expect(fired).to.deep.equal(bid.native.impressionTrackers);
});

it('fires click trackers', () => {
const fired = fireNativeTrackers({ action: 'click' }, bid);
expect(fired).to.deep.equal(bid.native.clickTrackers);
});
});