User Agent and Google Analytics

i am having tracking id which generally starts with UA. don’t know about the CID.

so i have done for CID  which is mentioned below.

local ga_cid = tostring(math.random(10000000, 99999999))

local function networkListener(e)

    

    if e.isError then

        print(“Couldn’t track your application.”)

    else

        

        

        print(“Tracking is going on in GA…”);

        print(e.response);

        

        GA=json.decode(e.response);

        

–[[        for l=1,#GA do

        

            print(“GA…&tid…”…GA[l].tid);

        

        end]]

        

    end

end

network.request(“http://google-analytics.com/collect?v=1&tid=”…ga_utmac…"&cid="…ga_cid…"&t=pageview&dp=%2Fapp%2Fhome&dt=Home&ul=en_US",“POST”,networkListener)

Please tell me it is right or not.

Thanks for your quick reply fosforus

I hope to be able to get deeper into this over the next week.

It looks theres some more info about the CID here:

https://developers.google.com/games/services/console/enabling#step_2_add_your_game_to_the_dev_console

scroll down to “Step 3. Generate an OAuth 2.0 client ID”

Hi fosforus

Thanks for your quick reply. I am still confused means we need to use CID of Google Play Developer Console,…???

Or it is different for Google Analytics…???

I am really new to mobile app development. I don’t get it really.

Thanks once again for your quick response.

The Google Play Developer Console is different from Google Analytics…it is the Android equivalent to the iOS Developer Member Center.

https://play.google.com/apps/publish/signup/

Alright fosforus

I will sign up for the  Google Play Developer Console. and will take the CID of Google Play Developer Console and rest of the code will remain the same. Am i right sir…???

Thanks for your Help…!! Sir 
 

I wish I could tell you for sure… I havent done the CID part yet. We’re trailblazing here.

The CID is a unique identifier for each device that uses your app, not your Google Play app number (if there is such a thing).  Google Analytics will use the CID to track the users returning visits and other user engagement metrics.

OK, so it’s like the UID (on an iOS device)?

It looks like we can use system.getInfo( “deviceID” )  to acquire a unique CID for devices. 

I’m testing now and will know tomorrow whether or not the events are going to show up on the dashboard. 

OK, It looks like it is working! Below is the code I used, and you can use the “Real Time” option in Google Analytics dashboard to see the events happening… in “real time” :slight_smile:

First you will need to add a file called ga.lua to your project, and the contents of the file should be as follows:

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-XXXXX-XX" -- YOUR GA MOBILE APP TID local appName = "myGame" -- 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

Then, at any page or event you wish to track, you include the following at the top of the file:

module(..., package.seeall) local ga = require( "ga" )

Then at the point of the event send the call to the “track” function with an argument describing the page:

-- GOOGLE ANALYTICS ga.track("launchScreen")

This is using the “page view” tracking. I was not able to get the “event” tracking to work properly at this time. Also I’m sure there is a better way to handle the ga.lua object or class…maybe someone can refine it?

Note: In the network request, I used av=1, but you can use whatever version number is relevant to your app.

Hello fosforus sir,

the code is working fine.

Actually i was also getting the same response which i am getting currently.

Difference is just CID. I was not generating CID.

Thanks alot SIr!!!

Did anyone ever manage to log the device model name with Google Analytics? Everything else seems to work wonderfully.

So you can over ride the user agent with

&ua=some\_url\_encoded\_user\_agent\_string

and I tried that with a iOS safari user agent and the device info showed up perfectly in Analytics.

Problem is we can’t fetch the user agent with Corona.

If we use webview in Corona and go to www.whatsmyuseragent.com we get the correct user agent. I haven’t used webview much, is there any way to transfer information from the webview to be used in an analytics module? If so a rest api would be easy to build.

One hacky option would be to have 2 different strings, one for iPhone and another for iPad and use them depending on the device. Then ignore the OS version and other device information in the analytics since mobile/tablet is probably the most important.

Of course it would be possible to build a more advanced string to include all the device info but its kind of volatile since Google Analytics does not support custom user agents and it would have to be quite exact presumably.

One workaround to get User Agent is to grab it from a webview. See code below. I made a Google Analytics module using this functionality: http://code.coronalabs.com/code/google-analytics-module

local HTML file called ua.html

\<script\> &nbsp; &nbsp; window.onload = function() { &nbsp; &nbsp; &nbsp; &nbsp; userAgent = encodeURIComponent(navigator.userAgent) &nbsp; &nbsp; &nbsp; &nbsp; alreadyLoaded = document.URL.indexOf("userAgent") \> -1 &nbsp; &nbsp; &nbsp; &nbsp; if (!alreadyLoaded) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.location.assign(document.URL+"?userAgent="+userAgent) &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } \</script\>

local userAgent local uaWebView &nbsp; local function uaListener(event) &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local findStr = "userAgent=" &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; if not userAgent and event and event.url and string.find(event.url, findStr) then &nbsp; &nbsp; &nbsp; &nbsp; userAgent = string.sub(event.url, (event.url:match('^.\*()'..findStr) or 0) + string.len(findStr)) &nbsp; &nbsp; &nbsp; &nbsp; uaWebView:removeSelf() &nbsp; &nbsp; &nbsp; &nbsp; print("User agent is", userAgent) &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; end &nbsp; uaWebView = native.newWebView(0, -display.contentHeight, 5, 5 ) -- Position webview off screen uaWebView:request("ua.html", system.ResourceDirectory) uaWebView:addEventListener("urlRequest", uaListener) &nbsp;

Did anyone ever manage to log the device model name with Google Analytics? Everything else seems to work wonderfully.

So you can over ride the user agent with

&ua=some\_url\_encoded\_user\_agent\_string

and I tried that with a iOS safari user agent and the device info showed up perfectly in Analytics.

Problem is we can’t fetch the user agent with Corona.

If we use webview in Corona and go to www.whatsmyuseragent.com we get the correct user agent. I haven’t used webview much, is there any way to transfer information from the webview to be used in an analytics module? If so a rest api would be easy to build.

One hacky option would be to have 2 different strings, one for iPhone and another for iPad and use them depending on the device. Then ignore the OS version and other device information in the analytics since mobile/tablet is probably the most important.

Of course it would be possible to build a more advanced string to include all the device info but its kind of volatile since Google Analytics does not support custom user agents and it would have to be quite exact presumably.

One workaround to get User Agent is to grab it from a webview. See code below. I made a Google Analytics module using this functionality: http://code.coronalabs.com/code/google-analytics-module

local HTML file called ua.html

\<script\> &nbsp; &nbsp; window.onload = function() { &nbsp; &nbsp; &nbsp; &nbsp; userAgent = encodeURIComponent(navigator.userAgent) &nbsp; &nbsp; &nbsp; &nbsp; alreadyLoaded = document.URL.indexOf("userAgent") \> -1 &nbsp; &nbsp; &nbsp; &nbsp; if (!alreadyLoaded) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.location.assign(document.URL+"?userAgent="+userAgent) &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } \</script\>

local userAgent local uaWebView &nbsp; local function uaListener(event) &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local findStr = "userAgent=" &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; if not userAgent and event and event.url and string.find(event.url, findStr) then &nbsp; &nbsp; &nbsp; &nbsp; userAgent = string.sub(event.url, (event.url:match('^.\*()'..findStr) or 0) + string.len(findStr)) &nbsp; &nbsp; &nbsp; &nbsp; uaWebView:removeSelf() &nbsp; &nbsp; &nbsp; &nbsp; print("User agent is", userAgent) &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; end &nbsp; uaWebView = native.newWebView(0, -display.contentHeight, 5, 5 ) -- Position webview off screen uaWebView:request("ua.html", system.ResourceDirectory) uaWebView:addEventListener("urlRequest", uaListener) &nbsp;

Excellent! Have just been tearing my hair out over this, thanks for sharing.

Excellent! Have just been tearing my hair out over this, thanks for sharing.