Prebid.js icon indicating copy to clipboard operation
Prebid.js copied to clipboard

How to add costum analytic adapter

Open chensMMG opened this issue 9 years ago • 1 comments

Type of issue

Question

Description

I'm having some issues adding custom analytic adapter.

I cloned the latest version to include the analytics.json, and have followed the API documentation under 'Integrate with the Prebid Analytics API' but with no luck.

In the project you have example (as shown under src/adapters/analytics) how to open custom adapter, I copied and changed the 'example.js' script and added the name to analytics.json.

There is no extra information or examples about how to work with the custom adapter, like explanations about the different 'analyticsType' variable.

After doing all of the above I did not succeed in making the adapter fire the analytics event.

Steps to reproduce

First, I have tried to open analytic adapter like the first example js file - src/adapters/analytics/example.js. The analytics type set to 'library', I opened new js file under src/analytics/libraries and copy the first example file and change all the 'example' params.

Second, I have tried to open analytic adapter like the second example js file - src/adapters/analytics/example2.js. After some checking it seems to be adapter that works only with GA.

Expected results

After running my test prebid script and runnig the snippets codes for checking all registered and winning bids (my bid is registered as a valid bid and winning bid) , the analytic adapter did not fire.

Actual results

Using the first step, when the bid is over an ajax request is fired to the url I entered but with no data.

Platform details

Node version: v4.4.3 Npm version: 2.15.1 Prebid.js version: v0.24.0-pre

Example code

Analytic adapter:

import adapter from 'AnalyticsAdapter';

export default adapter(
  {
    url: 'URL',
    global: 'MarsMediaPrebidAnalytics',
    handler: 'on',
    analyticsType: 'library'
  }
);

Library:

window.MarsMediaPrebidAnalytics = function(hander, type, data) {
  console.log(`call to MarsMediaPrebidAnalytics Analytics library: example('${hander}', '${type}', ${JSON.stringify(data)})`);
};

window[window.MarsMediaPrebidAnalytics] = function() {};

//var utils = require('utils');
//var events = require('events');
//var pbjsHandlers = require('prebid-event-handlers');
var utils = { errorless: function(fn) { return fn; } };

var events = { init: function() { return arguments; } };

var pbjsHandlers = {
  onBidAdjustment: args => console.log('pbjsHandlers onBidAdjustment args:', args),
  onBidTimeout: args => console.log('pbjsHandlers bidTimeout args:', args),
  onBidRequested: args => console.log('pbjsHandlers bidRequested args:', args),
  onBidResponse: args => console.log('pbjsHandlers bidResponse args:', args),
  onBidWon: args => console.log('pbjsHandlers bidWon args:', args)
};

// init
var marsmediaBeacon = window[window.MarsMediaPrebidAnalytics];
var bufferedQueries = marsmediaBeacon.q || [];

events.init();

window[window.MarsMediaPrebidAnalytics] = utils.errorless(function() {
  if (arguments[0] && arguments[0] === 'on') {
    var eventName = arguments[1] && arguments[1];
    var args = arguments[2] && arguments[2];
    if (eventName && args) {
      if (eventName === 'bidAdjustment') {
        pbjsHandlers.onBidAdjustment.apply(this, [args]);
      }
      if (eventName === 'bidTimeout') {
        pbjsHandlers.onBidTimeout.apply(this, [args]);
      }
      if (eventName === 'bidRequested') {
        pbjsHandlers.onBidRequested.apply(this, [args]);
      }
      if (eventName === 'bidResponse') {
        pbjsHandlers.onBidResponse.apply(this, [args]);
      }
      if (eventName === 'bidWon') {
        pbjsHandlers.onBidWon.apply(this, [args]);
      }
    }

    console.log("handling event: "+eventName);
  }
});

// apply bufferedQueries
bufferedQueries.forEach(function(args) {
  marsmediaBeacon.apply(this, args);
});

How do I add some logic to the analytic adapter? Why is the library js file is not fired?

chensMMG avatar May 09 '17 15:05 chensMMG

@chensMMG - I just wrote a custom adapter and found the the documentation lacking for it.

First to enable your adapter, you need to add it to analytics.json, not package.json as mentioned on http://prebid.org/dev-docs/integrate-with-the-prebid-analytics-api.html

You will also need to change the path to get to the analytics source code in the gulpfile.js - I changed mine to var analyticsDirectory = './src/adapters/analytics'; to match the path from the git repo.

When you go to build prebid, you need to use the --analytics param on the build - gulp build --analytics

Lastly, I hacked the gulpHelpers.js to include the files during the concat process, it looks like thye changes how they were determining how to include the files, or at least it doesn't match how they are in the git repo. I change the the getAnalyticsSources to look like this:

getAnalyticsSources: function(directory) {
    if (!argv.analytics) {return [];} // empty arrays won't affect a standard build

    const directoryContents = fs.readdirSync(directory);
    return directoryContents
      .map(moduleDirectory => {
        var fp = path.join(directory, moduleDirectory);
        var fps = fs.statSync(fp);
        if (fps.isDirectory()) {
          fp += '/*.js';
        }

        return fp;
      });
/*      .filter(file => isModuleDirectory(path.join(directory, file)))
      .map(moduleDirectory => {
        const module = require(path.join(directory, moduleDirectory, MANIFEST));
        return path.join(directory, moduleDirectory, module.main);
      });
*/
    // get only subdirectories that contain package.json with 'main' property
    function isModuleDirectory(filePath) {
      try {
        const manifestPath = path.join(filePath, MANIFEST);
        if (fs.statSync(manifestPath).isFile()) {
          const module = require(manifestPath);
          return module && module.main;
        }
      }
      catch (error) {}
    }
  },

Hope that helps.

dlr1967 avatar May 19 '17 15:05 dlr1967