Add custom geolocation data to Prebid.js bidders request

54 Views Asked by At

I'm trying to find the way to set custom geo data to Prebid.js bidders request.

I found that there's a official geolocation module that checks user's permssion on the front to get it.

I found in the code that there's requestBidsObject.ortb2Fragments.global.device.geo attribute were set to needed value:

deepSetValue(requestBidsObject, 'ortb2Fragments.global.device.geo', {
    lat: geolocation.coords.latitude,
    lon: geolocation.coords.longitude,
    lastfix: Math.round((timestamp() - geolocation.timestamp) / 1000),
    type: 1
});

Is there a legal way to set it with some custom data before sendign the bidders requests?

1

There are 1 best solutions below

0
On

I've created a new module for this issue, just added the new file called customGeolocationRtdProvider.js which contents:

import {submodule} from '../src/hook.js';
import {deepAccess, deepSetValue, logInfo, logWarn, timestamp} from '../src/utils.js';

let geolocation;
function getGeolocationData(requestBidsObject, onDone, providerConfig, userConsent) {
  geolocation = deepAccess(providerConfig, 'params.geo');
  console.log("GEOLOCATION ", geolocation);
  console.log(deepAccess(providerConfig, 'params'));
  if (geolocation) {
    deepSetValue(requestBidsObject, 'ortb2Fragments.global.device.geo', {
      lat: geolocation.coords.latitude,
      lon: geolocation.coords.longitude,
      lastfix: Math.round((timestamp() - geolocation.timestamp) / 1000),
      type: 1
    });
    logInfo('geolocation was successfully received ', requestBidsObject.ortb2Fragments.global.device.geo)
  }
  else
  {
    logWarn('no custom geolocation object provided')
  }
  
  onDone();
}
function init(moduleConfig) {
  geolocation = void 0;
  return true;
}
export const customGeolocationSubmodule = {
  name: 'customGeolocation',
  getBidRequestData: getGeolocationData,
  init: init,
};
function registerSubModule() {
  submodule('realTimeData', customGeolocationSubmodule);
}
registerSubModule();

To build the new prebid.js i've just added the new line to my modules.json:

{
  ...
  "customGeolocationRtdProvider"
  ...
}

Now i can provide the geolocation json right to prebid config:

pbjs.setConfig({
    realTimeData: {
        dataProviders: [
            {
                name: "customGeolocation",
                waitForIt: true,
                params: {
                    geo: {
                       latitude: someLatitudeFloat,
                       longitude: someLongitudeFloat
                    }
                }
            }
        ]
    }
});