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. http://mymobileapp.mywebsite.com)
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 -DgroupId=com.google.android.apps.analytics -DartifactId=GoogleAnalyticsTracker -Dversion=0.7 -Dpackaging=jar

Let's create a basic app.

F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml

Choose the galatea-archetype plugin
groupId: org.eoti.android
artifactId: AnalyticsTest
version: 1.0-SNAPSHOT
package: org.eoti.android

Assuming your emulator is running...


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

Add this dependency to your pom.xml:
<dependency>
  <groupId>com.google.android.apps.analytics</groupId>
  <artifactId>GoogleAnalyticsTracker</artifactId>
  <version>0.7</version>
</dependency>

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;

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

        setContentView(R.layout.main);
        track("/");
        dispatch();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        tracker.stop();
    }

    // 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)
    {
        tracker.trackPageView(page);
    }

    // Allows sending in bulk by queuing up multiple tracks before calling dispatch
    // timestamps based on when dispatch is called
    private void dispatch()
    {
        tracker.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