A BrightScript interface to Segment.IO event tracking
If you're in the market to add analytics to your Roku application this might be the solution for you.
- Copy Analytics.brs to your source folder.
- Decide how you're going to reference the "user". If they have an actual user ID, or email address, or username and are logged in to a session, then use those. Otherwise
User = createObject("roDeviceInfo").GetDeviceUniqueId()
is a great way to uniquely identify. - Get access to the message port that's being used in your global event loop.
- And in your Session, or somewhere else that's persistant, create an instance of the Analytics object via the following:
Analytics = Analytics("UserIdentifier", "SegmentIOAPIKey", YourEventLoopPort)
The above will set up an initial "Identify" call to Segment.IO in order to start tracking this user.
###Example MessagePort = GetGlobal().MessagePort User = createObject("roDeviceInfo").GetDeviceUniqueId() ApiKey = "ABCD1234"
Analytics = Analytics(User, ApiKey, MessagePort)
'My event loop
while true
msg = wait(0,MessagePort)
'You do stuff with events in your app here
Analytics = GetSession().Analytics
Analytics.HandleAnalyticsEvents(msg)
end while
Utilizing your reference to the Analytics object you initialized above you can make tracking calls using ViewScreen, and AddEvent.
###Screen Views
ViewScreen takes a simple string parameter like so: Analytics.ViewScreen("VideoContentGridScreen")
###Events LogEvent takes in a string with the event that took place and an optional roAssociativeArray of details for your event.
details = CreateObject("roAssociativeArray")
details.buttonName = "Back"
Analytics = GetSession().Analytics
Analytics.AddEvent("Button Pressed", details)
All events (Including the initial Identify at initialization time are queued up and sent as batches to SegmentIO. This is why the timer needs to be ping'ed during the event loop. The events are sent to Segment.IO every 60 seconds as long as your event loop is still active.