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 AccountYou 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-1STEP 2 : Add Google Analytics to Titanium Appadd 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/#realtimeFor
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?