Dec 27, 2010

Tracking Java ME Apps with Google Analytics

Hi all,

Today I would like to announce another project of mine, which I believe it will be very helpful for most Java ME developers. This new project is called  Google Analytics ME.

This micro Java API allows developers to track their Java ME apps using  Google Analytics. Developers will be able to analyze how the users are using their apps, using this Google's powerful tool, just like they are used to do with their websites.

To work with Google Analytics ME is as easy as it is to install that piece of JavaScript code provided by Google, in order to start tracking your websites. The only requirement of Google Analytics ME is a tracking code (e.g. UA-1736743-0), which you get as soon as you register an account for your website (app) on Google Analytics.

Once you have your tracking code, all you need is to specify which points of your app will be tracked by the API.

Google Analytics ME is capable of working with two types of events provided by Google Analytics: Page View and Event Tracking.

A Page View is an event that represents a page (or screen) that was viewed by a user. For instance, you may use this event to check how many times your app was started up, by tracking the MIDlet's constructor. See below how to implement it:

public AppMIDlet() {
    ...
    Tracker tracker = Tracker.getInstance("UA-1736743-0");
    tracker.addToQueue(new PageView("/app_started"));
    ...
}

In a similar way, you can also track how many times each app's screen is viewed by users, just using another unique id for each one. For instance:

public MainMenu() {
    super("Media Player")
    ...
    Tracker tracker = Tracker.getInstance("UA-1736743-0");
    tracker.addToQueue(new PageView("/media_player"));
    ...
}

An Event Tracking may be used to track events that occur in a screen, e.g., a button click or the usage of a given feature. For instance, considering your app is a media player, you can track how many times or how long a given song is played.

An event is composed of four information: Category, Action, Label and Value. A Category only works to group actions. For instance, a Category could be a screen's title, which you intend to track its events. The Action, considering a media player, would be the events of playing or stopping a song. In addition, for each Action, a Label may also be specified. In this case, you could use a Label to indicate the song's name being played.

Specifying up to these three parameters, Google Analytics will count how many times they were tracked. On the other hand, when you specify the Value, which is a integer, Google Analytics will also sum all values that were tracked for a given Category, Action and Label.

This way, we can track, for instance, for how long a given song was played, because Google Analytics will display a sum of all values.

See below how to work with events:

public void play(String media) {
    Tracker tracker = Tracker.getInstance("UA-1736743-0");
    tracker.addToQueue(new Event("/media_player", "Play", media, null));
    ...
}

public void stop(String media) {
    Tracker tracker = Tracker.getInstance("UA-1736743-0");
    tracker.addToQueue(new Event("/media_player", "Stop", media, timePlayed));
    ...
}

It is important to point out that only Category and Action are required. For further information on event tracking, click here.

You may be wondering why the events are being added to a queue. It is because the Tracker class can work synchronously and asynchronously. All queued events are flushed automatically by an internal background process that runs within a fixed-delay strategy. In addition, to work synchronously, you can use the method Tracker.track(Request), which immediately sends the event to Google Analytics.

...
Tracker tracker = Tracker.getInstance("UA-1736743-0");
tracker.track(new Event("/media_player", "Rewind", null, null));
...

You can also adjust the interval between each flush operation, by using the method Tracker.getIntance(String, long). In addition, the queue can be explicitly flushed through the method Tracker.flush(boolean).

...
Tracker tracker = Tracker.getInstance("UA-1736743-0", 120);
...

The code above specifies that background process runs at each 120 seconds. It is also important to point out that there is only one Tracker object per tracking code, since it uses the Singleton pattern.

As usual regarding my projects, this one is also available on Kenai.com and can be downloaded here.

I hope you guys enjoy it.

Happy new year!

See you in the next post...

3 comments:

Anonymous said...

this project is very intersting and I will use it in my next work.

Ajibola said...

Your JavaME API's are very useful. Hope to use this in my first ME app to be launched, just some tweaking left and UI improvements. Great stuff, keep up the good work.

Eric Simmons said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai