Issue with network.request

I am using this code in a separate file (ga.lua) to send data to Google Analytics. In the simulator it works fine, but not on device (android, tried several)

In the simulator it works fine, I can even see real time as well. I’ve used this exact same setup on other apps and theese works fine. But now I just can’t get it to work. Tried with different Analytics accounts. Same result.

I carefulle checked if any filenames differs from code (lower/uppercase) since simulator ignoers this and device doesn’t.

ga.lua:

local ga = {}; -- CALLING THE INIT FUNC WILL ATTEMPT TO LOG A GOOGLE ANALYTICS MEASUREMENT PROTOCOL EVENT IN LIEU OF THE PLUGIN -- FOR MORE INFO, SEE https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#apptracking local track = function(pageName) local ga\_tid = "UA-XXXXXXXXX" -- YOUR GA MOBILE APP TID local appName = "IGOTTAreachthebus" -- Name of your game - w/o spaces or special chars local cd = pageName -- argument sent in function call local ga\_cid = system.getInfo("deviceID") local function networkListener(e) if e.isError then print("GA Tracking unsuccessful") else print("Tracking sent to GA...."); print(e.response); end end network.request("http://google-analytics.com/collect?v=1&tid="..ga\_tid.."&cid="..ga\_cid.."&an="..appName.."&t=appview&av=1&cd="..pageName.."","POST",networkListener) end ga.track=track return ga

main.lua:

ga = require( "ga" )

levels.lua:

ga.track("Level: " .. level)

Debug output (CMD)

I/Corona ( 4457): Tracking sent to GA.... I/Corona ( 4457): \<!DOCTYPE html\> I/Corona ( 4457): \<html lang=en\> I/Corona ( 4457): \<meta charset=utf-8\> I/Corona ( 4457): \<meta name=viewport content="initial-scale=1, minimum-scale =1, width=device-width"\> I/Corona ( 4457): \<title\>Error 400 (Bad Request)!!1\</title\> I/Corona ( 4457): \<style\> I/Corona ( 4457): \*{margin:0;padding:0}html,code{font:15px/22px arial,sans- serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-wid th:390px;min-height:180px;padding:30px 0 15px}\* \> body{background:url(//www.goog le.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin: 11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@ media screen and (max-width:772px){body{background:none;margin-top:0;max-width:n one;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo\_sm \_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{backgroun d:url(//www.google.com/images/errors/logo\_sm\_2\_hr.png) no-repeat 0% 0%/100% 100% ;-moz-border-image:url(//www.google.com/images/errors/logo\_sm\_2\_hr.png) 0}}@medi a only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www. google.com/images/errors/logo\_sm\_2\_hr.png) no-repeat;-webkit-background-size:100 % 100%}}#logo{display:inline-block;height:55px;width:150px} I/Corona ( 4457): \</style\> I/Corona ( 4457): \<a href=//www.google.com/\>\<span id=logo aria-label=Google\>\< /span\>\</a\> I/Corona ( 4457): \<p\>\<b\>400.\</b\> \<ins\>ThatÔÇÖs an error.\</ins\> I/Corona ( 4457): \<p\>Your client has issued a malformed or illegal request. \<ins\>ThatÔÇÖs all we know.\</ins\>

I’m guessing the problem is you are not url encoding something. All the parameters need to be url encoded. 

Example:

[lua]local urlOperations = require(“socket.url”)

local urlEncode = urlOperations.escape

local ga_cid = urlEncode(system.getInfo(“deviceID”))[/lua]

Thank you for taking the time to help. I tried this with the same result. In simulator everything works well, but not on device. I inserted your code this way:

local ga = {}; -- CALLING THE INIT FUNC WILL ATTEMPT TO LOG A GOOGLE ANALYTICS MEASUREMENT PROTOCOL EVENT IN LIEU OF THE PLUGIN -- FOR MORE INFO, SEE https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#apptracking local track = function(pageName) local ga\_tid = "UA-XXXXXX-10" -- YOUR GA MOBILE APP TID local appName = "IGOTTAreachthebus" -- Name of your game - w/o spaces or special chars local cd = pageName -- argument sent in function call --local ga\_cid = system.getInfo("deviceID") local urlOperations = require("socket.url") local urlEncode = urlOperations.escape local ga\_cid = urlEncode(system.getInfo("deviceID")) local function networkListener(e) if e.isError then print("GA Tracking unsuccessful") else print("Tracking sent to GA...."); print(e.response); end end network.request("http://google-analytics.com/collect?v=1&tid="..ga\_tid.."&cid="..ga\_cid.."&an="..appName.."&t=appview&av=1&cd="..pageName.."","POST",networkListener) end ga.track=track return ga

I would urlencode the pageName as well. 

But easiest way to debug this is to print out the url and check what it is in the console. Then you can analyze it and even paste it into your browser on your PC and it should work just as it would on your device.

[lua]local url = “http://google-analytics.com/collect?v=1&tid="..ga_tid.."&cid="..ga_cid.."&an="..appName.."&t=appview&av=1&cd="..pageName.."

print(url)

network.request(url,“POST”,networkListener)[/lua]

Also you have POST in your network request but you are not posting anything. That might be the issue. Try GET instead.

I found the problem. It was, as “always” with coding - in the details.

When printing the URL as you suggested, it occured to me that the URL contained a space. (Level: 20) i changed this to “Level20” and now it works fine :slight_smile:

Thank you!

Ok glad you got it working. Spaces are fine btw if you url encode them, looks nicer in Google Analytics :slight_smile:

You can give my module a try if you wish: http://code.coronalabs.com/code/google-analytics-module

To track pages in the same way you are doing you would do:

[lua]local ga = require(“GoogleAnalytics.ga”)

ga.enterScene(pageName)[/lua]

Thanks, I’m gonna take a look at your code. It seems like it could provide more statistics if needed :slight_smile:

I’m guessing the problem is you are not url encoding something. All the parameters need to be url encoded. 

Example:

[lua]local urlOperations = require(“socket.url”)

local urlEncode = urlOperations.escape

local ga_cid = urlEncode(system.getInfo(“deviceID”))[/lua]

Thank you for taking the time to help. I tried this with the same result. In simulator everything works well, but not on device. I inserted your code this way:

local ga = {}; -- CALLING THE INIT FUNC WILL ATTEMPT TO LOG A GOOGLE ANALYTICS MEASUREMENT PROTOCOL EVENT IN LIEU OF THE PLUGIN -- FOR MORE INFO, SEE https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#apptracking local track = function(pageName) local ga\_tid = "UA-XXXXXX-10" -- YOUR GA MOBILE APP TID local appName = "IGOTTAreachthebus" -- Name of your game - w/o spaces or special chars local cd = pageName -- argument sent in function call --local ga\_cid = system.getInfo("deviceID") local urlOperations = require("socket.url") local urlEncode = urlOperations.escape local ga\_cid = urlEncode(system.getInfo("deviceID")) local function networkListener(e) if e.isError then print("GA Tracking unsuccessful") else print("Tracking sent to GA...."); print(e.response); end end network.request("http://google-analytics.com/collect?v=1&tid="..ga\_tid.."&cid="..ga\_cid.."&an="..appName.."&t=appview&av=1&cd="..pageName.."","POST",networkListener) end ga.track=track return ga

I would urlencode the pageName as well. 

But easiest way to debug this is to print out the url and check what it is in the console. Then you can analyze it and even paste it into your browser on your PC and it should work just as it would on your device.

[lua]local url = “http://google-analytics.com/collect?v=1&tid="..ga_tid.."&cid="..ga_cid.."&an="..appName.."&t=appview&av=1&cd="..pageName.."

print(url)

network.request(url,“POST”,networkListener)[/lua]

Also you have POST in your network request but you are not posting anything. That might be the issue. Try GET instead.

I found the problem. It was, as “always” with coding - in the details.

When printing the URL as you suggested, it occured to me that the URL contained a space. (Level: 20) i changed this to “Level20” and now it works fine :slight_smile:

Thank you!

Ok glad you got it working. Spaces are fine btw if you url encode them, looks nicer in Google Analytics :slight_smile:

You can give my module a try if you wish: http://code.coronalabs.com/code/google-analytics-module

To track pages in the same way you are doing you would do:

[lua]local ga = require(“GoogleAnalytics.ga”)

ga.enterScene(pageName)[/lua]

Thanks, I’m gonna take a look at your code. It seems like it could provide more statistics if needed :slight_smile: