Skip to main content

Client Side Ad Insertion

The fl-ads-ima library integrates with GoogleIMA to offer Client Side Ad Insertion capability for both VOD and Live.

GoogleIMA

Setup

GoogleIMA SDK must be loaded.

// Add the script tag for GoogleIMA SDK
<script type="text/javascript" src="//imasdk.googleapis.com/js/sdkloader/ima3.js"></script>;

Create Ad Composable Player

AdComposablePlayer manages both main content player and ad player, providing a seamless main content playback with GoogleIMA ads served as per GoogleIMA Ad response.

Create 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 Ad Player

Create an ad Player using main content Player and GoogleIMAAdRequest. Please note, the created ad player cannot be used standalone, as it just manages GoogleIMA Ad playback alone. The main content playback is not managed by this ad player.​

AdsRenderingSettings

The following parameters that control the rendering of ads.

NameRequiredTypeDescription
autoAlignfalsebooleanSet 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.
bitratefalsenumberMaximum recommended bitrate in kbit/s.
enablePreloadingfalsebooleanEnables preloading of video assets
loadVideoTimeoutfalsenumberTimeout (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.
mimeTypesfalsestring[]Only supported for linear video mime types. If specified, the player will include media that matches the MIME type(s) specified in the list and exclude media that does not match the specified MIME type(s). The format is a list of strings, e.g., [ 'video/mp4', 'video/webm', ... ] If not specified, the SDK will pick the media based on player capabilities.
playAdsAfterTimefalsenumberFor VMAP and ad rules playlists, only play ad breaks scheduled after this time (in seconds).
enableUiElementsfalsebooleanSpecifies whether the UI elements should be displayed or not.
useStyledLinearAdsfalsebooleanRender linear ads with full UI styling
useStyledNonLinearAdsfalsebooleanRender 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();

This player could be used like conventional player from FLPlayer library for playback.

note

GoogleIMA sdk will provide the skipAd button if the ad is skippable.

Listening to Ad Events

The player can subscribe to ad events to track the state of ad playback and implement custom UIs. The AdPlayer emits the following ad events:

Event NameDescription
adbreakstartCalled when an ad break starts
adbreakendCalled when an ad break ends
adstartCalled when playback of an advert starts
adendCalled when playback of an advert ends
adprogressupdateCalled when an advert is playing
adcuepointsCalled when cue points are available
adverttrackingeventCalled when an ad tracking event is signalled
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 ad count down timer if needed
});

player.subscribe("adverttrackingevent", function (adTrackingEvent) {
// signal ad and media events
});

Ad Metadata

AdBreakInfo

Represents an ad break object.

PropertyTypeDescription
adBreakIDstringUnique id of the ab break
contentTimePositionAdContentTimePositionThe position of the ad break, can be on of preroll, midroll or postroll
adBreakStartTimeOffsetMsnumberThe start position in milliseconds
remainingTimeMsnumberThe time remaining in the ad break from the playhead provided, in milliseconds
durationMsnumberThe duration of the ad break, in milliseconds
totalAdsnumberTotal number of adverts in the ad break
adBreakIndexnumberThe index of the current ad break
note

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.

PropertyTypeDescription
adIDstringThe identifier of the advert
sequencenumberThe sequence of the advert
adStartTimeOffsetMsnumberThe start playhead position of the advert in milliseconds
durationMsnumberThe duration of the advert in milliseconds
remainingTimeMsnumberReturns the natural playback time remaining for the advert in milliseconds
isSkippablebooleanIndicates if the ad is skippable
skipOffsetMsnumberThe 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.
isFillerbooleanWhether this advert represents filler content
adBreakInfoAdBreakInfoThe ad break in which the advert belongs in
vastPropertiesVastPropertiesRepresents a VAST properties of advert.
VASTProperties

Represents a VAST properties object.

PropertyTypeDescription
adSystemstringThe source ad server of the advert
AdTitlestringThe common name of the advert
adServingIdstringAn identifier used to compare impression-level data across systems
creativeIdstringCreativeId of the advert
categorystringCategory of the advert content
descriptionstringThe longer description of the advert
advertiserstringAdvertiser name
pricingstringPricing element of the advert
surveystringAn URI to any resource file having to do with collecting survey data
expiresstringNumber of seconds in which the ad is valid for execution
adChoicesIconsArray of AdChoicesIconThe List of AdChoicesIcon instances
AdChoicesIcon

Represents VAST response metadata related to WTA (Why This Ad?) privacy disclosures. These disclosures are returned inside the AdChoices element of the VAST response. WTA related attributes are part of the relevant Icon tag and are used to render the AdChoices icon.

PropertyTypeDescription
programstringThe program represented in the icon (e.g. AdChoices).
widthstringThe pixel width of the icon asset.
heightstringThe pixel height of the icon asset.
xPositionstringThe 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.
yPositionstringThe y-coordinate of the top left corner of the icon asset relative to the ad display area; values of"top" or "bottom" also accepted.
offsetstringThe 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).
durationstringThe 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).
apiFrameworkstringIdentifies the API needed to execute the icon resource file if applicable.
altTextstringThe 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.
imageURLstringThe URL of the image file for the icon (PNG is preferred).
clickThroughURLstringThe 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.
fallbackImagesArray of IconClickFallbackImageThe List of IconClickFallbackImage instances.
IconClickFallbackImage

In cases where the WTA icon can not display the Ad Settings page, such as when the platform does not support a browser, WTA can show users a fallback image that contains the same disclosure information as the Ad Settings page. When an icon click occurs, the ad must pause and the image must be rendered above the video. The player must provide a means for the user to close the dialogue, for example by pressing the back button. The image must not be obstructed and should not be downloaded unless a click-action occurs.

PropertyTypeDescription
widthstringThe pixel width of the image asset.
heightstringThe pixel height of the image asset.
altTextstringThe alternative text for the fallback image.
imageURLstringThe URL of the image file for the fallback image (PNG is preferred).

Events tracked as part of AdTrackingEvent

EventDescription
AD_BREAK_STARTED_EVENTFired when an ad break in a stream starts.
AD_STARTED_EVENTFired when an ad starts playing.
AD_FIRST_QUARTILE_EVENTFired when the ad playhead crosses first quartile.
AD_MIDPOINT_EVENTFired when the ad playhead crosses midpoint.
AD_THIRD_QUARTILE_EVENTFired when the ad playhead crosses third quartile.
AD_PAUSED_EVENTFired when an ad is paused.
AD_RESUMED_EVENTFired when an ad is resumed.
AD_MUTED_EVENTFired when an ad is muted.
AD_UNMUTED_EVENTFired when an ad is unmuted.
AD_ENDED_EVENTFired when an ad completes playing.
AD_BREAK_ENDED_EVENTFired when an ad break in a stream ends.
AD_CLICKFired when an ad is clicked.
AD_VIDEO_CLICKEDFired when a non-clickthrough portion of a video ad is clicked.
AD_IMPRESSIONFires when the impression URL has been pinged.
AD_USER_CLOSEFires when the ad is closed by the user.
AD_SKIPPED_EVENTFired when an ad was skipped.
AD_INTERACTIONFires when an ad triggers the interaction callback. Ad interactions contain an interaction ID string in the ad data.
AD_SKIPPABLE_STATE_CHANGEDFires when the displayed ads skippable state is changed.
AD_VIDEO_ICON_CLICKEDFires when a user clicks a video icon.

CCPA compliance

The CCPA consent string must be sent as us_privacy query param with ad request url. Please check Google IMA SDK Documentation¹ to read further on for CCPA compliance. ​ The below code 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';