Conditional Ad Placement
From developer.newsgator.com
If you would like to control at what times an ad should be displayed in your widget, you can implement some code that will handle this. A good example is if you have a cobranded widget campaign and only want particular (approved) domains to show your ad.
There are a few approaches you can take, this is just one example. It's worth noting that we are working on building this into our Editor's Desk utility so you can manage this via a UI, but in the meantime you can code it yourself.
1. Create a domain whitelist.
Create a simple JavaScript file that contains an array for each of the domains that you'd like to be able to display ads, something like this:
var ngAdsApprovedList = [ //To add a new domain to the whitelist //copy the first line from the list, paste it at the top of the list, and change the part inside the quotes // be sure to use the exact domain portion of a real URL for the site. If the site uses "www" or ".org" then you should here. "newsgatorwidgets.com", "hosted.newsgator.com" ];
Upload this JS file into your Editor's Desk account (File Upload utility)
2. In the widget JS, include this function to determine what domain the widget is residing on and whether or not it is in the approved list.
function ngAllowAd(){
if(typeof ngAdsApprovedList == 'undefined' || !ngAdsApprovedList.length){
return false; //couldn't load the whitelist, bail out
}
var host = null;
try{
if(document && document.location){
host = document.location.host;
}
} catch(e){}
if(!host){
try{
host = window.location.host;
} catch(e){}
}
if(!host){
return false; //couldn't figure out where we are, bail out
}
host = host.toLowerCase();
for(var i = 0; i < ngAdsApprovedList.length; i++){
if(host == ngAdsApprovedList[i].toLowerCase()){
return true; //domain is approved, show ads
}
}
//domain not on the whitelist, don't show ads
return false;
}
3. In the widget HTML:
Load the JS file:
{if LoadScript(NGBaseUrl + "/host/" + OrgCode + "/js/AdsApprovedList.js", "typeof ngAdsApprovedList != 'undefined'")}
<!--- All of your widget code --->
{/if}
Load the ad if the domain is in the approved list
{if (ngAllowAd())}
<iframe id="ng_adContainer" src="http://my_ad.html" scrolling="no" style="width:244px;height:70px;border:0;" frameborder="0"></iframe>
{/if}
