Track ads using JavaScript 2.x track-ads-on-javascript
The following instructions provide guidance for implementation using the 2.x SDKs.
Ad tracking constants
AdBreakStartAdBreakCompleteAdStartAdCompleteAdSkipImplementation steps
-
Identify when the ad break boundary begins, including pre-roll, and create an
AdBreakObjectby using the ad break information.AdBreakObjectreference: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 nameAd break name such as pre-roll, mid-roll, and post-roll. Yes positionThe number position of the ad break starting with 1. Yes startTimePlayhead value at the start of the ad break. Yes Ad break object creation:
code language-js var adBreakObject = MediaHeartbeat.createAdBreakObject(<ADBREAK_NAME>, <POSITION>, <START_TIME>); -
Call
trackEvent()withAdBreakStartin theMediaHeartbeatinstance to begin tracking the ad break:code language-js mediaHeartbeat.trackEvent(MediaHeartbeat.Event.AdBreakStart, adBreakObject); -
Identify when the ad starts and create an
AdObjectinstance using the ad information.AdObjectreference: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 nameFriendly name of the ad. Yes adIdUnique identifier for the ad. Yes positionThe number position of the ad within the ad break, starting with 1. Yes lengthAd length Yes Ad object creation:
code language-js var adObject = MediaHeartbeat.createAdObject(<AD_NAME>, <AD_ID>, <POSITION>, <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:
code language-js /* Set custom context data */ var adCustomMetadata = { affiliate: "Sample affiliate", campaign: "Sample ad campaign", creative: "Sample creative" };
-
Call
trackEvent()with theAdStartevent in theMediaHeartbeatinstance 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-js _onAdStart = function() { this._mediaHeartbeat.trackEvent(MediaHeartbeat.Event.AdStart, adObject, adCustomMetadata); }; -
When the ad playback reaches the end of the ad, call
trackEvent()with theAdCompleteevent:code language-js _onAdComplete = function() { this._mediaHeartbeat.trackEvent(MediaHeartbeat.Event.AdComplete); }; -
If ad playback did not complete because the user chose to skip the ad, track the
AdSkipevent:code language-js _onAdSkip = function() { this._mediaHeartbeat.trackEvent(MediaHeartbeat.Event.AdSkip); }; -
If there are any additional ads within the same
AdBreak, repeat steps 3 through 7 again. -
When the ad break is complete, use the
AdBreakCompleteevent to track:code language-js _onAdBreakComplete = function() { this._mediaHeartbeat.trackEvent(MediaHeartbeat.Event.AdBreakComplete); };
See the tracking scenario VOD playback with pre-roll ads for more information.