Combining winners and organic results
How to combine auction winners and organic listings
After running a listing auction, you will often want to combine the winners with organic results.
For example, suppose you’re building a category section of a webshop that will also show a number of sponsored products.
You will need a way to “inject” your auction winners into the regular category results.
This page shows the steps involved in building such a category section, but similar considerations apply to other pages or widgets.
Scenario
The category section needs to support pagination and show 3 products per page. If possible, the first product on each page will be a sponsored product.
The products on this category page have the following structure:
The resolvedBidId
is null
when the product is not promoted and contain a string ID when it is.
Our goal is to have the pseudo code in place for an endpoint that could create lists of such products.
1. Query your organic results
The first step will be to query your organic results. The products that should be displayed for a specific page and category.
The code to do this could look like this:
Let’s say we were to run this code for the first page of the Shoes
category, and there’s 3 or more products available in this category. This code would then output something similar to this:
Note that these products don’t have the resolvedBidId
field yet, let’s add it.
Now, we do need to consider what happens if there are no organic results for this page and category. We probably want to return a 404 error and not run any auctions.
This gives us the following code:
With this error handling in place, we’re ready to run an auction.
2. Running an auction
In our scenario we want to run an auction for a single slot:
Not sure how run an auction for a category? Check out these examples.
If there are winners, the output of the above code would look something like this:
If you look at the winners, you will see that they contain no product data, just a bunch of ID’s.
We will need to query the product data for these winners.
Also, it’s possible for there to be no winners. It could simply be that there are no suitable active campaigns for this auction, but there are many other potential reasons.
Regardless, we need to account for this case and we’ll simply return the organic results available in products
.
3. Query product data for winners
Now, when there are winners. We need to query the product data for them.
Following through on our earlier examples, this could log something like:
Again, we need to add the resolvedBidId
to complete this data. But, this time we shouldn’t set it to null
, because we’re now dealing with promoted products.
If we assume queryProductsByIds
will return products in the same order as the provided ids
, we can add the bid IDs as follows:
Why is this bid ID necessary? The bid ID is vital to enable Topsort to attribute events to bids and campaigns.
Now all that remains is to merge our promoted products with the organic results.
4. Merging
We want to show the promoted products at the start of the list.
To achieve, we will need to prepend the promoProducts
to the products
.
However, this can then make products
have more elements than our intended page size of 3. So we need to slice it to remain in this page size.
Now our code returns a maximum of 3 products, of which the first product can be a sponsored product.