How to Add an Analytics Adapter

The Prebid Analytics API provides a way to get analytics data from Prebid.js and send it to the analytics provider of your choice, such as Google Analytics. Because it’s an open source API, you can write an adapter to send analytics data to any provider you like. Integrating with the Prebid Analytics API has the following benefits:

Architecture of the Analytics API

Before we jump into looking at code, let’s look at the high-level architecture. As shown in the diagram below, Prebid.js calls into an analytics adapter. The analytics adapter is the only part of the code that must be stored in the Prebid.js repo.

The analytics adapter listens to events and may call out directly to the analytics backend, or it may call out to an analytics library that integrates with the analytics server.

For instructions on integrating an analytics provider, see the next section.

Prebid Analytics Architecture Diagram

Creating an Analytics Module

Working with any Prebid project requires using Github. In general, we recommend the same basic workflow for any project:

  1. Fork the appropriate Prebid repository (e.g. Prebid.js).
  2. Create a branch in your fork for your proposed code change. (e.g. feature/exAnalyticsAdapter)
  3. Build and test your feature/bug fix in the branch.
  4. Open a pull request to the appropriate repository’s master branch with a good description of the feature/bug fix.
  5. If there’s something that needs to change on the website, follow the above steps for the website repo.

Step 1: Add a markdown file describing the module

  1. Create a markdown file under modules with the name of the bidder suffixed with ‘AnalyticsAdapter’, e.g.,

Example markdown file:

# Overview

Module Name: Ex Analytics Adapter
Module Type: Analytics Adapter

# Description

Analytics adapter for Contact for information.

Step 2: Add analytics source code

  1. Create a JS file under modules with the name of the bidder suffixed with ‘AnalyticsAdapter’, e.g., exAnalyticsAdapter.js

  2. Create an analytics adapter to listen for Prebid events and call the analytics library or server. See the existing *AnalyticsAdapter.js files in the repo under modules.

  3. There are two types of analytics adapters. The example here focuses on the ‘endpoint’ type. See AnalyticsAdapter.js for more info on the ‘bundle’ type.

    • endpoint - Calls the specified URL on analytics events. Doesn’t require a global context.
    • bundle - An advanced option expecting a global context.
  4. In order to get access to the configuration passed in from the page, the analytics adapter needs to specify an enableAnalytics() function, but it should also call the base class function to set up the events.

A basic prototype analytics adapter:

import {ajax} from 'src/ajax';
import adapter from 'src/AnalyticsAdapter';
import CONSTANTS from 'src/constants.json';
import adaptermanager from 'src/adaptermanager';

const analyticsType = 'endpoint';

let exAnalytics = Object.assign(adapter({url, analyticsType}), {
  // ... code ...

// save the base class function
exAnalytics.originEnableAnalytics = exAdapter.enableAnalytics;

// override enableAnalytics so we can get access to the config passed in from the page
exAnalytics.enableAnalytics = function (config) {
  initOptions = config.options;
  exAnalytics.originEnableAnalytics(config);  // call the base class function

  adapter: exAnalytics,
  code: 'exAnalytic'

Analytics adapter best practices:

  • listen only to the events required
  • batch up calls to the backend for post-auction logging rather than calling immediately after each event.

Step 3: Add unit tests

  1. Create a JS file under test/spec/modules with the name of the bidder suffixed with ‘AnalyticsAdapter_spec’, e.g., exAnalyticsAdapter_spec.js

  2. Write great unit tests. See the other AnalyticsAdapter_spec.js files for examples.

Step 4: Submit the code

Once everything looks good, submit the code, tests, and markdown as a pull request to the Prebid.js repo.

Step 5: Website pull request

There are two files that need to be updated to list your new analytics adapter.

  1. Create a fork of the website repo and a branch for your new adapter. (e.g. feature/exAnalyticsAdapter)

  2. Update overview/ to add your adapter alphabetically into the list.

  3. Update to add your new adapter alphabetically into the li st of other analytics adapters.

  4. Submit the pull request to the repo.

Step 6: Wait for Prebid volunteers to review

We sometimes get pretty busy, so it can take a couple of weeks for the review process to complete, so while you’re waiting, consider joining to help us out with code reviews. (!)

Further Reading