-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit AD_RENDER_SUCCEEDED and AD_RENDER_FAILED for native ads (#199)
* Emit AD_RENDER_SUCCEEDED and AD_RENDER_FAILED for native ads * Merge master
- Loading branch information
Showing
8 changed files
with
361 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,53 @@ | ||
/* | ||
* Script to handle firing impression and click trackers from native teamplates | ||
*/ | ||
import { newNativeAssetManager } from './nativeAssetManager'; | ||
import {newNativeAssetManager} from './nativeAssetManager'; | ||
import {prebidMessenger} from './messaging.js'; | ||
|
||
const AD_ANCHOR_CLASS_NAME = 'pb-click'; | ||
const AD_DATA_ADID_ATTRIBUTE = 'pbAdId'; | ||
|
||
export function newNativeRenderManager(win) { | ||
let sendMessage; | ||
|
||
|
||
function findAdElements(className) { | ||
let adElements = win.document.getElementsByClassName(className); | ||
return adElements || []; | ||
} | ||
|
||
function loadClickTrackers(event, adId) { | ||
fireTracker(adId, 'click'); | ||
} | ||
|
||
function fireTracker(adId, action) { | ||
if (adId === '') { | ||
console.warn('Prebid tracking event was missing \'adId\'. Was adId macro set in the HTML attribute ' + AD_DATA_ADID_ATTRIBUTE + 'on the ad\'s anchor element'); | ||
} else { | ||
let message = { message: 'Prebid Native', adId: adId }; | ||
|
||
// fires click trackers when called via link | ||
if (action === 'click') { | ||
message.action = 'click'; | ||
} | ||
sendMessage(message); | ||
} | ||
} | ||
|
||
function fireNativeImpTracker(adId) { | ||
fireTracker(adId, 'impression'); | ||
} | ||
|
||
function fireNativeCallback() { | ||
const adElements = findAdElements(AD_ANCHOR_CLASS_NAME); | ||
for (let i = 0; i < adElements.length; i++) { | ||
adElements[i].addEventListener('click', function(event) { | ||
loadClickTrackers(event, window.pbNativeData.adId); | ||
}, true); | ||
} | ||
} | ||
|
||
// START OF MAIN CODE | ||
let renderNativeAd = function(doc, nativeTag) { | ||
window.pbNativeData = nativeTag; | ||
sendMessage = prebidMessenger(nativeTag.pubUrl, win); | ||
const nativeAssetManager = newNativeAssetManager(window, nativeTag); | ||
|
||
if (nativeTag.hasOwnProperty('adId')) { | ||
|
||
if (nativeTag.hasOwnProperty('rendererUrl') && !nativeTag.rendererUrl.match(/##.*##/i)) { | ||
const scr = doc.createElement('SCRIPT'); | ||
scr.src = nativeTag.rendererUrl, | ||
scr.id = 'pb-native-renderer'; | ||
doc.body.appendChild(scr); | ||
} | ||
nativeAssetManager.loadAssets(nativeTag.adId, () => { | ||
fireNativeImpTracker(nativeTag.adId); | ||
fireNativeCallback(); | ||
}); | ||
} else { | ||
console.warn("Prebid Native Tag object was missing 'adId'."); | ||
} | ||
} | ||
|
||
return { | ||
renderNativeAd | ||
} | ||
export function newNativeRenderManager(win, mkMessenger = prebidMessenger, assetMgr = newNativeAssetManager) { | ||
let sendMessage; | ||
|
||
|
||
let renderNativeAd = function (doc, nativeTag) { | ||
window.pbNativeData = nativeTag; | ||
sendMessage = mkMessenger(nativeTag.pubUrl, win); | ||
|
||
function signalResult(adId, success, info) { | ||
sendMessage({ | ||
message: 'Prebid Event', | ||
adId, | ||
event: success ? 'adRenderSucceeded' : 'adRenderFailed', | ||
info | ||
}); | ||
} | ||
|
||
try { | ||
const nativeAssetManager = assetMgr(window, nativeTag); | ||
|
||
if (nativeTag.adId != null) { | ||
|
||
if (nativeTag.hasOwnProperty('rendererUrl') && !nativeTag.rendererUrl.match(/##.*##/i)) { | ||
const scr = doc.createElement('SCRIPT'); | ||
scr.src = nativeTag.rendererUrl, | ||
scr.id = 'pb-native-renderer'; | ||
doc.body.appendChild(scr); | ||
} | ||
nativeAssetManager.loadAssets(nativeTag.adId, () => { | ||
signalResult(nativeTag.adId, true); | ||
}, (e) => { | ||
signalResult(nativeTag.adId, false, {reason: 'exception', message: e.message}); | ||
}); | ||
} else { | ||
signalResult(null, false, {reason: 'missingDocOrAdid'}); | ||
console.warn('Prebid Native Tag object was missing \'adId\'.'); | ||
} | ||
} catch (e) { | ||
signalResult(nativeTag && nativeTag.adId, false, {reason: 'exception', message: e.message}); | ||
console.error('Error rendering ad', e); | ||
} | ||
}; | ||
|
||
return { | ||
renderNativeAd | ||
}; | ||
} |
Oops, something went wrong.