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: 
IRoad
Visitor

Google Analytics

Hi,
I am trying this code on my appMAIN.brs, but I am not lucky because google analytics not show up the access. Someone can help me to understand where is the problem. I need to put this code in any brs or I need to have a dedicate brs and how can i do?
Thanks

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 = "UA-39XXXXXX-1" '** Google Account **
var_utmhn = "www.XXXXX.tv" '** 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
0 Kudos
10 REPLIES 10
TheEndless
Channel Surfer

Re: Google Analytics

Make sure your current view filter in the Google Analytics interface includes today. For some reason, it seems to default to yesterday and earlier.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
IRoad
Visitor

Re: Google Analytics

Everything is fine with google analytics, but the question is, this code can I put in any file brs or I need to keep separate it.
0 Kudos
TheEndless
Channel Surfer

Re: Google Analytics

You should be able to put it in any file, but it may be easier to manage in a separate file. You just need to make sure you're calling analyticsPageView() with the appropriate parameters anywhere in your channel that you want a page view tracked.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
IRoad
Visitor

Re: Google Analytics

Can you help me with the function...
0 Kudos
IRoad
Visitor

Re: Google Analytics

something is wrong but what ...

[code:1ahyszeu][/code:1ahyszeu]Function InitGATracker(AccountID As String, HostName As String)
resinfo = CreateObject("roDeviceInfo")
m.GATracker = CreateObject("roAssociativeArray")
m.GATracker.Account = AccountID '(UA-395106XX-1) (utmac)
m.GATracker.Version = "1" 'The version of ga.js (utmwv)
If Len(HostName) > 0 Then
m.GATracker.HostName = HostName 'Domain (utmhn)
Else
m.GATracker.HostName = "roku.iroad.com" 'Domain (utmhn)
End If
m.GATracker.Language = "en-us" 'Language of browser (utmul)
m.GATracker.Resolution = resinfo.GetDisplayMode() + " " + resinfo.GetDisplayType() 'Resolution and TV type (utmsr)
m.GATracker.ColorDepth = "24-bit" 'Color depth of display (utmsc)
m.GATracker.Debug = False 'Set debug printing to false
m.GATracker.Pretend = False 'Set pretend mode to false
End Function

Function GATrackPageView(PageTitle As String, PageURL As String, UserVal As String)
GATrackEvent(PageTitle, PageURL, UserVal, "", "", "", "")
End Function[/code]
0 Kudos
TheEndless
Channel Surfer

Re: Google Analytics

How are you calling the tracking functions from within your code? Unlike a webpage, simply adding the library functions and setting the account and host name isn't sufficient. You need to make explicit calls to the GATrackPageView for every "page view" you want to track.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
IRoad
Visitor

Re: Google Analytics

I am trying to include the google analytics on my channel but I am not lucky... 😞 the GAtracker is inside in the SOURCE... HELPPPPPP
I don't have any response from Google Analytics ....

Function InitGATracker(AccountID As String, HostName As String)
InitGATracker("UA-3651064x-1", "roku.iroad.com")
resinfo = CreateObject("roDeviceInfo")
m.GATracker = CreateObject("roAssociativeArray")
m.GATracker.Account = "UA-3651064x-1" (utmac)
m.GATracker.Version = "1" 'The version of ga.js (utmwv)
If Len(HostName) > 0 Then
m.GATracker.HostName = HostName 'Domain (utmhn)
Else
m.GATracker.HostName = "roku.iroad.com" 'Domain (utmhn)
End If
m.GATracker.Language = "en-us" 'Language of browser (utmul)
m.GATracker.Resolution = resinfo.GetDisplayMode() + " " + resinfo.GetDisplayType() 'Resolution and TV type

(utmsr)
m.GATracker.ColorDepth = "24-bit" 'Color depth of display (utmsc)
m.GATracker.Debug = False 'Set debug printing to false
m.GATracker.Pretend = False 'Set pretend mode to false
End Function

Function GATrackPageView(PageTitle As String, PageURL As String, UserVal As String)
GATrackPageView("CategoryFeed", "/categoryfeed", "")
GATrackEvent(PageTitle, PageURL, UserVal, "", "", "", "")
End Function

Function GATrackEvent(PageTitle As String, PageURL As String, UserVal As String, ECategory As String, EAction As

String, ELabel As String, EValue As String)
GATrackEvent("CategoryFeed", "categoryFeed", "/categoryfeed", "", "", "", "")
If Len(m.GATracker.Account) = 0 Then
If m.GATracker.Debug Then Print "GA Tracker Error: No GA account id specified"
Return 0
End IF
If Len(ECategory) > 0 And Len(EAction) > 0 Then
is_event = True
Else
is_event = False
End If
PA = Len(PageTitle): PB = Len(PageURL)
If PA = 0 And PB > 0 Then PageTitle = PageURL
If PB = 0 And PA > 0 Then PageURL = PageTitle
If PA = 0 And PB = 0 Then
PageTitle = "Roku": PageURL = "Roku"
End If
If Len(UserVal) = 0 Then UserVal = "Roku"

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

var_utmn = GARandNumber(1000000000,9999999999).ToStr() 'Random Request Number
var_cookie = GARandNumber(1000000000,9999999999).ToStr() 'Random Cookie Number
var_random = GARandNumber(1000000000,2147483647).ToStr() 'Random Number Under 2147483647
var_today = timestamp.asSeconds().ToStr() 'Unix Timestamp For Current Date

urchin_url = "http://www.google-analytics.com/__utm.gif?utmwv=1&utmn=" + var_utmn
urchin_url = urchin_url + "&utmsr=" + xfer.Escape(m.GATracker.Resolution)
urchin_url = urchin_url + "&utmsc=24-bit&utmul=" + xfer.Escape(m.GATracker.Language)
urchin_url = urchin_url + "&utmje=0&utmfl=-&utmdt=" + xfer.Escape(PageTitle)
urchin_url = urchin_url + "&utmhn=" + xfer.Escape(m.GATracker.HostName)
urchin_url = urchin_url + "&utmr=-&utmp=" + xfer.Escape(PageURL)
urchin_url = urchin_url + "&utmac=" + m.GATracker.Account
urchin_url = urchin_url + "&utmcc=__utma%3D" + var_cookie
urchin_url = urchin_url + "." + var_random + "." + var_today + "." + var_today + "." + 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(UserVal) + "%3B"
If is_event Then
urchin_url = urchin_url + "&utme=5(" + xfer.Escape(ECategory) + "*" + xfer.Escape(EAction)
If Len(ELabel) > 0 Then urchin_url = urchin_url + "*" + xfer.Escape(ELabel)
If Len(EValue) > 0 Then urchin_url = urchin_url + ")(" + xfer.Escape(EValue)
urchin_url = urchin_url + ")&utmt=event"
End If

If m.GATracker.Pretend Then
response = "In pretend mode"
Else
xfer.SetURL(urchin_url)
response = xfer.GetToString()
End If
If m.GATracker.Debug Then
If is_event Then
Print "GA Tracker: Sent an event with response: " + response
Else
Print "GA Tracker: Sent a page view with response: " + response
End If
Print "GA Tracker: URL - " + urchin_url
End If
End Function

Function GARandNumber(num_min As Integer, num_max As Integer) As Integer
Return (RND(0) * (num_max - num_min)) + num_min
End Function
0 Kudos
TheEndless
Channel Surfer

Re: Google Analytics

That code is very wrong. You're calling the function from within the function, which will cause a stack overflow very quickly. You should not modify the Google Analytics functions at all. Leave them as they were when you got them from the source.
Then, in the existing code in your channel, add the following to the relevant places...

Sub RunUserInterface()
... other existing startup code ...
' Initialize the Google Analytics code when you channel starts
InitGATracker("UA-3651064x-1", "roku.iroad.com")
End Sub

' This function is an example. You may not have a ShowHomeScreen function in your code,
' but the idea is to add the page view code everywhere you want to track something in Google
Sub ShowHomeScreen()
... other existing code ...
GATrackPageView("Homescreen", "/homescreen", "")
End Sub

When adding the GATrackPageView call, replace the "Homescreen" and "/homescreen" values with whatever you want to show up in Google.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
IRoad
Visitor

Re: Google Analytics

I am totally agree with you, the code is wrong, but when I tried to upload the old script I received error (Install Failure: Compilation Failed)

[code]
Function InitGATracker("UA-3651064x-1", "roku.iroad.com")
resinfo = CreateObject("roDeviceInfo")
m.GATracker = CreateObject("roAssociativeArray")
m.GATracker.Account = AccountID 'The Google Account ID(s) (utmac)
m.GATracker.Version = "1" 'The version of ga.js (utmwv)
If Len(HostName) > 0 Then
m.GATracker.HostName = HostName 'Domain (utmhn)
Else
m.GATracker.HostName = "roku.iroad.com" 'Domain (utmhn)
End If
m.GATracker.Language = "en-us" 'Language of browser (utmul)
m.GATracker.Resolution = resinfo.GetDisplayMode() + " " + resinfo.GetDisplayType() 'Resolution and TV type (utmsr)
m.GATracker.ColorDepth = "24-bit" 'Color depth of display (utmsc)
m.GATracker.Debug = False 'Set debug printing to false
m.GATracker.Pretend = False 'Set pretend mode to false
End Function

Function GATrackPageView(PageTitle As String, PageURL As String, UserVal As String)
GATrackPageView("Homescreen", "/appHomeScreen", "")
GATrackEvent(PageTitle, PageURL, UserVal, "", "", "", "")
End Function

Function GATrackEvent(PageTitle As String, PageURL As String, UserVal As String, ECategory As String, EAction As String, ELabel As String, EValue As String)
If Len(m.GATracker.Account) = 0 Then
If m.GATracker.Debug Then Print "GA Tracker Error: No GA account id specified"
Return 0
End IF
If Len(ECategory) > 0 And Len(EAction) > 0 Then
is_event = True
Else
is_event = False
End If
PA = Len(PageTitle): PB = Len(PageURL)
If PA = 0 And PB > 0 Then PageTitle = PageURL
If PB = 0 And PA > 0 Then PageURL = PageTitle
If PA = 0 And PB = 0 Then
PageTitle = "Roku": PageURL = "Roku"
End If
If Len(UserVal) = 0 Then UserVal = "Roku"

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

var_utmn = GARandNumber(1000000000,9999999999).ToStr() 'Random Request Number
var_cookie = GARandNumber(1000000000,9999999999).ToStr() 'Random Cookie Number
var_random = GARandNumber(1000000000,2147483647).ToStr() 'Random Number Under 2147483647
var_today = timestamp.asSeconds().ToStr() 'Unix Timestamp For Current Date

urchin_url = "http://www.google-analytics.com/__utm.gif?utmwv=1&utmn=" + var_utmn
urchin_url = urchin_url + "&utmsr=" + xfer.Escape(m.GATracker.Resolution)
urchin_url = urchin_url + "&utmsc=24-bit&utmul=" + xfer.Escape(m.GATracker.Language)
urchin_url = urchin_url + "&utmje=0&utmfl=-&utmdt=" + xfer.Escape(PageTitle)
urchin_url = urchin_url + "&utmhn=" + xfer.Escape(m.GATracker.HostName)
urchin_url = urchin_url + "&utmr=-&utmp=" + xfer.Escape(PageURL)
urchin_url = urchin_url + "&utmac=" + m.GATracker.Account
urchin_url = urchin_url + "&utmcc=__utma%3D" + var_cookie
urchin_url = urchin_url + "." + var_random + "." + var_today + "." + var_today + "." + 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(UserVal) + "%3B"
If is_event Then
urchin_url = urchin_url + "&utme=5(" + xfer.Escape(ECategory) + "*" + xfer.Escape(EAction)
If Len(ELabel) > 0 Then urchin_url = urchin_url + "*" + xfer.Escape(ELabel)
If Len(EValue) > 0 Then urchin_url = urchin_url + ")(" + xfer.Escape(EValue)
urchin_url = urchin_url + ")&utmt=event"
End If

If m.GATracker.Pretend Then
response = "In pretend mode"
Else
xfer.SetURL(urchin_url)
response = xfer.GetToString()
End If
If m.GATracker.Debug Then
If is_event Then
Print "GA Tracker: Sent an event with response: " + response
Else
Print "GA Tracker: Sent a page view with response: " + response
End If
Print "GA Tracker: URL - " + urchin_url
End If
End Function

Function GARandNumber(num_min As Integer, num_max As Integer) As Integer
Return (RND(0) * (num_max - num_min)) + num_min
End Function]
[/cod
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.