IMA SDKs make it easy to integrate multimedia ads into your websites and apps. IMA SDKs can request ads from any VAST-compliant ad server and manage ad playback in your apps. With IMA DAI SDKs, apps make a stream request for ad and content video—either VOD or live content. The SDK then returns a combined video stream, so that you don't have to manage switching between ad and content video within your app.
Select the DAI solution you're interested in
Full service DAI
This guide demonstrates how to integrate the IMA DAI SDK into a video player app. If you would like to view or follow along with a completed sample integration, download the simple example from GitHub.
IMA DAI overview
Implementing the IMA DAI SDK involves two main components as demonstrated in this guide:
StreamRequest
— either aVODStreamRequest
or aLiveStreamRequest
: An object that defines a stream request. Stream requests can either be for video-on-demand or live streams. Live stream requests specify an asset key, while VOD requests specify a CMS ID and video ID. Both request types can optionally include an API key needed to access specified streams, and a Google Ad Manager network code for the IMA SDK to handle ads identifiers as specified in Google Ad Manager settings.StreamManager
: An object that handles dynamic ad insertion streams and interactions with the DAI backend. The stream manager also handles tracking pings and forwards stream and ad events to the publisher.
Prerequisites
- Three empty files
- dai.html
- dai.css
- dai.js
- Python installed on your computer, or a web server to use for testing
Start a development server
Since the IMA DAI SDK loads dependencies using the same protocol as the page from which it's loaded, you need to use a web server to test your app. A quick way to start a local development server is to use Python's built-in server.
Using a command line, from the directory that contains your
index.html
file run:python -m http.server 8000
In a web browser, go to to
http://localhost:8000/
You can also use any other web server, such as the Apache HTTP Server.
Create a video player
First, modify dai.html to create a HTML5 video element and a div to use for the clickthrough. The following example imports the IMA DAI SDK. For more details, see Import the IMA DAI SDK.
Also, add the necessary tags to load the dai.css
and dai.js
files, as well as to import the hls.js
video player. Then,
modify dai.css
to specify the size and position of the page elements.
Finally, in dai.js
, define variables to hold the stream request
information, an initPlayer()
function to run when the page loads, and set up
the play button to request a stream on click.
To resume playback during paused ad breaks, set up event listeners for the video
element's pause
and start
events to show and hide player controls.
Load the IMA DAI SDK
Next, add the IMA framework using a script tag in dai.html, before the tag for dai.js.
Initialize the StreamManager
In order to request a set of ads, create an ima.dai.api.StreamManager
, which
is responsible for requesting and managing DAI streams. The constructor takes a
video element and an ad UI element to handle ad clicks.
Make a stream request
Define functions to request streams. This example includes functions for
both VOD and livestreams, which create instances of the VODStreamRequest
class and the LiveStreamRequest
class. After you have your streamRequest
instance, call the
streamManager.requestStream()
method with the stream request instance.
Both stream request methods take an optional API key. If you're using a
protected stream, you need to create a DAI authentication key. For more details,
see
Authenticate DAI video stream requests.
Neither stream in this example is protected using a DAI authentication key, so
apiKey
is not used.
Parse stream metadata (Livestream only)
For livestreams, you also need to add a handler to listen to timed metadata
events and forward the events to the StreamManager
class for IMA to emit ad
events during ad breaks:
This guide uses the hls.js
player for stream playback, but your metadata
implementation depends on the type of player you use.
Handle stream events
Implement event listeners for major video events. This example handles the
LOADED
, ERROR
, AD_BREAK_STARTED
and AD_BREAK_ENDED
events by calling
an onStreamEvent()
function. This function handles stream loading, stream
errors, and disabling the player controls during ad playback, which the IMA SDK
requires.
When the stream is loaded, the video player loads and plays the provided URL
using a loadUrl()
function.
That's it! You're now requesting and displaying ads with the IMA DAI SDK. To learn about more advanced SDK features, see the other guides or the samples on GitHub.