Search This Blog

Showing posts with label analytics. Show all posts
Showing posts with label analytics. Show all posts

31 May 2010

Disclaimer


Use the Alert Dialogs example as a starting point.....

Add this icon to your src\main\android\res\drawable directory

I grabbed that icon from the Tango project, but had to change the filename to be compatable with Android.

Then from the Google Analytics example, create an analytics profile, update your pom.xml and AndroidManifest.xml


Instead of putting the tracker code directly into the onCreate method, let's move that into the "I Accept!" button.  Note the change to onDestroy as well!

public class DisclaimerTestActivity extends Activity {
    private static String TAG = "DisclaimerTest";
    private GoogleAnalyticsTracker tracker = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("Disclaimer");
        dialog.setMessage("In order to provide support for this application, we reserve the right to anonymously track and report usage information in this application.  We will track which parts of the application are popular but will not track who you are or what you type / look at.");
        dialog.setIcon(R.drawable.network);
        dialog.setButton("I Accept!", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                tracker = GoogleAnalyticsTracker.getInstance();
                tracker.start("UA-YOUR-ACCOUNT-HERE", DisclaimerTestActivity.this);
                setContentView(R.layout.main);
                track("/");
                dispatch();
                return;
            }
        });
        dialog.setButton2("No Way!", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
                return;
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if(tracker != null) 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();
    }
}


Redeploy (mvn clean install).  Now, if you accept the disclaimer it will do analytic notifications (which can take up to 24 hours to show up).  If you cancel, it will just close the app.

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: