---
title: Ad tracking for Roku applications
source: https://docs.newrelic.com/docs/streaming-video-&-ads/installation/roku/ad-tracking
---

The Roku Video Agent provides ad event monitoring through two different Ad APIs:

-   [Roku Advertising Framework (RAF)](https://developer.roku.com/en-gb/docs/developer-program/advertising/roku-advertising-framework.md)
-   [Google IMA](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/roku)

## Prerequisites

Before enabling ad tracking:

-   Complete [Roku agent installation](https://docs.newrelic.com/docs/streaming-video-&-ads/installation/roku/get-started-with-roku)
-   Initialize the New Relic Agent (referenced as `m.nr` in examples below)

## Roku Advertising Framework (RAF)

RAF is integrated within the NRAgent. No additional files are required.

### Enable RAF tracking

Pass the NRAgent object (created with `NewRelic("ACCOUNT ID", "LICENSE KEY")`) to the Ads task using a field. Inside the Ads task, perform the following:

```brightscript
adIface = Roku_Ads()

' Ad Iface setup code...

logFunc = Function(obj = Invalid as Dynamic, evtType = invalid as Dynamic, ctx = invalid as Dynamic)
    'Call RAF tracker, passing the event and context
    nrTrackRAF(obj, evtType, ctx)
End Function

' m.top.nr is the reference to the field where we have the NRAgent object
adIface.setTrackingCallback(logFunc, m.top.nr)
```

For a complete usage example, check out the files `VideoScene.brs` (function `setupVideoWithAds()`) and `AdsTask.brs` in the Roku agent package.

## Google IMA

### Copy required files

Ensure the following files are included in your project directory:

```
components/NewRelicAgent/trackers
    IMATracker.brs
    IMATracker.xml
source/
    IMATrackerInterface.brs
```

### Create IMA tracker

Create the IMA Tracker object:

```brightscript
tracker = IMATracker(m.nr)
```

Where `m.nr` is the NRAgent object.

### Set up ad tracking

Pass the tracker object to the IMA SDK Task using a field and include the script `IMATrackerInterface.brs` in the task XML. Inside the task, set up the following tracking:

**Track ad breaks:**

```brightscript
m.player.adBreakStarted = Function(adBreakInfo as Object)
    ' Ad break start code...

    ' Send AD_BREAK_START
    nrSendIMAAdBreakStart(m.top.tracker, adBreakInfo)
End Function

m.player.adBreakEnded = Function(adBreakInfo as Object)
    ' Ad break end code...

    ' Send AD_BREAK_END
    nrSendIMAAdBreakEnd(m.top.tracker, adBreakInfo)
End Function
```

**Track ad events:**

```brightscript
m.streamManager.addEventListener(m.sdk.AdEvent.START, startCallback)
m.streamManager.addEventListener(m.sdk.AdEvent.FIRST_QUARTILE, firstQuartileCallback)
m.streamManager.addEventListener(m.sdk.AdEvent.MIDPOINT, midpointCallback)
m.streamManager.addEventListener(m.sdk.AdEvent.THIRD_QUARTILE, thirdQuartileCallback)
m.streamManager.addEventListener(m.sdk.AdEvent.COMPLETE, completeCallback)

Function startCallback(ad as Object) as Void
    ' Send AD_START
    nrSendIMAAdStart(m.top.tracker, ad)
End Function

Function firstQuartileCallback(ad as Object) as Void
    ' Send AD_QUARTILE (first)
    nrSendIMAAdFirstQuartile(m.top.tracker, ad)
End Function

Function midpointCallback(ad as Object) as Void
    ' Send AD_QUARTILE (midpoint)
    nrSendIMAAdMidpoint(m.top.tracker, ad)
End Function

Function thirdQuartileCallback(ad as Object) as Void
    ' Send AD_QUARTILE (third)
    nrSendIMAAdThirdQuartile(m.top.tracker, ad)
End Function

Function completeCallback(ad as Object) as Void
    ' Send AD_END
    nrSendIMAAdEnd(m.top.tracker, ad)
End Function
```

Where `m.top.tracker` is the tracker object passed to the task.

For a complete usage example, check out the files `VideoScene.brs` (function `setupVideoWithIMA()`) and `imasdk.brs` in the Roku agent package.
