Using Google Analytics in Titanium Mobile
This is an Answer not a Question! :)
I've wanted to use Google Analytics to track pages in my app, however implementing the Google Analytics SDK via a Titanium module was a pain!
So, I decided to replicate the SDK code direct into Titanium.
This also means I don't have to maintain and build a separate module, which is nice.
All the code is on github: https://github.com/rogchap/Titanium-Google-Analytics
It is currently setup for iOS, however it would be easy to change to Android by changing the user-agent string.
I hope some one out there finds this useful!?
Leave a comment if you use it.
20 Answers
-
@Scott Miller
In your app.js call the analytics.js
set the params and the events.Then on each window/page/view you want to track, have an onFocus event set that calls his track page view function. The param it takes is the "page" that shows in your google analytics results.
win1.addEventListener('focus', function(e){ Titanium.App.Analytics.trackPageview('/win1'); });
So, if I have a window whose views show a RSS feed, i might call
win1.addEventListener('focus', function(e){ Titanium.App.Analytics.trackPageview('/rss'); });
After the user clicks one of the items in the list and views a window with the article in it, maybe call something like
win1.addEventListener('focus', function(e){ Titanium.App.Analytics.trackPageview('/win1/'+urlencode(rsstitle); });
-
Roger, I'm getting an error (downloading the source and compiling without changes):
"[ERROR] Script Error = Can't find variable: Class at analytics.js (line 78).
[ERROR] Script Error = Result of expression 'Analytics' [undefined] is not a constructor. at app.js (line 6)."
(I'm compiling with version 1.4.1.1 of the SDK, but that doesn't especially matter in this instance.)
-
works like a charme. thanks Roger!
-
Hi,
I'm getting the following warning message when calling 'Titanium.App.Analytics.trackPageview(my_url)' from other pages.
[WARN] (null)->(null) [0]->not specified is being made in a thread not owned by <KrollContext: 0xc006200>
Any ideas?
-
hi ?i used the analytics.js,but it does not work!
1 Copy analytics.js to Resources
2 Paste next code into app.jsvar googleAnalyTics_id = 'UA-37684090-1'; /* analytics */ Titanium.include('analytics.js'); var analytics = new Analytics(googleAnalyTics_id); analytics.reset(); Titanium.App.addEventListener('analytics_trackPageview', function(e){ analytics.trackPageview('/iPad' + e.pageUrl); }); Titanium.App.addEventListener('analytics_trackEvent', function(e){ analytics.trackEvent(e.category, e.action, e.label, e.value); }); Titanium.App.Analytics = { trackPageview:function(pageUrl){ Titanium.App.fireEvent('analytics_trackPageview', {pageUrl:pageUrl}); }, trackEvent:function(category, action, label, value){ Titanium.App.fireEvent('analytics_trackEvent', {category:category, action:action, label:label, value:value}); } } analytics.start(10); /* analytics end */ Titanium.App.addEventListener('close', function(e){ analytics.stop(); });
3 Paste next code when a win open
Titanium.App.Analytics.trackPageview('win-name');
It is no error! but when i go to https://www.google.com/analytics/web/ ,there is no data.
The url Analytics'client opened is
http://www.google-analytics.com/utm.gif?utmwv=4.4mi&utmn=644932630&utmcs=UTF-8&utmsr=320x480&utmsc=24-bit&utmul=en-undefined&utmac=UA-37684090-1&utmt=event&utme=5(ButtonClicklogin)(4)&utmcc=utma=737325.-317559966.1358739378.1358739378.1358739378.1Was there something i missed?
-
Hi Roger or anyone else that may have used this to publish an app.
Does the app store allow the location data fed back to Google?
It is probably not GPS but rather your ISP providing a location, but just wanted to know if it has caused anyone any issues?
-
BTW - you might want to check out Appsee for your mobile analytics, its way nicer than GA and gives you things like user video recordings and heat maps… They have an appcelerator plugin (www.appsee.com)
-
Nice work Roger!
-
Any reason why data wouldn't show up in the GA interface?
I'm seeing the info messages within titanium saying that a page has been reached…
-
I'm not sure if GA doesn't completely report the data until hours later, but it appears the iPhone OS version is not available within GA. Any idea why? These user-agent strings look good:
Here is the user-agent from your js file:
User-Agent: GoogleAnalytics/1.0 (iPhone Simulator; U; CPU iPhone OS 4.1 like Mac OS X; en-undefined)Here is the user-agent from iPhone simulator on a website:
User-Agent: Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7 -
Thanks for sharing, I will be implementing this when my app nears completion
-
Roger,
Have you checked the module against 1.5? I think some runtime errors are occurring.
-
Hello guys, i am new to titanium. Somebody could give a step by step on how to implement this module into an app?
-
I am getting an error when I include analytics.js
"Result of expression 'Titanium.Database' [undefined] is not an object at analytics.js (line 107)
Any clue?
-
Roger in your readme file you say "In the Titanium SDK add the following… iphone/Classes/PlatformModule.m
-(NSString)countryCode
{
NSLocale locale = [NSLocale currentLocale];
return [locale objectForKey:NSLocaleCountryCode];
}iphone/Classes/PlatformModule.h
@property(readonly,nonatomic) NSString *countryCode;
"what is the file name of the file to update? There is a lot of files in the sdk folder.
-
i was trying to run the project file provided, unfortunately it cant be run in emulator (no "launch" button). anyone could help me?
-
I've just started testing this out and it seems promising! However, from the example in app.js, do I need to put the code at the top on every page I want to track? Just curious as to how you all did it. I'm thinking maybe just using an include?
-
roger plz i want the explanation to this code…….can u do that for me
-
I got it to work perfectly but I have one problem: when you're on a slow connection like edge or really slow 3g the app will stop functioning for a few seconds while uploading to google. Can you change the time limit or do anything else to make it work more smoothly?
-
"I hope some one out there finds this useful!?"
Ha Ha Ha!!!
you sir are amazing! and i hope you will live for 1000 years!
answer to your question: found it useful thanks :D