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.
Generic Analytics Adapter: initial release (prebid#9134)
* New module: generic analytics adapter * Use special gvlid value instead of `isVendorless` flag for vendorless consent checks * Mark generic analytics as vendorless for gdpr enforcement * Allow analytics adapters to define dynamic gvlids * Add gvlid option * Gdpr enforcement softVendorExceptions
- Loading branch information
1 parent
720525c
commit 5a53df0
Showing
10 changed files
with
605 additions
and
98 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
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) { | ||
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.