-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,11 @@ export function nativeBidIsValid(bid) { | |
return false; | ||
} | ||
|
||
// all native bid responses must defined a landing page url | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: |
||
if (!deepAccess(bid, 'native.clickUrl')) { | ||
return false; | ||
} | ||
|
||
const requestedAssets = bidRequest.nativeParams; | ||
if (!requestedAssets) { | ||
return true; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I know it sucks for testing, but... it seems more robust to set a stub for |
||
} | ||
|
||
/** | ||
* Sets native targeting key-value paris | ||
* @param {Object} bid | ||
* @return {Object} targeting | ||
*/ | ||
export function setNativeTargeting(bid) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably rename to
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to return this anymore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} |
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.