forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move vast tracking logic out of core
- Loading branch information
Showing
7 changed files
with
144 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import {addBidResponse} from '../../src/auction.js'; | ||
import {VIDEO} from '../../src/mediaTypes.js'; | ||
import {logError} from '../../src/utils.js'; | ||
import {isActivityAllowed} from '../../src/activities/rules.js'; | ||
import {ACTIVITY_REPORT_ANALYTICS} from '../../src/activities/activities.js'; | ||
import {activityParams} from '../../src/activities/activityParams.js'; | ||
|
||
const vastTrackers = []; | ||
|
||
addBidResponse.before(function (next, adUnitcode, bidResponse, reject) { | ||
if (FEATURES.VIDEO && bidResponse.mediaType === VIDEO) { | ||
const vastTrackers = getVastTrackers(bidResponse); | ||
if (vastTrackers) { | ||
bidResponse.vastXml = insertVastTrackers(vastTrackers, bidResponse.vastXml); | ||
const impTrackers = vastTrackers.get('impressions'); | ||
if (impTrackers) { | ||
bidResponse.vastImpUrl = [].concat(impTrackers).concat(bidResponse.vastImpUrl).filter(t => t); | ||
} | ||
} | ||
} | ||
next(adUnitcode, bidResponse, reject); | ||
}); | ||
|
||
export function registerVastTrackers(moduleType, moduleName, trackerFn) { | ||
if (typeof trackerFn === 'function') { | ||
vastTrackers.push({'moduleType': moduleType, 'moduleName': moduleName, 'trackerFn': trackerFn}); | ||
} | ||
} | ||
|
||
export function insertVastTrackers(trackers, vastXml) { | ||
const doc = new DOMParser().parseFromString(vastXml, 'text/xml'); | ||
const wrappers = doc.querySelectorAll('VAST Ad Wrapper, VAST Ad InLine'); | ||
try { | ||
if (wrappers.length) { | ||
wrappers.forEach(wrapper => { | ||
if (trackers.get('impressions')) { | ||
trackers.get('impressions').forEach(trackingUrl => { | ||
const impression = doc.createElement('Impression'); | ||
impression.appendChild(doc.createCDATASection(trackingUrl)); | ||
wrapper.appendChild(impression); | ||
}); | ||
} | ||
}); | ||
vastXml = new XMLSerializer().serializeToString(doc); | ||
} | ||
} catch (error) { | ||
logError('an error happened trying to insert trackers in vastXml'); | ||
} | ||
return vastXml; | ||
} | ||
|
||
export function getVastTrackers(bid) { | ||
let trackers = []; | ||
vastTrackers.filter( | ||
({ | ||
moduleType, | ||
moduleName, | ||
trackerFn | ||
}) => isActivityAllowed(ACTIVITY_REPORT_ANALYTICS, activityParams(moduleType, moduleName)) | ||
).forEach(({trackerFn}) => { | ||
let trackersToAdd = trackerFn(bid); | ||
trackersToAdd.forEach(trackerToAdd => { | ||
if (isValidVastTracker(trackers, trackerToAdd)) { | ||
trackers.push(trackerToAdd); | ||
} | ||
}); | ||
}); | ||
const trackersMap = trackersToMap(trackers); | ||
return (trackersMap.size ? trackersMap : null); | ||
}; | ||
|
||
function isValidVastTracker(trackers, trackerToAdd) { | ||
return trackerToAdd.hasOwnProperty('event') && trackerToAdd.hasOwnProperty('url'); | ||
} | ||
|
||
function trackersToMap(trackers) { | ||
return trackers.reduce((map, {url, event}) => { | ||
!map.has(event) && map.set(event, new Set()); | ||
map.get(event).add(url); | ||
return map; | ||
}, new Map()); | ||
} | ||
|
||
export function addImpUrlToTrackers(bid, trackersMap) { | ||
if (bid.vastImpUrl) { | ||
if (!trackersMap) { | ||
trackersMap = new Map(); | ||
} | ||
if (!trackersMap.get('impressions')) { | ||
trackersMap.set('impressions', new Set()); | ||
} | ||
trackersMap.get('impressions').add(bid.vastImpUrl); | ||
} | ||
return trackersMap; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {addImpUrlToTrackers, getVastTrackers, insertVastTrackers, registerVastTrackers} from 'libraries/vastTrackers/vastTrackers.js'; | ||
import {MODULE_TYPE_ANALYTICS} from '../../../src/activities/modules.js'; | ||
|
||
describe('vast trackers', () => { | ||
it('insert into tracker list', function() { | ||
let trackers = getVastTrackers({'cpm': 1.0}); | ||
if (!trackers || !trackers.get('impressions')) { | ||
registerVastTrackers(MODULE_TYPE_ANALYTICS, 'test', function(bidResponse) { | ||
return [ | ||
{'event': 'impressions', 'url': `https://vasttracking.mydomain.com/vast?cpm=${bidResponse.cpm}`} | ||
]; | ||
}); | ||
} | ||
trackers = getVastTrackers({'cpm': 1.0}); | ||
expect(trackers).to.be.a('map'); | ||
expect(trackers.get('impressions')).to.exists; | ||
expect(trackers.get('impressions').has('https://vasttracking.mydomain.com/vast?cpm=1')).to.be.true; | ||
}); | ||
|
||
it('insert trackers in vastXml', function() { | ||
const trackers = getVastTrackers({'cpm': 1.0}); | ||
let vastXml = '<VAST><Ad><Wrapper></Wrapper></Ad></VAST>'; | ||
vastXml = insertVastTrackers(trackers, vastXml); | ||
expect(vastXml).to.equal('<VAST><Ad><Wrapper><Impression><![CDATA[https://vasttracking.mydomain.com/vast?cpm=1]]></Impression></Wrapper></Ad></VAST>'); | ||
}); | ||
|
||
it('test addImpUrlToTrackers', function() { | ||
const trackers = addImpUrlToTrackers({'vastImpUrl': 'imptracker.com'}, getVastTrackers({'cpm': 1.0})); | ||
expect(trackers).to.be.a('map'); | ||
expect(trackers.get('impressions')).to.exists; | ||
expect(trackers.get('impressions').has('imptracker.com')).to.be.true; | ||
}); | ||
}) |
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