Track ads using JavaScript 3.x track-ads-on-javascript

The following instructions provide guidance for implementation using the 3.x SDKs.

If you are implementing any previous versions of the SDK, you can download the Developers Guides here: Download SDKs.

Ad tracking constants

Constant name
Constant for tracking AdBreak Start event
Constant for tracking AdBreak Complete event
Constant for tracking Ad Start event
Constant for tracking Ad Complete event
Constant for tracking Ad Skip event

Implementation steps

  1. 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
    Variable Name Type Description
    name string Non empty string denoting adbreak name (pre-roll, mid-roll, and post-roll).
    position number The number position of the ad break starting with 1.
    startTime number Playhead value at the start of the ad break.

    Ad break object creation:

    code language-js
    var adBreakObject =
  2. Call trackEvent() with AdBreakStart in the MediaHeartbeat instance to begin tracking the ad break:

    code language-js
    tracker.trackEvent(ADB.Media.Event.AdBreakStart, adBreakObject);
  3. 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
    Variable Name Type Description
    name string Non empty string denoting ad name.
    adId string Non empty string denoting ad identifier.
    position number The number position of the ad within the adbreak, starting with 1.
    length number Positive number denoting length of the ad.

    Ad object creation:

    code language-js
    var adObject =
  4. (Optional) Attach standard and/or ad metadata to the media tracking session through context data variables.

    • Implement standard ad metadata on JavaScript

    • 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 context data */
      // Standard metadata keys provided by adobe.
      adMetadata[ADB.Media.AdMetadataKeys]  ="Sample Advertiser";
      adMetadata[ADB.Media.AdMetadataKeys] = "Sample Campaign";
      // Custom metadata keys
      adMetadata["affiliate"] = "Sample affiliate";
      adMetadata["campaign"] = "Sample ad campaign";
      adMetadata["creative"] = "Sample creative";
  5. Call trackEvent() with the AdStart event in the MediaHeartbeat 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-js
    _onAdStart = function() {
        tracker.trackEvent(ADB.Media.Event.AdStart, adObject, adMetadata);
  6. When the ad playback reaches the end of the ad, call trackEvent() with the AdComplete event:

    code language-js
    _onAdComplete = function() {
  7. If ad playback did not complete because the user chose to skip the ad, track the AdSkip event:

    code language-js
    _onAdSkip = function() {
  8. If there are any additional ads within the same AdBreak, repeat steps 3 through 7 again.

  9. When the ad break is complete, use the AdBreakComplete event to track:

    code language-js
    _onAdBreakComplete = function() {

See the tracking scenario VOD playback with pre-roll ads for more information.

Granular ad tracking

The default ad ping interval is 10 seconds.

You can set up granular ad tracking to enable 1 second ad tracking.

This information must be provided when starting a tracking session.


ADB.Media.MediaObjectKey = {
   GranularAdTracking: "media.granularadtracking"


var mediaObject = ADB.Media.createMediaObject("media-name", "media-id", 60, ADB.Media.StreamType.VOD, ADB.Media.MediaType.Video);

// Enable granular ad tracking
mediaObject[ADB.Media.MediaObjectKey.GranularAdTracking] = true;