Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Trevor
Visitor

UPDATE v2.0 Google Analytics Page View and Event Tracking

Now updated to 2.0 this version includes:
*NEW* Event tracking now supported
Better use of the global this (m) for tracking variables
Added debug and pretend mode
Download and instructions here: http://bloggingwordpress.com/2012/04/google-analytics-for-roku-developers/

-----------------------------------------------------------------------------------
OLD v1.1 Below - I strongly suggest using the updated v2.0 code above


I've updated this to version 1.1 - It now is easier to setup, I've added language support as requested by jeremyk and it now includes television mode and resolution in the reporting. You can get it the updated version 1.1 here - http://bloggingwordpress.com/2012/04/google-analytics-for-roku-developers/

-----------------------------------------------------------------------------------
OLD v1.0 Code Below - I strongly suggest using the updated v2.0 code above


Thought I'd share my code for how I'm tracking stuff in my channel with Google Analytics. Just wrote this today, it does work, but I haven't done any extensive testing with it. Simple enough to use though, just give it what you'd like to show up under Page in GA, it also supports a User Defined Variable if you want to set that, or leave it empty "". Call it using analyticsPageView("Whatever You'd Like To Register As Page","User Defined Variable")

Replace the ** Google Account ** variable with your own and ** Domain ** too.

REM ****************************************
REM ** Tracking Code for Google Analytics **
REM ****************************************

Function analyticsPageView(var_URI As String, var_uservar As String)

timestamp = CreateObject("roDateTime")
xfer = CreateObject("roURLTransfer")

var_utmac = "XX-XXXXXXXX-X" '** Google Account **
var_utmhn = "roku.whatever.com" '** Domain **
var_utmn = stri(randNumber(1000000000,9999999999)) 'Random Request Number
var_utmn = right(var_utmn,len(var_utmn)-1) 'Remove Space Added By Stri()
var_cookie = stri(randNumber(1000000000,9999999999)) 'Random Cookie Number
var_cookie = right(var_cookie,len(var_cookie)-1) 'Remove Space Added By Stri()
var_random = stri(randNumber(1000000000,2147483647)) 'Random Number Under 2147483647
var_random = right(var_random,len(var_random)-1) 'Remove Space Added By Stri()
var_today = stri(timestamp.asSeconds()) 'Unix Timestamp For Current Date
var_today = right(var_today,len(var_today)-1) 'Remove Space Added By Stri()
var_referer = "Roku" 'Referer Url (Doesn't Exist Here)
if len(var_uservar) = 0 var_uservar = "Roku" 'User Defined Variable
if len(var_URI) = 0 var_URI = "Roku" 'URL For Page View

urchin_url = "http://www.google-analytics.com/__utm.gif?utmwv=1&utmn=" + var_utmn
urchin_url = urchin_url + "&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn=" + var_utmhn
urchin_url = urchin_url + "&utmr=" + var_referer
urchin_url = urchin_url + "&utmp=" + xfer.Escape(var_URI)
urchin_url = urchin_url + "&utmac=" + var_utmac
urchin_url = urchin_url + "&utmcc=__utma%3D" + var_cookie
urchin_url = urchin_url + "." + var_random
urchin_url = urchin_url + "." + var_today
urchin_url = urchin_url + "." + var_today
urchin_url = urchin_url + "." + var_today
urchin_url = urchin_url + ".2%3B%2B__utmb%3D" + var_cookie
urchin_url = urchin_url + "%3B%2B__utmc%3D" + var_cookie
urchin_url = urchin_url + "%3B%2B__utmz%3D" + var_cookie
urchin_url = urchin_url + "." + var_today
urchin_url = urchin_url + ".2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D" + var_cookie
urchin_url = urchin_url + "." + xfer.Escape(var_uservar) + "%3B"

xfer.SetURL(urchin_url)
response = xfer.GetToString()

End Function

Function randNumber(num_min As Integer, num_max As Integer) As Integer
return (RND(0) * (num_max - num_min)) + num_min
End Function
*** Trevor Anderson - bloggingwordpress.com - moviemavericks.com ***
0 Kudos
19 REPLIES 19
destruk
Binge Watcher

Re: Working Google Analytics Page View Tracking Code

Nice work!
0 Kudos
newchannel
Roku Guru

Re: Working Google Analytics Page View Tracking Code

Looks great. Does this go into it's own brs file or where should it be placed? I'm looking to track my channel and banner ad activity once I complete the build.

Thank you.
http://www.victoryNOWfilmsandtv.com
0 Kudos
destruk
Binge Watcher

Re: Working Google Analytics Page View Tracking Code

Since they are functions, they can go in any file. When you package or upload a channel to a roku device, it takes all the brs files and merges them into one file before compiling, so as long as you don't have duplicate function or subroutine names, you'd good no matter what you do.
0 Kudos
Trevor
Visitor

Re: Working Google Analytics Page View Tracking Code

I put it in its own file so it's portable, that way it can just be dropped into any project.
*** Trevor Anderson - bloggingwordpress.com - moviemavericks.com ***
0 Kudos
newchannel
Roku Guru

Re: Working Google Analytics Page View Tracking Code

Thank you both for explaining this. I was not aware of how the brs files were compiled. That explains alot.

Thank you for the coding. I will try it.
http://www.victoryNOWfilmsandtv.com
0 Kudos
jeremyk
Visitor

Re: Working Google Analytics Page View Tracking Code

Thanks for this, I actually had something similar that just wasn't firing. One question though, when I try and run your code (using the video player as a base sample) it won't even compile and I get the following:

*** ERROR compiling /pkg:/source/appVideoScreen.brs:
Syntax Error. (compile error &h02) in ...kg:/source/appVideoScreen.brs(164)
Syntax Error. (compile error &h02) in ...kg:/source/appVideoScreen.brs(165)
Syntax Error. (compile error &h02) in ...kg:/source/appVideoScreen.brs(167)
Syntax Error. (compile error &h02) in ...kg:/source/appVideoScreen.brs(168)
Syntax Error. (compile error &h02) in ...kg:/source/appVideoScreen.brs(169)


Those lines are:

timestamp = CreateObject("roDateTime")
xfer = CreateObject("roURLTransfer")

var_utmac   = "XX-XXXXXXX-XX"                          '** Google Account **
var_utmhn   = "roku.xxx.com"                      '** Domain **
var_utmn    = stri(randNumber(1000000000,9999999999))  'Random Request Number


Seems really odd to me, any ideas what the issue might be??
0 Kudos
Trevor
Visitor

Re: Working Google Analytics Page View Tracking Code

I would firstly put the analytics code in a file all its own, like analytics.brs. Then drop that into the VideoPlayer sample and call the function, it's cleaner that way. I don't see any syntax errors, what's the full appVideoScreen.brs look like?
*** Trevor Anderson - bloggingwordpress.com - moviemavericks.com ***
0 Kudos
jeremyk
Visitor

Re: Working Google Analytics Page View Tracking Code

I will try isolating it into it's own brs file. The code is literally the Videoplayer sample code untouched. Thanks again!
0 Kudos
jeremyk
Visitor

Re: Working Google Analytics Page View Tracking Code

I got it, the issue was it bringing in some weird control characters from the forum and nothing more. Works like a charm. You should also considering adding things like language (utmul variable) and hopefully as they roll into non-English countries they will start exposing this to us explicitly. It just makes it a little prettier when you log into GA.

Thanks again!!
0 Kudos