🎉 AI Engineers: Aporia's 2024 Benchmark Report and mutiSLM has been released. View the report here>>

April 4, 2024 - last updated
Machine Learning

Optimizing ad placement in search and recommendation systems: A practical guide


When building search and recommendation systems, one critical aspect to consider is the integration of sponsored content or advertisements into the results. For example, companies like Google and Amazon display sponsored results alongside organic ones. This article will guide you through the process of integrating ads into your search and recommendation results while maintaining a positive user experience.

Problem overview

Integrating ads into search and recommendation results presents several challenges, such as:

  • Determining where the ads should be injected in the stack.
  • Deciding the placement of ads within the result set.
  • Ensuring ads do not negatively impact user experience.
  • Balancing ad revenue with customer satisfaction.

There are different approaches to integrating ads into your search and recommendation results. Two common methods are to display ads in fixed positions or to interleave them within the organic results. Here, we will discuss both approaches and provide some code examples.

When building search and recommendation systems, one critical aspect to consider is the integration of sponsored content or advertisements into the results.

Method 1: Ads in fixed positions

In this method, ads are displayed at predefined positions within the results (e.g., positions 2 and 5 out of 6). You can achieve this by adding an orchestration layer that aggregates organic results and ad results, pushing the search or recommendations results down accordingly.

When building search and recommendation systems, one critical aspect to consider is the integration of sponsored content or advertisements into the results.

Here’s a sample implementation using Python:

def integrate_ads(organic_results, ad_results, ad_positions):
    Insert ad_results into organic_results at specified ad_positions and return the modified list.

        organic_results (list): A list of organic search results, e.g. ["result", "result", "result"].
        ad_results (list): A list of ads to be inserted, e.g. ["ad", "ad"].
        ad_positions (list): A list of positions for ads, e.g. [1, 4].

        list: The modified list with ad_results inserted into organic_results at ad_positions.
    for index, ad in enumerate(ad_results):
        organic_results.insert(ad_positions[index], ad)
    return organic_results

integrated = integrate_ads(["organic 1", "organic 2", "organic 3", "organic 4", "organic 5"], ["ad 1", "ad 2"], [1,3])

This will output the following:

['organic 1', 'ad 1', 'organic 2', 'ad 2', 'organic 3', 'organic 4', 'organic 5']

Real-world example of ads in fixed positions

A classic example of this strategy is Google’s search engine. When you perform a search on Google, the first few results (typically 1-3) are often sponsored ads. These ads are relevant to your search query but are placed in predefined positions at the top of the search results. This strategy is not limited to Google but is also used by other search engines like Bing and Yahoo.

Method 2: In-feed ads

In-feed ads are interleaved within the organic results and require a more sophisticated approach, such as a third-pass ranker that accounts for ad margins and business objectives. This method can improve user experience by displaying ads more naturally within the result set.

Examples of different ranking logic could include:

  • Prioritizing ads with higher bids or relevance scores.
  • Prioritizing ads that are contextually relevant to the user’s search or browsing history.
  • Considering user engagement metrics, such as click-through rates (CTR) or conversion rates, when ranking ads.

To implement In-feed ads, you can modify the integrate_ads function from the previous example, adding a custom ranking function to control the placement of ads.

def should_pair(organic_result, ad_result):
    threshold = 2
    organic_keywords = set(organic_result.lower().split())
    ad_keywords = set(ad_result.lower().split())
    return len(organic_keywords.intersection(ad_keywords)) > threshold

def integrate_ads(organic_results, ad_results):
    integrated = []
    for organic_index, organic_result in enumerate(organic_results):
        for ad in ad_results:
            if should_pair(organic_result, ad):
    return integrated

organic_results = ["a spring jacket for men", "a winter coat for women", "a snow jacket for children"] 
ads = ["ad: spring jacket selection for men", "ad: snow clothing for children"]

integrated = integrate_ads(organic_results, ads)

This considers each ad for each result, and if it has overlapping keywords it will display the ad after the results. In our example, the code will output the following:

['a spring jacket for men', 'ad: spring jacket selection for men', 'a winter coat for women', 'a snow jacket for children', 'ad: snow clothing for children']

Note that this means that sometimes not all ads will be shown, and it is possible that no ads will be shown.

Real-World examples of In-feed ads

A well-known example of this approach is Facebook’s News Feed or Instagram’s Feed. As you scroll through your feed, you will see sponsored posts or ads appearing amidst posts from your friends or people you follow. These sponsored posts are interleaved within organic content. The placement of these ads is determined by Facebook’s or Instagram’s ad placement algorithm which considers factors like your interests, interactions, and other behavioral data to serve you ads that you’re more likely to engage with. Another example is YouTube, which interleaves ads within the list of recommended videos, based on your viewing history and preferences.

Monitoring search & recommendations Ad models with Aporia

Aporia’s ML observability platform helps ensure your ad integrations are performing at their best. With the platform, you’re able to easily optimize ad integration performance, and discover untapped revenue streams. Aporia’s integration into your ad search and ranking setup takes under 7 minutes, enabling you to scale and improve your ML models with confidence and speed.

See how Aporia enhances your ad ranking models:

  • Monitor your ads and recommendations to gain insights into ad performance and user engagement for each position. This helps you ensure that your ad placements are optimized for both user experience and revenue generation.
  • Detect drift in the interleaved ads ranking algorithm to ensure that your ad placements remain relevant and effective over time, as user preferences and market conditions evolve.
  • Track nDCG, CTR, and other performance metrics for different ad positions to determine the best ad placements and opportunities for maximizing return on ad spend. 
  • Perform A/B testing on ads in predefined positions.
  • Measure and visualize what works best, allowing you to tailor your interleaved ads to the specific preferences and behaviors of your users. 
  • Slice and dice ad segments to find new connections in your data, and identify the root cause of any issues in production models. 

Wrapping up

Integrating ads into search and recommendation results is a critical aspect of monetizing your systems without negatively impacting the user experience. By using the techniques outlined in this article and leveraging Aporia’s ML Observability platform, you can efficiently manage your ad placements while maintaining high-quality search and recommendation services.

Want to learn more about optimizing ad search and recommendations with Aporia? Book a demo with one of our ML observability experts.

Rate this article

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Green Background

Control All your GenAI Apps in minutes