-
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
Generic Analytics Adapter: initial release #9134
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b148a3f
New module: generic analytics adapter
dgirardi 23741bb
Use special gvlid value instead of `isVendorless` flag for vendorless…
dgirardi 51060ff
Mark generic analytics as vendorless for gdpr enforcement
dgirardi 215b466
Merge branch 'master' into generic-analytics
dgirardi 391e81e
Allow analytics adapters to define dynamic gvlids
dgirardi b84883c
Add gvlid option
dgirardi 90d54e0
Gdpr enforcement softVendorExceptions
dgirardi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import AnalyticsAdapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js'; | ||
import {prefixLog, isPlainObject} from '../src/utils.js'; | ||
import * as CONSTANTS from '../src/constants.json'; | ||
import adapterManager from '../src/adapterManager.js'; | ||
import {ajaxBuilder} from '../src/ajax.js'; | ||
|
||
const DEFAULTS = { | ||
batchSize: 1, | ||
batchDelay: 100, | ||
method: 'POST' | ||
} | ||
|
||
const TYPES = { | ||
handler: 'function', | ||
batchSize: 'number', | ||
batchDelay: 'number', | ||
gvlid: 'number', | ||
} | ||
|
||
const MAX_CALL_DEPTH = 20; | ||
|
||
export function GenericAnalytics() { | ||
const parent = AnalyticsAdapter({analyticsType: 'endpoint'}); | ||
const {logError, logWarn} = prefixLog('Generic analytics:'); | ||
let batch = []; | ||
let callDepth = 0; | ||
let options, handler, timer, translate; | ||
|
||
function optionsAreValid(options) { | ||
if (!options.url && !options.handler) { | ||
logError('options must specify either `url` or `handler`') | ||
return false; | ||
} | ||
if (options.hasOwnProperty('method') && !['GET', 'POST'].includes(options.method)) { | ||
logError('options.method must be GET or POST'); | ||
return false; | ||
} | ||
for (const [field, type] of Object.entries(TYPES)) { | ||
// eslint-disable-next-line valid-typeof | ||
if (options.hasOwnProperty(field) && typeof options[field] !== type) { | ||
logError(`options.${field} must be a ${type}`); | ||
return false; | ||
} | ||
} | ||
if (options.hasOwnProperty('events')) { | ||
if (!isPlainObject(options.events)) { | ||
logError('options.events must be an object'); | ||
return false; | ||
} | ||
for (const [event, handler] of Object.entries(options.events)) { | ||
if (!CONSTANTS.EVENTS.hasOwnProperty(event)) { | ||
logWarn(`options.events.${event} does not match any known Prebid event`); | ||
if (typeof handler !== 'function') { | ||
logError(`options.events.${event} must be a function`); | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function processBatch() { | ||
const currentBatch = batch; | ||
batch = []; | ||
callDepth++; | ||
try { | ||
// the pub-provided handler may inadvertently cause an infinite chain of events; | ||
// even just logging an exception from it may cause an AUCTION_DEBUG event, that | ||
// gets back to the handler, that throws another exception etc. | ||
// to avoid the issue, put a cap on recursion | ||
if (callDepth === MAX_CALL_DEPTH) { | ||
logError('detected probable infinite recursion, discarding events', currentBatch); | ||
} | ||
if (callDepth >= MAX_CALL_DEPTH) { | ||
return; | ||
} | ||
try { | ||
handler(currentBatch); | ||
} catch (e) { | ||
logError('error executing options.handler', e); | ||
} | ||
} finally { | ||
callDepth--; | ||
} | ||
} | ||
|
||
function translator(eventHandlers) { | ||
if (!eventHandlers) { | ||
return (data) => data; | ||
} | ||
return function ({eventType, args}) { | ||
if (eventHandlers.hasOwnProperty(eventType)) { | ||
try { | ||
return eventHandlers[eventType](args); | ||
} catch (e) { | ||
logError(`error executing options.events.${eventType}`, e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Object.assign( | ||
Object.create(parent), | ||
{ | ||
gvlid(config) { | ||
return config?.options?.gvlid | ||
}, | ||
enableAnalytics(config) { | ||
if (optionsAreValid(config?.options || {})) { | ||
options = Object.assign({}, DEFAULTS, config.options); | ||
handler = options.handler || defaultHandler(options); | ||
translate = translator(options.events); | ||
parent.enableAnalytics.call(this, config); | ||
} | ||
}, | ||
track(event) { | ||
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. is it straightforward to see which events a particular analytics adapter subscribes to? |
||
if (event.eventType === CONSTANTS.EVENTS.AUCTION_INIT && event.args.hasOwnProperty('config')) { | ||
// clean up auctionInit event | ||
// TODO: remove this special case in v8 | ||
delete event.args.config; | ||
} | ||
const datum = translate(event); | ||
if (datum != null) { | ||
batch.push(datum); | ||
if (timer != null) { | ||
clearTimeout(timer); | ||
timer = null; | ||
} | ||
if (batch.length >= options.batchSize) { | ||
processBatch(); | ||
} else { | ||
timer = setTimeout(processBatch, options.batchDelay); | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
export function defaultHandler({url, method, batchSize, ajax = ajaxBuilder()}) { | ||
const callbacks = { | ||
success() {}, | ||
error() {} | ||
} | ||
const extract = batchSize > 1 ? (events) => events : (events) => events[0]; | ||
const serialize = method === 'GET' ? (data) => ({data: JSON.stringify(data)}) : (data) => JSON.stringify(data); | ||
|
||
return function (events) { | ||
ajax(url, callbacks, serialize(extract(events)), {method}) | ||
} | ||
} | ||
|
||
adapterManager.registerAnalyticsAdapter({ | ||
adapter: GenericAnalytics(), | ||
code: 'generic', | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
these infinite loops have occurred before, eg https://github.com/prebid/Prebid.js/pull/7805/files ; should we more generally defend against them?
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.
There is a more general prevention mechanism in #9113. That also allows the pub to set which events should be forwarded to any analytics adapter. However, adapters can choose to ignore some or circumvent the default event tracking system; so in general, the only way to find out which events are tracked is reading code. For this particular adapter though control is entirely on the publisher.