javascriptadsadsensegoogle-dfp

Finding ad status in google IMA ads (Completed or Skipped)?


I need to find weather the ad is skipped or completed. Also started or loaded and or clicked. I am confused with the google's dev docs. I need to get some thing like this,

if (google.ima.AdEvent.Type.COMPLETE){
    console.log("Ad has been completed")
}

If anything like this is there?


Solution

  • The google.ima.AdEvent includes "SKIPPED" and "COMPLETE", among many others (see here for full reference list). You can track each of these adEvent.type using and event listener attached to the adsManager instance (details here) :

    //get ads manager
    adsManager = adsManagerLoadedEvent.getAdsManager(
            videoContent, adsRenderingSettings); 
    
    //add eventlistener
    adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, onAdEvent);
    adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED, onAdEvent);
    adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, onAdEvent);
    adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, onAdEvent);
    
    //adEvent function
    function onAdEvent(adEvent) {
      var ad = adEvent.getAd();
      switch (adEvent.type) {
        case  google.ima.AdEvent.Type.LOADED:
          console.log('loaded');
          break;
        case  google.ima.AdEvent.Type.STARTED:
          console.log('started');
          break;
        case  google.ima.AdEvent.Type.COMPLETE:
          console.log('complete');
          break;
        case  google.ima.AdEvent.Type.SKIPPED:
          console.log('skipped');
          break;
      }
    }
    

    You can download working and detailed samples here (see the "simple" one in particular).