Track ads on iOS
- Topics:
- Media Analytics
CREATED FOR:
- User
- Admin
- Developer
The following instructions provide guidance for implementation using the 2.x SDKs.
Ad tracking constants
Constant name | Description |
---|---|
ADBMediaHeartbeatEventAdBreakStart | Constant for tracking AdBreak Start event |
ADBMediaHeartbeatEventAdBreakComplete | Constant for tracking AdBreak Complete event |
ADBMediaHeartbeatEventAdStart | Constant for tracking Ad Start event |
ADBMediaHeartbeatEventAdComplete | Constant for tracking Ad Complete event |
ADBMediaHeartbeatEventAdSkip | Constant for tracking Ad Skip event |
Implementation steps
-
Identify when the ad break boundary begins, including pre-roll, and create an
AdBreakObject
by using the ad break information.AdBreakObject
reference: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:
id adBreakObject = [ADBMediaHeartbeat createAdBreakObjectWithName:[ADBREAK_NAME] position:[POSITION] startTime:[START_TIME]];
-
Call
trackEvent()
withAdBreakStart
in theMediaHeartbeat
instance to begin tracking the ad break:- (void)onAdBreakStart:(NSNotification *)notification { [_mediaHeartbeat trackEvent:ADBMediaHeartbeatEventAdBreakStart mediaObject:adBreakObject data:nil]; }
-
Identify when the ad starts and create an
AdObject
instance using the ad information.AdObject
reference:Variable NameDescriptionRequiredname
Friendly name of the ad.YesadId
Unique identifier for the ad.Yesposition
The number position of the ad within the ad break, starting with 1.Yeslength
Ad lengthYesAd object creation:
id adObject = [ADBMediaHeartbeat createAdObjectWithName:[AD_NAME] adId:[AD_ID] position:[POSITION] length:[LENGTH]];
-
Optionally attach standard and/or ad metadata to the media tracking session through context data variables.
-
Custom ad metadata - For custom metadata, create a variable object for the custom data variables and populate with the data for the current ad:
NSMutableDictionary *adDictionary = [[NSMutableDictionary alloc] init]; [adDictionary setObject:@"Sample affiliate" forKey:@"affiliate"]; [adDictionary setObject:@"Sample campaign" forKey:@"campaign"]; [adDictionary setObject:@"Sample creative" forKey:@"creative"];
-
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:
- (void)onAdStart:(NSNotification *)notification { [_mediaHeartbeat trackEvent:ADBMediaHeartbeatEventAdStart mediaObject:adObject data:adDictionary]; }
-
When the ad playback reaches the end of the ad, call
trackEvent()
with theAdComplete
event.- (void)onAdComplete:(NSNotification *)notification { [_mediaHeartbeat trackEvent:ADBMediaHeartbeatEventAdComplete mediaObject:nil data:nil]; }
-
If ad playback did not complete because the user chose to skip the ad, track the
AdSkip
event.- (void)onAdSkip:(NSNotification *)notification { [_mediaHeartbeat trackEvent:ADBMediaHeartbeatEventAdSkip mediaObject:nil data:nil]; }
-
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:- (void)onAdBreakComplete:(NSNotification *)notification { [_mediaHeartbeat trackEvent:ADBMediaHeartbeatEventAdBreakComplete mediaObject:nil data:nil]; }
See the tracking scenario VOD playback with pre-roll ads for more information.