The Blog

Google Analytics Bridge for Android developers

23 Ago 09

Google Analytics Bridge is a java library for android ( Soon to be Open Source licensedΒ ) that will help developers understand what the user interaction with their own application.

Google Analytics Bridge is really easy to setup and it runs on a separate thread. This is do to the problem with the internet request sometime freezing the UI and setting off the hated message:

“The Application xyz is not responing”

With the two choises :

  1. Force Close
  2. Wait

Since The Google Analytics Bridge runs on a different thread to avoid this error.

First Setup

Download and import the jar file to the application you want use the Google Analytics Bridge on.Β The setup is a fast one-line command but you need the following information:

  • Google Analytics Tracking Code ( EX: “UA-123123123-1” )
  • The Domain Name

Set up a brand new Google Analytics Profile

you will need to set up a new Google Analytics Profile or use an existing one. You will need to open a new website profile on google analytics.

The procedure is fast and simple but it does require The domain name.

In that field you can specify whatever you want, but i suggest something like:

track.your-application-name.com

After creating the new profile, copy the UA-XXXXXX-Y code for a further usage.

The Constructor

Once you’ve done all of step one it’s time to create our constructor.

In the onCreate routine place the following code:

[JAVA]new AnalyticsBridge(this.getApplicationContext(),”UA-XXXXXX-Y”,”track.your-application-name.com”);[/JAVA]

You don’t need to create an AnalyticsBridge object , after this line you will call the methods in a static way.

NOTE: You should call the constructor once per application lifetime.

Public Methods

_trackPageview(String pageTitle)

If you’ve some experience integrating google analytics on your own website you’ll also remember what this method will do.

_trackPageview method on the web it’s used for logging a new page View by the user. Similarly calling this static method on android will generate a google analytics request that will log a new Activity View.

Lets say you’ve a lot of activity, and some of theese will show the same content but in a different way, then i would put the following code on each onCreate Subroutine:

[JAVA] /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AnalyticsBridge._trackPageview(this.getClass().getName());

}[/JAVA]

This code will log each opened activity to google analytics.. Isn’t it easy? πŸ™‚

_trackEvent(String category, String action)

_trackEvent method is the same used on the web google analytics apis. I made this bridge because you should want to track some event made by the user.

Let suppouse you want to track a button click event on your user interface – this could help you understand how the user interact with your user interface – then you simply write the following statement on your View.OnClickListener implementation:

[JAVA] Button loginbutton=new Button();
loginbutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
AnalyticsBridge._trackEvent(“Click”, “Login Button”);

}
});[/JAVA]

That code will track the event of category “Click” and action “login Button”.

Download:

Right now the library is quite stable . You can download it here

Thanks

Thanks to barakinflorida who corrected my bad english πŸ™‚

Comments