Load videoplayer only when creative exist

109 Views Asked by At

I created a Ad unit for video in Google AdManager. Manual is here: https://support.google.com/admanager/answer/1181016?hl=en. I used generated AdTagUrl in my Radiant Player (I want to use it for video advertisement included to article). Manual is here: https://www.radiantmediaplayer.com/docs/latest/outstream-video-ads.html. It's working OK. But only when AdManager sends a creative. When AdManager sends no creative, I must hide the player.

Is there a way to detect, if AdManager sends a creative before loading the player?

For example something like:

function showOutstreamVideo()
{
    //detect if there is an advertisement
    var x = new XMLHttpRequest();
    x.open("GET", AdTagUrl, true);
    x.onreadystatechange = function () {
    if (x.readyState == 4 && x.status == 200)
    {
        var vast = x.responseXML;
        if(vast.getElementsByTagName("Ad").length>0)
        {   //code for read, init and place player
        }
  }
}

...but I cannot init Radiant Player with this vast object. And when I use adTagUrl again, for AdManager it'll be second impression, that can be different. Is there a solution to prevent the page from unnecessarily requesting the player script, when there is no creative?

Thanks and sorry for my English

2

There are 2 best solutions below

2
Christoph Schellhaus On

Have you tried loading the Radiant Player dynamically?

var settings = {};
var vast = x.responseXML;
if(vast.getElementsByTagName("Ad").length>0) {
    var player = document.createElement('script');
    player.src = "https://cdn.radiantmediatechs.com/rmp/5.10.6/js/rmp.min.js";
    document.body.appendChild(player);
    var elementID = 'rmpPlayer';
    var rmp = new RadiantMP(elementID);
    rmp.init(settings);
}

0
user1897730 On

AdResponse: vast, (as string) was the right choice in player setting.

    var x = new XMLHttpRequest();
    x.open("GET", outstreamAdTagUrl, true);
    x.onreadystatechange = function ()
    {   if (x.readyState == 4 && x.status == 200)
        {   vast = x.responseText; //x.responeXML;
            //if(vast.getElementsByTagName("Ad").length>0)
            if(vast.length > 200) //it should be sufficient to determine if it is an ad or an empty VAST
            {   //read and init player
                var player = document.createElement('script');
                document.body.appendChild(player);
                player.onload = function(){runOutstream(vast);};
                player.setAttribute('src', 'https://cdn.radiantmediatechs.com/rmp/5.10.6/js/rmp.min.js');
            }
        }
    };
    x.send(null);

    function runOutstream(vast)
    {
    var settings = {
        ... license key etc...
        autoplay: true,
        adOutStreamMutedAutoplay: true,
        ads: true,
        //adsResponse: new XMLSerializer().serializeToString(vast), //when using responseXML()
        adsResponse: vast,
        adOutStream: true,
        skin: 'outstream',
        ...other setting
    };

    var rmp = new RadiantMP('rmpPlayer');
    rmp.init(settings);
    }