A VAST tag is an ad script generated by ad servers that allows ad servers to communicate with video players. More specifically, a VAST tag is a piece of code — an XML markup schema — placed on a publisher's website to display ads.
A VAST ad tag URL is used by a player to retrieve video and audio ads. You can manually assemble a VAST ad tag URL or modify an existing URL using the parameters below, and then test with the
VAST ad tags support both http and https. Impression tracking URLs respect the current SSL setting and return as either http or https. Click-through and click-tracking use http since they redirect from another page.
drop in the sample ad tags from above to see the ad showing up on the test video player
how to create a Player with VAST tag capability:
First, modify index.html to create a simple HTML5 video element, contained in a wrapping element, and a button to trigger playback. Also add the necessary tags to load the style.css and ads.js files. Then, modify styles.css to make the video player responsive for mobile devices. Finally, in ads.js, trigger video playback when the play button is clicked.
After completing this step, when you open index.html in your browser (via your development server) you should be able to see the video element and the video should start when you click the play button.
3. Import the IMA SDK
Next, add the IMA framework using a script tag in index.html, before the tag for ads.js.
// Prevent this function from running on if there are already ads loaded
if(adsLoaded) {
return;
}
adsLoaded = true;
// Prevent triggering immediate playback when ads are loading
event.preventDefault();
console.log("loading ads");
}
5. Create the ad container
In most browsers, the IMA SDK uses a dedicated ad container element for displaying both ads and ad-related UI elements. This container must be sized to overlay the video element from the top-left corner. The height and width of the ads placed in this container are set by the adsManager object, so you don't need to set these values manually.
Note: On desktop environments, in order to prevent accidental clicks, the IMA SDK will not open the click-through URL in the first 2 seconds of the ad playback.
To implement this ad container element, first create a new div within the video-container element. Then, update the CSS to position the element at the top-left corner of the video-element. Finally, define a variable for the container within the initializeIMA() function that runs when the page is loaded.
6. Initialize the AdsLoader and make an ads request
In order to request a set of ads, create an ima.AdsLoader instance. This instance takes an AdDisplayContainer object as an input and can be used to process ima.AdsRequest objects associated with a specified ad tag URL. The ad tag used in this example contains a 10-second pre-roll ad. You can test this, or any, ad tag URL using the
As a best practice, only maintain one instance of ima.AdsLoader for the entire lifecycle of a page. To make additional ad requests, create a new ima.AdsRequest object, but re-use the same ima.AdsLoader. For more information, see the
// Pass the request to the adsLoader to request ads
adsLoader.requestAds(adsRequest);
}
7. Listen for AdsLoader events
When ads are loaded successfully, the ima.AdsLoader emits an ADS_MANAGER_LOADED event. Parse the event passed to the callback to initialize the AdsManager object. The AdsManager loads the individual ads as defined by the response to the ad tag URL.
In addition, be sure to handle any errors that may occur during the loading process. If ads do not load, make sure that media playback continues, without ads, so as to not interfere with the user's experience.