API Integration with Prebid.js (GAM)

API Integration with Prebid.js (GAM)


Introduction

Integrating an API with a Prebid.js setup in a GAM environment can enhance functionalities such as fraud detection, analytics, or performance tracking. Below is a professional and engaging step-by-step guide to ensure seamless integration.


Steps to Integrate an API with Prebid.js in GAM

1. Identify API Usage

Understanding the purpose of the API within your Prebid.js setup is crucial. Common use cases include:

  • Pre-bid Filtering: Filtering traffic before Prebid sends bid requests to demand partners.
  • Bid Validation: Validating bids after receipt by Prebid but before forwarding them to GAM.
  • Post-bid Analytics: Delivering analytics or reports after bids are served in GAM.

2. Modify the Prebid.js Setup

Prebid.js provides hooks and events to customize the workflow and integrate APIs effectively.

a. Pre-request API Call (Before Bidding)

Use an API to validate traffic or enrich user data before Prebid.js sends bid requests.

Example Implementation:

function callApiAndBid() { // Example API call fetch('https://api.example.com/validate-traffic', { method: 'POST', body: JSON.stringify({ pageUrl: window.location.href }), }) .then(response => response.json()) .then(data => { if (data.valid) { // Proceed with Prebid auction pbjs.requestBids({ bidsBackHandler: sendAdserverRequest }); } else { // Skip auction or modify behavior console.log('Invalid traffic, skipping auction'); } }); } callApiAndBid();

This approach ensures only valid traffic triggers the auction.

b. Post-Bid API Call (After Receiving Bids)

Validate or enrich bid responses using the API after receipt but before forwarding them to GAM.

Example Implementation:

pbjs.onEvent('bidResponse', function(bid) { // Validate or enrich bid data fetch('https://api.example.com/validate-bid', { method: 'POST', body: JSON.stringify({ bidData: bid }), }) .then(response => response.json()) .then(data => { if (data.valid) { pbjs.addBidResponse(bid); } else { console.log('Invalid bid, rejecting.'); } }); });

This ensures only valid bids proceed to GAM.

c. Post-Auction API Call (After Auction Completion)

Send auction results to an API for analytics or tracking after Prebid.js selects the winning bid and passes it to GAM.

Example Implementation:

function sendAdserverRequest() { // Send auction result to API fetch('https://api.example.com/auction-result', { method: 'POST', body: JSON.stringify({ winningBid: pbjs.getHighestCpmBids(), }), }); // Pass the winning bid to GAM googletag.cmd.push(function() { pbjs.que.push(function() { pbjs.setTargetingForGPTAsync(); googletag.pubads().refresh(); }); }); }

This approach allows you to track or analyze winning bids effectively.

3. Configure GAM to Work with Prebid.js

Ensure your GAM setup aligns with Prebid.js by:

  • Configuring line items to listen for Prebid key-values.
  • Passing UTM parameters or key-value pairs required for targeting from Prebid.js to GAM.
  • Adjusting configurations if new key-value pairs or bidding logic are introduced.

4. Test and Optimize

Conduct thorough testing to ensure the integration performs as expected:

  • Monitor Latency: Ensure API response times are within Prebid.js’s auction timeout to avoid delays.
  • Use Asynchronous Calls: For non-critical APIs, such as those used for analytics, ensure asynchronous execution to prevent blocking the auction.
  • Implement Error Handling: Add robust error handling to manage API failures without impacting the auction process.


Key Considerations

  • Latency: Ensure API calls are quick and do not exceed Prebid.js’s auction timeout.
  • Asynchronous Execution: For APIs not requiring real-time interaction, execute asynchronously to avoid delays.
  • Error Management: Use proper error handling to prevent issues caused by API failures.

By following these steps, you can enhance your Prebid.js setup in a GAM environment, enabling advanced functionalities like fraud detection, bid validation, or analytics tracking.

For further assistance or specific guidance on your API or Prebid.js configuration, feel free to reach out!