Client Side Ad Insertion
The @quickplay/ads-ima library integrates with Google IMA to provide client-side ad insertion (CSAI) for both VOD and live content. This page shows how to load the SDK, create the ad player, handle ad events, and prefetch pre-roll ads.
Google IMA
Google IMA serves ads to the player based on the ad response for your ad tag.
Set up the SDK
You must load the Google IMA SDK before you create the ad player.
<!-- Add the script tag for the Google IMA SDK -->
<script
type="text/javascript"
src="//imasdk.googleapis.com/js/sdkloader/ima3.js"
></script>
Create the ad composable player
- Web
- Smart TV
AdComposablePlayer manages both the main content player and the ad player. It provides seamless content playback with Google IMA ads served according to the ad response.
Create the content player
Create a content player using the PlayerBuilder for Player or WebMAF Player.
const contentPlayer = playerBuilder
.mediaElement(videoElement)
.mediaUrl(contentUrl)
.drmLicenseUrl(playbackAsset.licenseUrl)
.drmScheme(playbackAsset.drmScheme)
.build();
Create the ad player
Create the ad player from the main content Player and a GoogleIMAAdRequest. The ad player manages only Google IMA ad playback, so you can't use it standalone. It doesn't manage main content playback.
AdsRenderingSettings
The following parameters control how the player renders ads.
| Name | Required | Type | Description |
|---|---|---|---|
| autoAlign | false | boolean | Set to false if you wish to have fine grained control over the positioning of all non-linear ads. If this value is true, the ad is positioned in the bottom center. If this value is false, the ad is positioned in the top left corner. The default value is true. |
| bitrate | false | number | Maximum recommended bitrate in kbit/s. |
| enablePreloading | false | boolean | Enables preloading of video assets |
| loadVideoTimeout | false | number | Timeout (in milliseconds) when loading a video ad media file. If loading takes longer than this timeout, the ad playback is canceled and the next ad in the pod plays, if available. |
| mimeTypes | false | string[] | Only supported for linear video MIME types. If specified, the player includes only media that matches the listed MIME types, for example ['video/mp4', 'video/webm']. If not specified, the SDK picks media based on player capabilities. |
| playAdsAfterTime | false | number | For VMAP and ad rules playlists, only play ad breaks scheduled after this time (in seconds). |
| enableUiElements | false | boolean | Specifies whether to display the UI elements. |
| useStyledLinearAds | false | boolean | Render linear ads with full UI styling |
| useStyledNonLinearAds | false | boolean | Render non-linear ads with a close and recall button. |
const adTagUrl =
'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&' +
'iu=/124319096/external/ad_rule_samples&ciu_szs=300x250' +
'&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vast&' +
'cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpost&cmsid=496&' +
'vid=short_onecue&correlator=&unviewed_position_start=1';
const adPlayer = flAdsIma
.createPlayerBuilder()
.ima(google.ima)
.adRequest({ adTagUrl: adTagUrl, slotWidth: 628, slotHeight: 300 })
.contentPlayer(contentPlayer)
.contentVideoElement(videoElement) // videoElement on which ad is played.
.contentContainerElement(videoWrapper) // adContainer on which ad UI and settings are rendered.
.adsRenderingSettings({
enableUiElements: true,
useStyledLinearAds: true,
})
.build();
AdComposablePlayer manages both the main content player and the ad player. It provides seamless content playback with Google IMA ads served according to the ad response.
Create the content player
Create a content player using the PlayerBuilder for Player or WebMAF Player.
const playbackOptions = {
enablePreload: true,
segmentPrefetchLimit: 1,
};
const contentPlayer = playerBuilder
.mediaElement(videoElement)
.mediaUrl(contentUrl)
.drmLicenseUrl(playbackAsset.licenseUrl)
.playabackOptions(playbackOptions)
.drmScheme(playbackAsset.drmScheme)
.build();
Create the ad player
Create the ad player from the main content Player and a GoogleIMAAdRequest. The ad player manages only Google IMA ad playback, so you can't use it standalone. It doesn't manage main content playback.
AdsRenderingSettings
The following parameters control how the player renders ads.
| Name | Required | Type | Description |
|---|---|---|---|
| autoAlign | false | boolean | Set to false if you wish to have fine grained control over the positioning of all non-linear ads. If this value is true, the ad is positioned in the bottom center. If this value is false, the ad is positioned in the top left corner. The default value is true. |
| bitrate | false | number | Maximum recommended bitrate in kbit/s. |
| enablePreloading | false | boolean | Enables preloading of video assets |
| loadVideoTimeout | false | number | Timeout (in milliseconds) when loading a video ad media file. If loading takes longer than this timeout, the ad playback is canceled and the next ad in the pod plays, if available. |
| mimeTypes | false | string[] | Only supported for linear video MIME types. If specified, the player includes only media that matches the listed MIME types, for example ['video/mp4', 'video/webm']. If not specified, the SDK picks media based on player capabilities. |
| playAdsAfterTime | false | number | For VMAP and ad rules playlists, only play ad breaks scheduled after this time (in seconds). |
| enableUiElements | false | boolean | Specifies whether the UI elements should be displayed or not. |
| useStyledLinearAds | false | boolean | Render linear ads with full UI styling |
| useStyledNonLinearAds | false | boolean | Render non-linear ads with a close and recall button. |
const adTagUrl =
'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&' +
'iu=/124319096/external/ad_rule_samples&ciu_szs=300x250' +
'&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vast&' +
'cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpost&cmsid=496&' +
'vid=short_onecue&correlator=&unviewed_position_start=1';
const adPlayer = flAdsIma
.createPlayerBuilder()
.ima(google.ima)
.adRequest({ adTagUrl: adTagUrl, slotWidth: 1080, slotHeight: 720 })
.contentPlayer(contentPlayer)
.contentVideoElement(videoElement) // videoElement on which ad is played.
.contentContainerElement(videoWrapper) // adContainer on which ad UI and settings are rendered.
.deferContentUntilAdLoad(true)
.imaSmartTvAdPlaybackConfig({
contentPlaybackDelayMs: 500,
adToContentTransitionFactor: 1,
adCueTransitionDelayMs: 1000,
})
.adsRenderingSettings({
enableUiElements: true,
useStyledLinearAds: true,
enablePreloading: true,
bitrate: 3000, //kbits
})
.build();
ImaCsaiSmartTvPlaybackConfig
imaSmartTvAdPlaybackConfig tunes the hand-off between ads and content on
smart TVs. All fields are optional.
| Name | Type | Default | Description |
|---|---|---|---|
| adEngine | string | 'ima' | The engine that plays the ads: 'ima' (Google IMA SDK) or 'custom' (the SDK's built-in VAST engine). See Use the built-in custom engine on smart TV. |
| contentPlaybackDelayMs | number | 500 | Base delay, in milliseconds, before content resumes after an ad. Gives the decoder time to settle. |
| adToContentTransitionFactor | number | 1 | Multiplier applied to contentPlaybackDelayMs for the resume after an ad. Increase it to give slower devices more time. |
| contentToAdTransitionDelay | number | 0 | Delay, in milliseconds, during the switch from content to an ad. Gives older TVs time to release the video surface. |
| adCueTransitionDelayMs | number | 1000 | Look-ahead window, in milliseconds, for detecting an upcoming mid-roll cue. Google IMA engine only. |
| adCueLateToleranceMs | number | 2000 | Grace period, in milliseconds, for a mid-roll cue the playhead has just passed, so a late timer doesn't skip the cue. |
| maxAdLoadTimeoutMs | number | 15000 | Longest time, in milliseconds, from the start of an ad load to its first frame. A slower ad is dropped with VAST error 402. Custom engine only. |
| disabledAdBreakTypes | string[] | [] | Break types to drop from the ad response, for example ['postroll']. Values are 'preroll', 'midroll', and 'postroll'. Custom engine only. |
The ad player works like a conventional player from the @quickplay/player library, so you can use it for playback.
The Google IMA SDK provides the Skip ad button when the ad is skippable.
Use the built-in custom engine on smart TV
On smart TVs, you can play client-side ads with the SDK's built-in VAST engine
instead of the Google IMA SDK. The VAST engine fetches the ad response, plays
each ad on the content video element, and fires the VAST tracking pixels
itself. It emits the same ad events and AdInfo, and follows the same ad
policy and ad frequency control, so the rest of your app code doesn't change.
Note: This is a preview feature currently under active development.
Enable the custom engine
Set adEngine to 'custom' in imaSmartTvAdPlaybackConfig. The custom engine
doesn't use the Google IMA SDK, so you don't need to load it or call .ima().
const adPlayer = flAdsIma
.createPlayerBuilder()
.adRequest({ adTagUrl: adTagUrl, slotWidth: 1080, slotHeight: 720 })
.contentPlayer(contentPlayer)
.contentVideoElement(videoElement)
.contentContainerElement(videoWrapper)
.deferContentUntilAdLoad(true)
.imaSmartTvAdPlaybackConfig({
adEngine: 'custom',
contentPlaybackDelayMs: 500,
adToContentTransitionFactor: 1,
})
.adsRenderingSettings({
bitrate: 3000, // kbit/s
mimeTypes: ['video/mp4'],
})
.adIncomingLeadTimeMs(10000) // Emit adincoming 10 seconds before a mid-roll
.build();
The player builder chooses the engine as follows:
- The ad engine runs only on smart TVs, that is, when the user agent
identifies Tizen, webOS, or WPE. On other platforms, the builder logs a
warning and uses the Google IMA SDK. If the same code also runs on other
platforms, keep loading the Google IMA SDK and calling
.ima(google.ima)there. - If you set a Google IMA DAI stream manager, the builder ignores
'custom', logs a warning, and uses SSAI. - An unknown
adEnginevalue logs a warning and uses'ima'.
Supported ad responses
The custom engine accepts the following responses for adTagUrl:
| Response | How the custom engine plays it |
|---|---|
| VMAP | Each AdBreak becomes a break at its timeOffset, played from its first AdTagURI. |
| Playlist | Each Preroll, Midroll, and Postroll element becomes a break at its timeOffset, played from its first Ad URL. |
| VAST | A single response plays as one pre-roll. |
The custom engine handles the responses in the following ways:
timeOffsetacceptsstart,end, seconds, andhh:mm:ssormm:sswith optional fractions. A break with any other offset, such as a percentage or a position, is skipped with a warning.- When several breaks share a position, for example two pre-rolls at
start, they all play, one after another, in the order of the response. - The custom engine follows up to five wrapper levels and merges every level's tracking pixels into the final ad. Ad pods play in sequence order.
- The custom engine plays progressive media files that match
mimeTypes, which defaults to['video/mp4']. Withbitrateset, it picks the highest bitrate at or below that value. Without it, it picks the height closest to the screen height. - The custom engine doesn't support inline
VASTAdDatain VMAP, VMAPrepeatAfter, VPAID, SIMID, non-linear ads, or OMID.
Settings the custom engine uses
The custom engine reads the existing builder settings. It doesn't use the Google
IMA UI settings, such as enableUiElements, useStyledLinearAds, and
autoAlign.
| Setting | Use in the custom engine |
|---|---|
adRequest adTagUrl | The ad response: VMAP, Playlist, or VAST. |
adRequest vastLoadTimeout | Timeout for the ad response and for each VAST or wrapper request. Default 8000 ms. |
adsRenderingSettings mimeTypes, bitrate | Choose the media file. See Supported ad responses. |
adsRenderingSettings loadVideoTimeout | Timeout for an ad's media to start playing. Default 10000 ms. |
adsRenderingSettings playAdsAfterTime | Skip breaks scheduled before this time, in seconds. |
imaSmartTvAdPlaybackConfig | Hand-off delays, cue tolerance, maxAdLoadTimeoutMs, and disabledAdBreakTypes. |
playbackPolicy | Seek, continue-watching, and repeat rules. See Continue watching and seeking. |
adIncomingLeadTimeMs | When adincoming starts its countdown before a mid-roll. |
adFrequencyControl | Ad frequency control. See Ad frequency control. |
Continue watching and seeking
The custom engine applies the ad policy you set with
playbackPolicy. If you don't set one, it uses the following default:
| Rule | Default | Effect |
|---|---|---|
autoSeekMode | AUTO_SEEK_SKIP_ALL | On continue watching, no break before the resume point plays. |
fastForwardMode | TRICK_FW_PLAY_LAST | A seek forward past breaks plays the last break passed. |
rewindMode | TRICK_RW_SKIP_ALL | A seek backward doesn't replay breaks. |
repeatMode | REPEAT_PLAY_ONCE | Each break plays once per session. |
interruptMode | INTERRUPT_RESUME | An interrupted ad resumes. |
For continue watching, set initialPlaybackTime in the content player's
playback options. You don't need to set playAdsAfterTime as well. With the
default policy, the pre-roll and every mid-roll before the resume point are
skipped, and they stay skipped for the rest of the session. Mid-rolls after the
resume point play as usual. To play the latest mid-roll before the resume
point instead, set autoSeekMode to AUTO_SEEK_PLAY_MIDROLL.
Build the ad UI
The custom engine doesn't draw any ad UI, so your app draws it from the ad events:
- Show the countdown before a mid-roll from
adincoming. - Show the ad counter, remaining time, and Skip ad button from
adstart,adprogressupdate, andAdInfo.skipAd()works once the ad'sskipOffsetMshas passed. - Handle click-through in your app. The custom engine fires the click tracking pixels but doesn't open the click-through URL.
Errors
The custom engine reports failures with aderror, using VAST error codes. It
never stops content playback because of an ad failure.
| Code | Cause | Result |
|---|---|---|
| 100, 101, 102 | The ad response or a VAST is malformed or unsupported. | The break, or every break, is skipped. |
| 300 | A break has no ad tag URL. | The break is skipped. |
| 301 | A request timed out. | The break is skipped. |
| 302 | The wrapper chain has more than five levels. | The break is skipped. |
| 303 | The response has no breaks, or a VAST has no ads. | Content plays without that break. |
| 401, 403 | No media file, or no media file with a supported MIME type. | The next ad plays. |
| 402 | The ad's media didn't start in time. | The next ad plays. |
| 405 | The ad's media failed during playback. | The next ad plays. |
| 900 | A request failed, or the network dropped. | The break is skipped. |
Lazy content player attach (Advanced CSAI)
Use lazy attach when the content player is not ready when you create the ad player. This happens with SSAI sources such as MediaTailor, where session setup and player build take time. The pre-roll can start while your app prepares the SSAI session in the background. When the content player is ready, attach it to the ad player.
Lazy attach is available for Advanced CSAI only. Other CSAI and SSAI players are unchanged.
Create the ad player without .contentPlayer(...). adTagUrl is still required in adRequest:
const csaiAdPlayer = createPlayerBuilder()
.ima(google.ima)
.adRequest({ adTagUrl: adTagUrl, slotWidth: 628, slotHeight: 300 })
.contentVideoElement(videoElement)
.contentContainerElement(videoWrapper)
.contentPlayerAttachConfig({
timeoutMs: 60000, // optional, default 60000
initialPlaybackTime: 30, // optional, set for continue-watching sessions
autoPlayOnLoad: true, // optional
})
.build();
csaiAdPlayer.play(); // the pre-roll starts now
try {
// create Mediatailor / IMA DAI / Brightcove SSAI player.
const ssaiPlayer = await createSsaiPlayer();
csaiAdPlayer.attachContentPlayer(ssaiPlayer);
} catch (error) {
csaiAdPlayer.failContentPlayerAttach(error);
}
All contentPlayerAttachConfig fields are optional:
| Name | Type | Default | Description |
|---|---|---|---|
| timeoutMs | number | 60000 | Time to wait for attachContentPlayer. If no player attaches in time, the player stops and reports ImaErrorCodes.CONTENT_PLAYER_ATTACH_TIMEOUT. |
| initialPlaybackTime | number | none | Resume position in seconds. Continue-watching sessions skip the pre-roll when this is set before the pre-roll loads. Applied to the attached player. |
| autoPlayOnLoad | boolean | none | Autoplay intent before attach. Applied to the attached player only for settings it doesn't already define. |
Follow these rules when you use late attach:
- Attach once. A second attach, or an attach on a player created with
.contentPlayer(...), is ignored with a warning. - Before attach,
subscribeandunsubscribecalls are queued and replayed in order.setPlaybackOptionsisn't accepted before attach. Other setters, such assetRequestInterceptor,setResponseInterceptor,setPreferredMimeTypes, andvolume, are ignored with a warning. Set them on the content player, or after attach. Pass the resume time and autoplay intent incontentPlayerAttachConfiginstead. - Ad ends before attach. The player reports buffering until the content player attaches, then content resumes.
- Ad fails before attach. The player waits for the attach and hands over as soon as the player arrives.
- Failed attach. If
failContentPlayerAttach(error)runs after the ad ends, buffering is cleared for listeners, so the app doesn't stay in a buffering state. - Attach timeout. If no player attaches within
timeoutMs, the player aborts and later attaches are rejected. The timeout error code isImaErrorCodes.CONTENT_PLAYER_ATTACH_TIMEOUT(0x101003). Match it witherror.doesBelongToCategory(ErrorCodes.ERROR_CATEGORY_MASK_INTERNAL). SettimeoutMsto cover your session-init retry budget plus player build time. - Failure is terminal. After
failContentPlayerAttach(error)or a timeout, create a new ad player to try again.
Skip preroll ads during audio switching
You can skip preroll ads when you resume content or switch audio tracks. Set both of the following values:
initialPlaybackTime: The playback position, in seconds, where content resumes.playAdsAfterTime: The time, in seconds, after which the player plays scheduled ad breaks.
adPlayer.adsRenderingSettings({
playAdsAfterTime: 5, // Set equal to initialPlaybackTime.
});
You must set both values to skip preroll ads. If playAdsAfterTime is 0 or undefined, preroll ads play normally.
With the custom engine on smart TV,
initialPlaybackTime alone skips the pre-roll under the default ad policy. See
Continue watching and seeking.
Listen to ad events
Subscribe to ad events to track ad playback state and build custom UIs. The AdPlayer emits the following events:
| Event Name | Description |
|---|---|
| adbreakstart | Called when an ad break starts |
| adbreakend | Called when an ad break ends |
| adstart | Called when playback of an advert starts |
| adend | Called when playback of an advert ends |
| adprogressupdate | Called when an advert is playing |
| adcuepoints | Called when cue points are available |
| adincoming | Called before a mid-roll, with the countdown |
| aderror | Called when an ad or ad request fails |
| adverttrackingevent | Called when an ad tracking event is signaled |
player.subscribe('adstart', function (adInfo) {
// Update the player controls if needed.
});
player.subscribe('adend', function (adInfo) {
// Update the player controls if needed.
});
player.subscribe('adprogressupdate', function (adInfo, progress) {
// Update the ad countdown timer if needed.
});
player.subscribe('adverttrackingevent', function (adTrackingEvent) {
// Signal ad and media events.
});
Handle ad blockers
Ad blockers can prevent the Google IMA SDK from loading. Before you create the ad player, check that google.ima is available. If it isn't, fall back to the content player.
The custom engine on smart TV doesn't need the Google IMA SDK, so on smart TVs with adEngine set to 'custom', ads still play when an ad blocker stops the Google IMA SDK from loading.
if (typeof google === 'undefined' || typeof google.ima === 'undefined') {
const contentPlayer = playerBuilder
.mediaElement(videoElement)
.mediaUrl(contentUrl)
.drmLicenseUrl(playbackAsset.licenseUrl)
.drmScheme(playbackAsset.drmScheme)
.build();
} else {
const adPlayer = flAdsIma
.createPlayerBuilder()
.ima(google.ima)
.adRequest({ adTagUrl: adTagUrl, slotWidth: 1080, slotHeight: 720 })
.contentPlayer(contentPlayer)
.contentVideoElement(videoElement)
.contentContainerElement(videoWrapper)
.contentPlaybackDelayMs(500)
.deferContentUntilAdLoad(true)
.adsRenderingSettings({
enableUiElements: true,
useStyledLinearAds: true,
})
.build();
// The above values are for reference only. Use environment-specific values.
}
Ad metadata
The following objects describe ads and ad breaks in ad events.
AdBreakInfo
Represents an ad break object.
| Property | Type | Description |
|---|---|---|
| adBreakID | string | Unique id of the ab break |
| contentTimePosition | AdContentTimePosition | The position of the ad break, can be on of preroll, midroll or postroll |
| adBreakStartTimeOffsetMs | number | The start position in milliseconds |
| remainingTimeMs | number | The time remaining in the ad break from the playhead provided, in milliseconds |
| durationMs | number | The duration of the ad break, in milliseconds |
| totalAds | number | Total number of adverts in the ad break |
| adBreakIndex | number | The index of the current ad break |
The totalAds read from AdPlayer may not be accurate when it comes from adstart event. This may change once ad playback starts. GoogleIMA advises to read totalAds on or after FIRST_QUARTILE ad tracking event, to get a reliable data.
AdInfo
Represents an advert object.
| Property | Type | Description |
|---|---|---|
| adID | string | The identifier of the advert |
| sequence | number | The sequence of the advert |
| adStartTimeOffsetMs | number | The start playhead position of the advert in milliseconds |
| durationMs | number | The duration of the advert in milliseconds |
| remainingTimeMs | number | Returns the natural playback time remaining for the advert in milliseconds |
| isSkippable | boolean | Indicates if the ad is skippable |
| skipOffsetMs | number | The value of skip offset for the advert, in milliseconds. If the VAST skip offset is not defined then this method returns -1, signifying that the advert cannot be skipped. |
| isFiller | boolean | Whether this advert represents filler content |
| adBreakInfo | AdBreakInfo | The ad break in which the advert belongs in |
| vastProperties | VastProperties | Represents a VAST properties of advert. |
VASTProperties
Represents the VAST properties of an advert.
| Property | Type | Description |
|---|---|---|
| adSystem | string | The source ad server of the advert |
| AdTitle | string | The common name of the advert |
| adServingId | string | An identifier used to compare impression-level data across systems |
| creativeId | string | CreativeId of the advert |
| category | string | Category of the advert content |
| description | string | The longer description of the advert |
| advertiser | string | Advertiser name |
| pricing | string | Pricing element of the advert |
| survey | string | An URI to any resource file having to do with collecting survey data |
| expires | string | Number of seconds in which the ad is valid for execution |
| adChoicesIcons | Array of AdChoicesIcon | The List of AdChoicesIcon instances |
AdChoicesIcon
Represents VAST response metadata for Why This Ad? (WTA) privacy disclosures.
The VAST response returns these disclosures inside the AdChoices element.
The WTA attributes are part of the relevant Icon tag and render the AdChoices icon.
| Property | Type | Description |
|---|---|---|
| program | string | The program represented in the icon (for example, AdChoices). |
| width | string | The pixel width of the icon asset. |
| height | string | The pixel height of the icon asset. |
| xPosition | string | The x-coordinate of the top, left corner of the icon asset relative to the ad display area. Values of "left" or "right" also accepted and indicate the leftmost or rightmost available position. |
| yPosition | string | The y-coordinate of the top left corner of the icon asset relative to the ad display area; values of"top" or "bottom" also accepted. |
| offset | string | The time of delay from when the associated linear creative begins playing to when the icon should be displayed; provided in the format HH:MM:SS.mmm or HH:MM:SS where .mmm is milliseconds (optional). |
| duration | string | The duration icon should be displayed unless clicked or ad is finished playing; provided in the format HH:MM:SS.mmm or HH:MM:SS where .mmm is milliseconds (optional). |
| apiFramework | string | Identifies the API needed to execute the icon resource file if applicable. |
| altText | string | The alternative text for the image. In an html5 image tag this should be the text for the alt attribute. This should enable screen readers to properly read back a description of the icon for visually impaired users. |
| imageURL | string | The URL of the image file for the icon (PNG is preferred). |
| clickThroughURL | string | The URL of the action that occurs when a user clicks the AdChoices icon. In most cases,the user sees the Ad Settings page that the media player opens when the icon is clicked. |
| fallbackImages | Array of IconClickFallbackImage | The List of IconClickFallbackImage instances. |
IconClickFallbackImage
When the WTA icon can't display the Ad Settings page, such as when the platform doesn't support a browser,
WTA shows a fallback image with the same disclosure information as the Ad Settings page.
When the user clicks the icon, the ad must pause and the player must render the image above the video.
The player must let the user close the dialog, for example by pressing the back button.
Nothing can obstruct the image, and the player must not download it unless the user clicks the icon.
| Property | Type | Description |
|---|---|---|
| width | string | The pixel width of the image asset. |
| height | string | The pixel height of the image asset. |
| altText | string | The alternative text for the fallback image. |
| imageURL | string | The URL of the image file for the fallback image (PNG is preferred). |
Ad tracking events
The adverttrackingevent event reports the following AdTrackingEvent values.
| Event | Description |
|---|---|
| AD_BREAK_STARTED_EVENT | Fired when an ad break in a stream starts. |
| AD_STARTED_EVENT | Fired when an ad starts playing. |
| AD_FIRST_QUARTILE_EVENT | Fired when the ad playhead crosses first quartile. |
| AD_MIDPOINT_EVENT | Fired when the ad playhead crosses midpoint. |
| AD_THIRD_QUARTILE_EVENT | Fired when the ad playhead crosses third quartile. |
| AD_PAUSED_EVENT | Fired when an ad is paused. |
| AD_RESUMED_EVENT | Fired when an ad is resumed. |
| AD_MUTED_EVENT | Fired when an ad is muted. |
| AD_UNMUTED_EVENT | Fired when an ad is unmuted. |
| AD_ENDED_EVENT | Fired when an ad completes playing. |
| AD_BREAK_ENDED_EVENT | Fired when an ad break in a stream ends. |
| AD_CLICK | Fired when an ad is clicked. |
| AD_VIDEO_CLICKED | Fired when a non-clickthrough portion of a video ad is clicked. |
| AD_IMPRESSION | Fires when the impression URL has been pinged. |
| AD_USER_CLOSE | Fires when the ad is closed by the user. |
| AD_SKIPPED_EVENT | Fired when an ad was skipped. |
| AD_INTERACTION | Fires when an ad triggers the interaction callback. Ad interactions contain an interaction ID string in the ad data. |
| AD_SKIPPABLE_STATE_CHANGED | Fires when the displayed ads skippable state is changed. |
| AD_VIDEO_ICON_CLICKED | Fires when a user clicks a video icon. |
CCPA compliance
Send the CCPA consent string in the us_privacy query parameter of the ad request URL. For details, see the Google IMA SDK CCPA documentation.
The following example shows an ad request URL with the IAB parameter 1YNN.
let adTagUrl =
'https://pubads.g.doubleclick.net/gampad/ads?' +
'iu=/124319096/external/single_ad_samples&output=vast&us_privacy=1YNN';
Ad frequency control
To limit how often viewers see ads, see Ad frequency control.
Optimize
Optimizations help your app start playback faster and use fewer resources. This section describes the optimization steps available for client-side ad insertion.
Prefetch pre-roll ads
Pre-roll ad prefetch improves video start time. It proactively requests the targeted pre-roll ad from Google Ad Manager (GAM) and warms up the Google IMA ad break before the user starts playback. When the user selects a video, the player immediately uses the prefetched pre-roll ad, which eliminates initial ad buffering and network latency.
To integrate pre-roll prefetch into your web app, complete the following steps:
- Maintain persistent DOM elements for the video and ad container.
- Initialize
QPAdsLoaderwith your prefetch configuration. - Enable prefetch on the IMA player builder using
.enablePrefetch(true). - Clean up when exiting playback.
Step 1: Maintain persistent DOM elements
Google IMA binds directly to the DOM elements you pass to its container. To reuse the warmed ad instance across page navigations, keep the video element and ad container mounted in the DOM across your catalog and player screens. Don't destroy or recreate these DOM nodes between playback sessions.
In React applications, define the video element and ad container at the root
level of your application (such as App.tsx) using useRef. You can then pass
these element references (videoRef and adContainerRef) to your player
screen:
// Define persistent media elements at the root level
function App() {
const videoRef = useRef<HTMLVideoElement>(null);
const adContainerRef = useRef<HTMLDivElement>(null);
const [selectedAsset, setSelectedAsset] = useState(null);
return (
<div className="app-container">
{/* Persistent media elements defined at root level */}
<div ref={adContainerRef} className="ad-container" />
<video ref={videoRef} playsInline />
<PlayerScreen
asset={selectedAsset}
videoRef={videoRef}
adContainerRef={adContainerRef}
onBack={() => setSelectedAsset(null)}
/>
</div>
);
}
Step 2: Initialize QPAdsLoader
Call QPAdsLoader.initAdsLoader() early in your application lifecycle, such as
when your dashboard or catalog page mounts. This triggers Google IMA to fetch
the pre-roll ad tag in the background:
QPPrefetchConfig options
Configure the prefetch request using the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
imaSDK | any | Yes | The Google IMA SDK instance (window.google.ima). |
adRequest | IMAAdRequest | Yes | Ad request containing the pre-roll VAST or VMAP ad tag URL. |
containerElement | Element | Yes | Persistent DOM container element for ad UI rendering. |
videoElement | HTMLVideoElement | Yes | Persistent HTML video element for playback. |
adsRenderingSettings | AdsRenderingSettings | No | Optional rendering settings such as enableUiElements or useStyledLinearAds. |
imaSdkSettings | ImaSdkSettings | No | Optional Google IMA SDK settings instance used by the prefetch request. |
import { QPAdsLoader } from '@quickplay/ads-ima';
import type { QPPrefetchConfig } from '@quickplay/ads-ima';
const prefetchConfig: QPPrefetchConfig = {
imaSDK: window.google.ima,
imaSdkSettings: new window.google.ima.ImaSdkSettings(),
adRequest: {
// Pre-roll-only ad tag URL used for background prefetching.
adTagUrl: '<PRE_ROLL_ONLY_AD_TAG_URL>',
slotWidth: window.innerWidth || 1920,
slotHeight: window.innerHeight || 1080,
},
adsRenderingSettings: {
enableUiElements: true,
useStyledLinearAds: true,
enablePreloading: true,
},
containerElement: adContainerRef.current, // Persistent ad container DOM element
videoElement: videoRef.current, // Persistent video DOM element
};
QPAdsLoader.initAdsLoader(prefetchConfig);
Step 3: Enable prefetch in the player builder
When the user selects an asset to play, construct your content player and ad
player. Call .enablePrefetch(true) on the IMA player builder so the player
reuses the prefetched pre-roll ad break:
import { createPlayerBuilder as createContentPlayerBuilder } from '@quickplay/player';
import { createPlayerBuilder as createImaPlayerBuilder } from '@quickplay/ads-ima';
import { MediaType } from '@quickplay/player-interface';
// 1. Build content player
const contentPlayer = createContentPlayerBuilder()
.mediaElement(videoRef.current)
.mediaUrl(asset.streamUrl)
.mediaType(MediaType.HLS)
.build();
// 2. Build ad player with enablePrefetch
const adPlayer = createImaPlayerBuilder()
.ima(window.google.ima)
.adRequest({
// Main playback ad tag URL returned by playback authorization.
adTagUrl: '<AD_TAG_URL>',
slotWidth: window.innerWidth || 1920,
slotHeight: window.innerHeight || 1080,
})
.contentPlayer(contentPlayer)
.contentVideoElement(videoRef.current)
.contentContainerElement(adContainerRef.current)
.enablePrefetch(true) // Set to false to prevent consuming the app-level prefetched pre-roll ad.
.build();
// 3. Start playback
adPlayer.load();
The prefetched pre-roll is sized from the prefetch request's slotWidth and slotHeight at page load, before the player is laid out. IMA doesn't follow the video element afterwards, so if you don't call resize, the pre-roll keeps the prefetch size and doesn't fit the video player.
Call resize with the current player size in these cases:
- When the ad break starts.
- On fullscreen change.
- On window resize.
const resizeAdPlayer = () => {
const { clientWidth, clientHeight } = videoRef.current;
adPlayer.resize(clientWidth, clientHeight, document.fullscreenElement != null);
};
adPlayer.subsribe('adbreakstart', () => resizeAdPlayer())
window.addEventListener('resize', resizeAdPlayer);
document.addEventListener('fullscreenchange', resizeAdPlayer);
resize applies to the active ad manager. Remove the listeners when you stop playback.
Prefetching and consumption are two separate steps, and enablePrefetch only
controls the second one.
- Prefetching is handled at the app level. It can happen regardless of the
enablePrefetchsetting. - Consumption is controlled by
enablePrefetch. It decides whether playback uses an already-prefetched pre-roll ad.
enablePrefetch | Prefetched ad available | Behavior |
|---|---|---|
true | Yes | Playback consumes the prefetched pre-roll ad. |
true | No | Playback follows the normal ad request flow. |
false | Yes | The prefetched ad is not consumed, even though it was already prefetched. |
false | No | Playback follows the normal ad request flow. |
Setting enablePrefetch to false does not stop the app from prefetching ads.
It only prevents playback from using them.
If the prefetched pre-roll is user-specific or profile-specific, see Step 4.
Step 4: Clean up when exiting playback
When the user exits the player screen (for example, by selecting Back to
return to the catalog), stop the player instances. Don't call
deinitAdsLoader(), so the shared loader stays ready to prefetch ads for later
videos:
// Stop active player instances
adPlayer.stop();
contentPlayer.stop();
// QPAdsLoader automatically prefetches a fresh pre-roll ad for the next asset.
Call QPAdsLoader.deinitAdsLoader() only when your prefetched ads are targeted
to a specific user or profile, for example when a user logs out or switches
profiles.
This prevents the player from reusing the previous user's ad.
Pre-roll ad prefetch is supported on browser only. It isn't supported on WebTV. Confirm your target platform before you enable this feature.