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

Google Universal Analytics

I am having trouble with logging events using the Measurement Protocol for Universal Google Analytics. Below is the function in question. I know spaces have to be urlencoded but I cant seem to get xfer.Escape or xfer.urlencode to work on the payload. Any help in the right direction is appreciated. Thanks.

Function UA_trackEvent(EventCat as String , EventAct as String , EventLab as String , EventVal as String) as Void

payload = "v=1"
payload = payload + "&uid=" + m.UATracker.userID
payload = payload + "&cid=" + m.UATracker.clientID
payload = payload + "&tid=" + m.UATracker.AccountID
payload = payload + "&dimension1=" + m.UATracker.model
'payload = payload + "&dimension2=" + m.UATracker.DisplayName
payload = payload + "&sr=" + m.UATracker.display
payload = payload + "&sd=" + m.UATracker.ratio
payload = payload + "&dr=" + config().trafSource

'payload = payload + "&an=" + m.UATracker.appName
'payload = payload + "&av=" + m.UATracker.appVersion

payload = payload + "&t=" + "event"
If Len(EventCat) > 0
payload = payload + "&ec=" + EventCat <-- problem area
end if
If Len(EventAct) > 0
payload = payload + "&ea=" + EventAct
end if
If Len(EventLab) > 0
payload = payload + "&el=" + EventLab <-- problem area
end if
If Len(EventVal) > 0
payload = payload + "&ev=" + EventVal
end if

xfer = CreateObject("roURLTransfer")
xfer.Addheader("x-roku-reserved-dev-id","")
xfer.SetURL(m.UATracker.endpoint + "?" + payload + "&z=" + GetRandomInt(10))
response = xfer.GetToString()

End Function



in the video brs file I am calling

UA_trackEvent("Video Plays", "play", episode.title, "")


the episode.title has spaces in it.
0 Kudos
3 REPLIES 3
RokuChris
Roku Employee
Roku Employee

Re: Google Universal Analytics

"uarlive" wrote:
I cant seem to get xfer.Escape or xfer.urlencode to work on the payload.


If you're escaping the whole payload, you're going to end up encoding characters that are part of the structure of the URL and shouldn't be encoded (?, &, =). Be sure you are only escaping the parameter values.
0 Kudos
uarlive
Visitor

Re: Google Universal Analytics

Thanks.

I tried this as well.

    payload = payload + "&t="  + "event" 
If Len(EventCat) > 0
payload = payload + "&ec=" + xfer.Escape(EventCat)
end if
If Len(EventAct) > 0
payload = payload + "&ea=" + xfer.Escape(EventAct)
end if
If Len(EventLab) > 0
payload = payload + "&el=" + xfer.Escape(EventLab)
end if
If Len(EventVal) > 0
payload = payload + "&ev=" + EventVal
end if



It returned this error.

'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in ...MHenu/pkg:/source/GAStats.brs(95)

095: payload = payload + "&ec=" + (xfer.Escape(EventCat))
Backtrace:
Function ua_trackevent(eventcat As , eventact As , eventlab As , eventval As ) As
file/line: /tmp/plugin/JGAA...MHenu/pkg:/source/GAStats.brs(95)
Function main() As
file/line: /tmp/plugin/JGAAAADMHenu/pkg:/source/appMain.brs(12)
0 Kudos
uarlive
Visitor

Re: Google Universal Analytics

I figured it out.

xfer = CreateObject("roURLTransfer")

Was moved to the top of the function. I was calling it prematurely. Thanks for the help.
0 Kudos