Search This Blog

30 May 2010

Google Analytics

Let's integrate Google Analytics into your app...

Go into your analytics account and click on "Add Website Profile" at the bottom of the table.  According to Google:
using a fake but descriptive website URL (e.g.
I assume this is because it has to be a second-level domain and not a full Market URL.

Write down the "Web Property ID" in the yellow box (ie: something like UA-12345-67)

Reminder, Google says:
You must indicate to your users, either in the application itself or in your terms of service, that you reserve the right to anonymously track and report a user's activity inside of your app.
Make sure to hit "Save and Finish"

Download the Google Analytics SDK for Android.  Extract it somewhere convenient.

Deploy it to your local repo
F:\java\GoogleAnalyticsAndroid_0.7> mvn install:install-file -Dfile=libGoogleAnalytics.jar -DartifactId=GoogleAnalyticsTracker -Dversion=0.7 -Dpackaging=jar

Let's create a basic app.

F:\work> mvn archetype:generate -DarchetypeCatalog=

Choose the galatea-archetype plugin
artifactId: AnalyticsTest
version: 1.0-SNAPSHOT

Assuming your emulator is running...

F:\work> cd AnalyticsTest
F:\work\AnalyticsTest> mvn clean install

Add this dependency to your pom.xml:

Add these to your AndroidManfiest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Edit your activity:
public class AnalyticsTestActivity extends Activity
    private static String TAG = "AnalyticsTest";
    private GoogleAnalyticsTracker tracker;

    public void onCreate(Bundle savedInstanceState)
        tracker = GoogleAnalyticsTracker.getInstance();
        tracker.start("UA-YOUR-ACCOUNT-HERE", this);


    protected void onDestroy()

    // track("Clicks", "Button", "clicked", 77
    private void track(String category, String action, String label, int value)
        tracker.trackEvent(category, action, label, value);

    // track("/testApplicationHomeScreen");
    // track("/download");
    private void track(String page)

    // Allows sending in bulk by queuing up multiple tracks before calling dispatch
    // timestamps based on when dispatch is called
    private void dispatch()

Make sure to change your UA-YOUR-ACCOUNT-HERE to match your Web Property ID from above.

Redeploy (mvn clean install) and run your app... and then... voila???

No, not quite.  From Google:
Google Analytics generally updates your reports every 24 hours. This means that it could take 24 hours for data to appear in your account after you have first installed the tracking code.

Looks like I will be checking back tomorrow to make sure it works ;)

Update: About 18 hours later:

No comments:

Post a Comment