The Google Interactive Media Ads (IMA) SDKs enable publishers to display linear, non-linear, and companion ads in videos and games.
IMA SDKs provide a set of APIs to developers to:
- Make video ad requests to DoubleClick for Publishers (DFP), the Google AdSense network, or any VAST-compliant ad server.
- Interpret the ad responses.
- Report metrics to the ad servers.
The IMA SDKs Quick Start guides explain how to use the SDKs to
display interactive media ads, such as pre-rolls and overlays, within video
content players. You need to know:
- ActionScript 3 to use Flash SDK.
- JavaScript to use HTML5 SDK.
- Objective-C to use iOS SDK.
- Java to use Android SDK.
Who use IMA SDKs?
- DFP users
Use IMA SDKs to serve video ads.
- AdSense for Video (AFV) ads and AdSense for Games (AFG) ads
Use IMA SDKs.
- Publishers using VAST ads from a third-party ad server
Book their VAST ads in DFP and use the IMA SDKs to request and display them.
- AdExchange for Video
Uses IMA SDKs.
Android Quick Start (Beta)
To get started, read:Note: For a working example, refer to the sample application. All code on this page is excerpted from the example.A note about the SDK architecture
The Android SDK uses a WebView to load a JavaScript backend. This WebView is only used for making ad requests, parsing responses, and pinging tracking URLs. All video rendering is done natively in Android using your content video player.
Dowloading, building, and running the sample application
Note: Before building and running the sample application with Eclipse, you must download and reference the IMA Android SDK- Obtain a copy of the sample application code from the downloads page.
- Create the Demo App V3 project in Eclipse.
- Start Eclipse.
- Click File > New > Project.
- Click Android.
- Select Android Project from Existing Code.
- Click Next.
- At Root Directory, enter the top level directory of the sample application.
- Click Finish.
For example,/home/{username}/googleads-ima-android-master/
. - Download the IMA Android SDK jar file from the
downloads page
and add it to the
libs/
directory in Eclipse. - Include Google Play Services in your application following these instructions.
- Build and run the sample application.
- Right-click Demo App V3 > Run as > Android application.
- Click Req. ad.
Your first ads request
To request ads from any VAST-compliant ad server:
- Create an instance of AdsLoader to load your ads
TheImaSdkFactory
class is the entry point to the IMA SDK. It's a singleton, so even if you don't keep a reference to the instance, it will be the same instance every time. The first thing you should create is anAdsLoader
, which will allow you to request ads.
ImaSdkFactory sdkFactory; @Override public void onCreate(Bundle savedInstanceState) { ... sdkFactory = ImaSdkFactory.getInstance(); adsLoader = sdkFactory.createAdsLoader(this); ... }
- Add event listeners
Register listeners for ad load success and failure events.
@Override public void onCreate(Bundle savedInstanceState) { ... adsLoader.addAdErrorListener(this); adsLoader.addAdsLoadedListener(this); ... } @Override public void onAdError(AdErrorEvent event) { // An error occurred. } @Override public void onAdsManagerLoaded(AdsManagerLoadedEvent event) { // Ads were successfully loaded }
- Create AdDisplayContainer to display your ads
TheAdDisplayContainer
houses the following objects:VideoAdPlayer
for linear ads,ViewGroup
for non-linear ads andCollection<CompanionAdSlot>
for companion ads.
Note: Since non-linear ads have not yet been implemented, theViewGroup
is not currently used or required.Note: To play video ads, you will first need a compatibleVideoAdPlayer
implementation. Refer to this tutorial for instructions on how to build a compatible player.protected void requestAd() { container = sdkFactory.createAdDisplayContainer(); container.setPlayer(videoPlayer); ... }
If your application has slots for companion ads, add a collection ofCompanionAdSlot
objects to the container. When you display the master ad, the ViewGroups you specify will automatically be cleared, then filled with Views to display the companion ads. Currently, only image companions (JPEG, PNG, non-animated GIF) are supported.
Note: Companions will remain visible after the master is finished playing. If you want them to be hidden, you are responsible for clearing the ViewGroups yourself.protected void requestAd() { ... CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot(); companionAdSlot.setContainer(companionView); companionAdSlot.setSize(300, 50); Collection<CompanionAdSlot> companionAdSlots = new ArrayList<CompanionAdSlot>(); companionAdSlots.add(companionAdSlot); container.setCompanionSlots(companionAdSlots); ... }
- Request ads from the server
Call therequestAds
method on theAdsLoader
to request an ad. After you request ads, the SDK will call your listeners to inform you of the result.
protected void requestAd() { ... AdsRequest request = sdkFactory.createAdsRequest(); request.setAdTagUrl(TAG_URL); request.setAdDisplayContainer(container); adsLoader.requestAds(request); }
Displaying ads
After a successful ad request, the SDK will provide you with an ads manager you can use to display ads. Follow these steps to display your ads:
- Get the ads manager
Once the ad loads successfully, theonAdsManagerLoaded
method of yourAdsLoadedListener
is called. Get an ads manager from the event you received by callingAdsManagerLoadedEvent.getAdsManager
and use that instance to display ads and add event listeners for ad playback events and errors.
@Override public void onAdsManagerLoaded(AdsManagerLoadedEvent event) { adsManager = event.getAdsManager(); adsManager.addAdErrorListener(this); adsManager.addAdEventListener(this); }
- Handle Ad Events
The SDK will send various events to your listener, three of which you need to handle.
- When an ad has been loaded,
AdEventType.LOADED
will be fired indicating that ads are ready to be played. AdEventType.CONTENT_PAUSE_REQUESTED
will be fired immediately before a video ad is played to indicate that you should pause your video content.- After the ad finishes playing, or you explicitly unload it,
AdEventType.CONTENT_RESUME_REQUESTED
will be fired to indicate that the content should begin playing again.
AdEventType.MIDPOINT
andAdEventType.COMPLETE
as well asAdEventType.CLICK
which occurs when a user clicks on the ad. The SDK will handle opening a browser with the ad's clickthrough URL, but you may want to unload the ad or otherwise respond to the interaction yourself. For the list of all event types seeAdEvent.AdEventType
.
@Override public void onAdEvent(AdEvent event) { switch (event.getType()) { case LOADED: adsManager.start(); break; case CONTENT_PAUSE_REQUESTED: videoPlayer.pauseContent(); break; case CONTENT_RESUME_REQUESTED: videoPlayer.resumeContent(); break; } }
- When an ad has been loaded,
- Initialize the ads manager
Before ads can be played, the ads manager must be initialized by callingAdsManager.init
. If an ad rules playlist is returned as the ad, the SDK will start executing the playlist. If there are pre-rolls scheduled within the ad rules playlist, they will start at this time.
@Override public void onAdsManagerLoaded(AdsManagerLoadedEvent event) { adsManager = event.getAdsManager(); adsManager.addAdErrorListener(this); adsManager.addAdEventListener(this); adsManager.init(); }
- Start the ads
CallAdsManager.start
to start ad playback. Video and overlay ads will start at this time. The ad rules playlist will ignore this call.AdsManager.start
can be called any time after theAdEventType.LOADED
event has been received.
No comments:
Post a Comment