Monday, April 9, 2012

Google Analytics in Titanium Application

I have recently used Google Analytics to track pages and track events in My Titanium iOS Application. For that I have used Roger Chapman's Titanium Google Analytics Library. I have followed the below steps

STEP 1 : Create Google Analytics Account

You need to Create a Google Analytics Account click this link( to get started.

After successful creating the account you will get a Google Analytic ID like similar to this one UA-18135349-1

STEP 2 : Add Google Analytics to Titanium App

add the below code in you app.js file

var analytics = new Analytics('UA-18135349-1'); //replace it with your Google Analytics ID


Titanium.App.addEventListener('analytics_trackEvent', function(e){
analytics.trackEvent(e.category, e.action, e.label, e.value);

Titanium.App.addEventListener('analytics_trackPageview', function(e){
var pagename = (e.pageUrl);

Titanium.App.Analytics = {
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});

// Function takes an integer which is the dispatch interval in seconds

// You don't need to call stop on application close, but this is just to show you can call stop at any time (Basically sets enabled = false)
Titanium.App.addEventListener('close', function(e){

STEP 3 : Track Pages

in your win1.js(the page which you want to Track Page View) add the below code

// track page view on focus
win.addEventListener('focus', function(e){
Titanium.App.Analytics.trackPageview('all-listings/list-view'); // here 'all-listings/list-view' page url, you can use your custom url

STEP 4 : Track Events

Add below code in EventListener of the Control which you want to track. For example in my app I have to track the closeBtn event, so that I used below code

// pass the following parameters trackEvent(category, action, label, value)
closeBtn.addEventListener('click', function (e) {

Okay. Cool now everything fine.But how to Check Google Analytics works or not?

Normally Google Analytics Account Activation takes 12 hrs to 1 day(if you created the account newly)

After the activation you can use the below link for real time testing(It will shows the last 30minutes activities)

For TrackEvent you can check this one

We can't test TrackEvent immediately like(realtime analytics). It takes some time to reflect the changes.

How to test Google Analytics track event for Mobile Applications?


  1. Wow, thank you so much for this. Works perfectly -- so cool to see everything tracked so effectively!

    1. Thanks Jason, You are Welcome. Thanks for reading my blog

  2. I've been looking for a simple solution for tracking events in Appcelerator. You nailed this tutorial!

  3. no, it make a request but nothing happens



  4. Thanks.
    works perfectly.

    any idea when there will be a release to support sdk2?

  5. Hey am not receiving any data in GA. Pls help me. My app is live.

  6. Hi,

    I have one Question.

    For testing Analytics the application is compulsory available in market ?


  7. Dharmik

    To test Analytics, your app no need to be in Market Place. You can see real time analytics too

  8. Is this work in android also ?

  9. Vaisakh,
    This is outdated tutorial about Google Analytics, but now you can use these free modules (developed by Matt Tuttle)

  10. Ok thanks :)

    But the code i downloaded from the link

    doesnot seems to work when i compile and run it in titanium studio. I am new to titanium. Can you please show me a way.. I want an android application that can access the google analytics api and retrieve graphs, pie charts and all stuffs and show it like the android application 'ganalytics'

  11. Checkout this documentation!/guide/Using_Titanium_Modules
    and use module from AppC market place