In the previous post, I discussed how to find/display content-relevant native ads by targeting to particular keywords and categories using Googletag. In this post, I’ll show you how to get the keywords and categories of a page automatically in real time when a page is viewed, and then, display relevant native ads using Googletag.
The following code is taken from Ads relevant to content with Adrelevantis bidder adapter.
//Content-Driven Advertising needs access to content. So, wait DOMContentLoaded event to start the bid process
document.addEventListener("DOMContentLoaded", function(event){
//Content-Driven Advertising refers to individual pages
//Set referrer to no-referrer-when-downgrade to ensure safety while providing page path
if (document.querySelector('meta[name="referrer"]') === null){
var meta = document.createElement('meta');
meta.name = "referrer";
meta.content = "no-referrer-when-downgrade";
document.getElementsByTagName('head')[0].appendChild(meta);
}
else {
document.querySelector('meta[name="referrer"]').content = "no-referrer-when-downgrade";
}
var q = document.getElementsByTagName('body')[0].innerText;
var pload = 'q=' + encodeURIComponent(q);
var h = new XMLHttpRequest();
h.onreadystatechange = function () {
if (4 === this.readyState) {
if (200 === this.status) {
var res = JSON.parse(this.responseText);
if (res != null){
var cats = res["Category"];
var keywrds = res["Keyword"].replaceAll(/\|/g,",");
bidFunc(keywrds, cats)
}
}
}
};
h.open("POST", "https://api.adrelevantis.com/getcatskeywords", true);
h.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
h.send(pload)
});
When DOMContentLoaded is fired, the page content is sent to a web service called “https://api.adrelevantis.com/getcatskeywords“. The web service returns ranked keywords and IAB Categories of the content. The keywords and the categories are passed into bidFunc to start bid.
In summary, here is what happens when a page is viewed. A user opens the page -> At DOMContentLoaded event, the page content is parsed for its keywords and IAB categories -> The keywords and the Categories are sent to the bidder -> The bidder returns a bid that matches the keywords and the categories along with other bid parameters -> The bid is sent to Google Ad Manager for display.
Here are a few live examples. Click them to see native ads that are relevant to their content.
Pets: how-you-can-maintain-your-pets-dental-health
Cooking: essential-french-onion-soup
Entertainment: laura-marano-new-single-let-me-cry
Home and Garden: the-impracticality-of-hardwood-flooring
Sports: big-ten-reporters-pick-michigan-to-win-league-title-in-2019
Contact: info@adrelevantis.com.
To understand what the Content-Driven Advertising is, please read the following post:





