Track ads on Android track-ads-on-android
The following instructions provide guidance for implementation using the 2.x SDKs.
Ad tracking constants
MediaHeartbeat.Event.AdBreakStart
MediaHeartbeat.Event.AdBreakComplete
MediaHeartbeat.Event.AdStart
MediaHeartbeat.Event.AdComplete
MediaHeartbeat.Event.AdSkip
Implementation steps
-
Identify when the ad break boundary begins, including pre-roll, and create an
AdBreakObject
by using the ad break information.AdBreakObject
reference:table 0-row-3 1-row-3 2-row-3 3-row-3 3-align-center 7-align-center 11-align-center 15-align-center Variable Name Description Required name
Ad break name such as pre-roll, mid-roll, and post-roll. Yes position
The number position of the ad break within the content, starting with 1. Yes startTime
Playhead value at the start of the ad break. Yes Ad break object creation:
code language-java MediaObject adBreakInfo = MediaHeartbeat.createAdBreakObject(<ADBREAK_NAME>, <POSITION>, <START_TIME>);
-
Call
trackEvent()
withAdBreakStart
in theMediaHeartbeat
instance to begin tracking the ad break:code language-java public void onAdBreakStart(Observable observable, Object data) { _heartbeat.trackEvent(MediaHeartbeat.Event.AdBreakStart, adBreakInfo, null); }
-
Identify when the ad starts and create an
AdObject
instance using the ad information.AdObject
reference:table 0-row-3 1-row-3 2-row-3 3-row-3 4-row-3 3-align-center 7-align-center 11-align-center 15-align-center 19-align-center Variable Name Description Required name
Friendly name of the ad. Yes adId
Unique identifier for the ad. Yes position
The number position of the ad within the ad break, starting with 1. Yes length
Ad length Yes Ad object creation:
code language-java MediaObject adInfo = MediaHeartbeat.createAdObject(<AD_NAME> <AD_ID>, <POSITION>, <LENGTH>);
-
Optionally attach standard and/or ad metadata to the media tracking session through context data variables.
help/use-cases/track-ads/impl-std-ad-metadata/impl-std-ad-metadata-android.md
-
Custom ad metadata - For custom metadata, create a variable object for the custom data variables and populate with the data for the current ad:
code language-java // Setting Ad Metadata HashMap<String, String> adMetadata = new HashMap<String, String>(); adMetadata.put("affiliate", "Sample affiliate"); adMetadata.put("campaign", "Sample ad campaign");
-
-
Call
trackEvent()
with theAdStart
event in theMediaHeartbeat
instance to begin tracking the ad playback.Include a reference to your custom metadata variable (or an empty object) as the third parameter in the event call:
code language-java public void onAdStart(Observable observable, Object data) { _heartbeat.trackEvent(MediaHeartbeat.Event.AdStart, adInfo, adMetadata); }
-
When the ad playback reaches the end of the ad, call
trackEvent()
with theAdComplete
event:code language-java public void onAdComplete(Observable observable, Object data) { _heartbeat.trackEvent(MediaHeartbeat.Event.AdComplete, null, null); }
-
If ad playback did not complete because the user chose to skip the ad, track the
AdSkip
event:code language-java public void onAdSkip(Observable observable, Object data) { _heartbeat.trackEvent(MediaHeartbeat.Event.AdSkip, null, null); }
-
If there are any additional ads within the same
AdBreak
, repeat steps 3 through 7 again. -
When the ad break is complete, use the
AdBreakComplete
event to track:code language-java public void onAdBreakComplete(Observable observable, Object data) { _heartbeat.trackEvent(MediaHeartbeat.Event.AdBreakComplete, null, null); }
See the tracking scenario VOD playback with pre-roll ads for more information.