Sunday, August 7, 2011

Track your android application with Google Analytics

Google Analytics has been the best available tool for tracking visitors activity on the websites, now Google has extended the support for it for mobile application developers and introduced the Google analytics for mobile.This lets you track the application usage and also the custom events. The mobile API is as simple as easy to use as the web API.

To get started with the development, you can involve the jar downloaded to your project and get the Google Analytics key from the analytics website. To start with data usage collection, create an object of the GoogleAnalyticsTracker class and instantiate with you unique API key.

        tracker = GoogleAnalyticsTracker.getInstance();

        tracker.start("UA-YOUR-ACCOUNT-HERE", <time to dispatch> ,this);

It is better to call the "start" function at the start of the main android activity and set the  "time to dispatch"  wisely.The system is designed in a way which does not consume a lot of battery by sending the usage data continuously,rather it stores all the data in a temporary SQLLite database which at the "dispatch" call sends all the data to the analytics server.Adding a parameter "time to dispatch" to the start function calls the dispatch function automatically after the specified time.

To catch an event call the  trackPageView function with the page name as the parameter.

        tracker.trackPageView("/testApplicationHomeScreen");

Custom variables can also be tracked by the setCustomVar function.

        tracker.setCustomVar(1, "Navigation Type", "Button click", 2);

Events such as button clicks can also be tracked with the trackEvent function

        tracker.trackEvent("Clicks","Button","clicked",123);   
The tracker can be manually dispatched by using

        tracker.dispatch();
 and the tracker object can also be destroyed so that it does not collect any further statistics by using

         tracker.stop();

So, In case you are having a website and an iPhone+android application for  same, you can track the complete usage using a single service and have the consolidated data about the users.Although the statistics data is delayed by few hours but still for a service with considerable usage, there is nothing better than Google Analytics!

Saturday, July 30, 2011

Hybrid applications using phonegap and sencha touch

Before you start off with developing a mobile application, make sure that you clearly know the benefits and limitations of both a native application and a HTML5 application ad choose which best fits into your requirements. This video from google IO would be helpful in making the correct decision.

Phonegap is an application which bridges the divide between native and HTML5 applications for mobiles and can be used to create hybrid applications for 6 popular platforms.In this tutorial, I create a simple application for android using phonegap and sencha touch which is a popular javascript platform providing great support for making html5 applications for touch devices.
Firstly, create a new android project in eclipse (refer this post on how do create a simple project in android) and include the phonegap jar to the project. Also create a folder "/assets/www" wherein we will put all the html5 content(for details refer to the phonegap setup page). Replace the main activity code with the following code:

public class SenchaPhoneGap extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }


Now the application has been setup and any kind HTML content in the "www" folder would be displayed as an android application and an .apk file will also be generated.
Next, we create some pages using sencha touch for our application.For this, create an "index.js" file and also put the "sencha-touch-debug.js" and "sencha-touch.css file" from the sencha touch sdk to you "www" folder.Also include these in the "index.html" file. The folder structure for your android application looks somewhat like this now:


and the index.html file will look somewhat lie this

<!DOCTYPE html>
 <html>
 <head>
     <meta charset="utf-8">
     <title>Sencha Phonegap</title>
     <link rel="stylesheet" href="sencha-touch.css" type="text/css">
     <script type="text/javascript" src="sencha-touch-debug.js"></script>
     <script type="text/javascript" src="index.js"></script>
 </head>
 <body></body>
 </html>


Now, all the presentation part and the logic part of the app goes into the index.js  . In this example, i use the code explained in this video which explains the panels in sencha touch. I dock 3 panels in a Tablayout which is a special layout for arranging the content in tabs. Below is the code for index.js
Ext.setup({
    onReady: function() {
        var twitter ={
        dock:"top",
        style:"background-color: #000099;color:white;",
        title: "twitter",
        html: "some content from twitter comes here"
        }
        var plus ={
        dock:"top",
        style:"background-color: #aaaaa;color:black;",
        title: "Google Plus",
        html: "some content from Google Plus comes here"
        }
        var facebook ={
        dock:"top",
        style:"background-color: #aaaa55;color:white;",
        title: "facebook",
        html: "some content from Facebook comes here"
        }
        var rootPanel= new Ext.TabPanel({
        fullscreen: true,
        layout: 'fit',
        items: [twitter,plus,facebook]
        });
    }
    })
;


You can run the application on the android emulator to check if the code is working.It would look similar to the below screenshot.


The combination of sencha and phonegap provides a super way of porting the applications to all the popular platforms.Follow the steps to create a simple phonegap application from http://www.phonegap.com/start and copy the contents for the "www" folder. Also, if you want to use the native capabilities of the phone such as location,database,filesystem,camera,accelerometer the you can also add the phonegap.js file from the phonegap sdk. You can download the code of this application from here
Watchout this space for more tutorials on how to create hybrid applications!


Update : 30 Dec 2012: I just bought an Android developers account and as an experiment I have uploaded this tutorial to play store at this location  :)

Thursday, June 30, 2011

Google Plus! hype or the future??


After the terrible failure of Google wave, Google Buzz and loosing all the Orkut users to Facebook, Google has come out with yet another social networking solution called the Google Plus! As the end user, one would as to himself "should I join Google plus when I am happily connected to all my friends on Facebook?" Well it would be too early to answer the question but for now lets have a look at what Google has to offer.

   As expected, Google played the game of invites again and let users send limited invites to their friends so that they can check out Google and there be a race to join the new service just the way it was with Gmail and Google wave. But this time they totally failed at this! Well, In case any of your friends having a plus account  shares some content with you, you get an email notification for the same and before you know it, you have a Plus account! That was a big security loophole which brought Google's strategy on its knees. Within just one day, I have most of my friends connected to me on Google Plus! Nailed it totally!

  Next is the usability for which we all are Facebook addicts.Well, the interface looks pretty neat and the updates and comments are more like Facebook, although it would have been better of the font size was smaller to fit in more content and the size of the photos I see in the updates is nothing less than HUGE! With all the rich functionalists of the application, it was expected to have a sluggish response to the user actions but Google has done a great job at this.The site is very responsive and runs smooth on Chrome and Firefox.

  What will attract the users would be the accessibility. Almost all the Google services now have a notification icon on the top right corner which shows the updates from Google Plus, more like the the notifications of Faceook but a lot more organized.With this, the user need not to have a G+ page open all the time to get updates as is the case with Facebook. I wonder if this will make blocking the website at school, colleges and workplace more difficult.

 Privacy seems to be a major concern after all the mess created on Facebook by the  spamming applications. There are no application on Google Plus as of now because the API has not yet been exposed to the developers, so for now that dimension can be looked over. One major flaw that I found with privacy was with the content such as photographs and videos. It fetched all my photographs from Picasa and shared it publicly so that my friends can further share them,so before joining , do make your Picasa albums and buzz content private. Also it enables edit a post after posting and receiving comments on it,I can already imagine scenarios where this can be misused making it another security flaw.

 My final verdict would be that Google has done a good job this time, but they look to be in a hurry!The application should have been tested properly and the launch delayed.Also before making a complete switch, I I would also look forward to "something awesome" that Facebook is about to launch in few days(ref http://www.reuters.com/article/2011/06/30/us-facebook-idUSTRE75T55S20110630) which I assume is the much awaited video calling feature!



 

 

Saturday, April 23, 2011

Scripting on android

With growing popularity if android amongst the developers, google launched the project Scripting Layer for Android(SL4A) which enables the developers to write scripts in multiple languages such as PHP,Perl,python,Lua,JRuby etc. All you need to do is to install SL4A from the android market and add the interpreters. The API for all the languages is almost similar and easy to understand. A quick helloworld example in all the supported languages can be seen  here . Also there is a complete list of tutorials which will help explain the usage. The neat API reference is also present to make the work easy for the developers.For each installed interpreter, there will also be a set of examples to keep you motivated. for example, a simple python program to open the camera, click a picture and save to the sdcard would cost you just 3 lines of code as:
import android
droid = android.Android()
droid.cameraCapturePicture('/sdcard/python_picture.jpg')


I would try to post some tutorials on SL4A scripting soon.For now check out the SL4A screencasts for a kickstart! 

Friday, April 22, 2011

Upgraded to FROYO on galaxy 3 i5801. Bugs and solutions

Samsung recently rolled out the much awaited froyo(android 2.2) upgrade for my Galaxy 3 i5801 (a.k.a Applo) handset.After a lot of trouble with downtime on samsung's servers and getting the same error "Operation timed out" for tens of times, I successfully upgraded my phone to a newer if not the latest version.
   It shows a significant boost in the performance and battery life. The phone now feels more smooth and has got rid of the very sluggish galley,messages and contacts applications.Also now it has got a cool new music player interface, a fresh menu and a good task manager. Moving some of the applications to the SD card has made it perform even better.
   The only 2 issues that i found were that swype was no longer working and the volume buttons could no longer be used to make a call silent.For swype, I installed "keyborad from android 2.3 " which is also pretty cool but not as cool as swype and for making a call silent, the power button can be used.I really hope to find a solution to the swype thing because of all its functionality.
  For now I need to create another AVD for android 2.2 in order to continue developing and testing my applications on the phone.

Update 04 May 2011:
Yet another major bug with the upgrade is that the camera application is crashing all the time. This kept me from capturing some really great memories yesterday and I really hate samsung for this one. For now, i have installed FxCamera from the market which almost solves the purpose and also has some other cool features such as decent autofocus which was missing in the original cam application.
Although Samsung is the one who has made the most out of Android and Open handset Alliance but they still need to work on the upgrades to retain their customers and continue to have a good image in the market.