I'm working on a custom (self-hosted) ads solution that allows the users to create ad units (by uploading the banner image and target URL) and get the ad display code. A JSON response is returned (by the endpoint) for the requested ad. As of now, here is what I do to get and display the ad on the page where ad code is added:
JSON output at the endpoint:
{
"success": true,
"ad_url": "https:\/\/adlandingpage.com",
"ad_banner": "http:\/\/localhost\/ads\/complete\/uploads\/banners\/add-banner.gif"
}
Content of rw-ads.js:
var getAds = function(ad_id, callback) {
url = 'http://localhost/ads/complete/index.php/ads/adjson/'+ad_id+'/';
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if(status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
xhr.send();
};
Ad code to be added to a page:
<div id="ad1"></div>
<script src="http://myadsplatform.tld/assets/js/rw-ads.js"></script>
<script>
var ad_id = 1;
getAds(ad_id, function(err, data) {
if(data.success) {
$('#ad1').html('<a href="'+data.ad_url+'"><img style="max-width: 100%;" src="'+data.ad_banner+'"></a>');
}
});
</script>
I need suggestions from experts. Am I doing this correctly or this can be improved? Thank you in advance for your help.