Ads

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(http://www.google.com/analytics/) 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

analytics.reset();

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);
analytics.trackPageview(pagename);
});

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});
}
}


// Function takes an integer which is the dispatch interval in seconds
analytics.start(10);

// 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){
analytics.stop();
});


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) {
Titanium.App.Analytics.trackEvent('Dialog','Cancel','CloseButton','');
win.close();
});


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)

https://www.google.com/analytics/web/#realtime

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?

12 comments:

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

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

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

    ReplyDelete
  3. no, it make a request but nothing happens
    http://www.google-analytics.com/__utm.gif?utmwv=4.4mi&utmn=339409638&utmcs=UTF-8&utmsr=320x480&utmsc=24-bit&utmul=en-US-undefined&

    utmac=UA-********-12&

    utmp=navigation/Favorites&utmcc=__utma=737325.4267514333.1352968911.1352991801.1353058850.35

    ReplyDelete
  4. Thanks.
    works perfectly.



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

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

    ReplyDelete
  6. Hi,

    I have one Question.

    For testing Analytics the application is compulsory available in market ?

    Thanx,
    Dharmik

    ReplyDelete
  7. Dharmik

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

    https://www.google.com/analytics/web/#realtime

    ReplyDelete
  8. Is this work in android also ?

    ReplyDelete
  9. Vaisakh,
    This is outdated tutorial about Google Analytics, but now you can use these free modules https://marketplace.appcelerator.com/apps/5081?1213411979 (developed by Matt Tuttle)

    ReplyDelete
  10. Ok thanks :)

    But the code i downloaded from the link https://github.com/MattTuttle/titanium-google-analytics

    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'

    ReplyDelete
  11. Checkout this documentation http://docs.appcelerator.com/titanium/3.0/#!/guide/Using_Titanium_Modules
    and use module from AppC market place https://github.com/MattTuttle/titanium-google-analytics

    ReplyDelete