The integration is pretty simple but the documentation is missing a couple of things. First lest install what we need. We are going to need Android developer SDK of course and Google Analytics for Mobile. Once you have everything, you need to configure 2 things:
- Copy the libGoogleAnalytics.jar to your project's /libs folder. If you do not have, you will need to create the folder and do not forget to add to your build path.
- Then you need to add the following permissions to your project's AndroidManifesr.xml manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
The next step is to start the tracker which uses the singleton pattern and you do it by calling GoogleAnalyticsTracker.getInstance(). Then call the start method and pass the tracking code ID and the activity being tracked. I usually start the tracker in the onCreate method because you just need to call it once.
import com.google.android.apps.analytics.GoogleAnalyticsTracker; public class NoteList extends ListActivity { private GoogleAnalyticsTracker tracker; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); tracker = GoogleAnalyticsTracker.getInstance(); tracker.start("UA-YOURCODE-HERE", this); } }Once tracker is started you have 3 options to collect data and send it to Google Analytics servers: Pageviews, Events and custom variables.
Pageview
I use it this everytime the user would go to a different activity or screen, which is good since you can know where the user goes the most.
tracker.trackPageView("/Add New Note");Event
As it name says it it tracks events. I usually add this to buttons, option menu and context menu items, basically everywhere where an event gets triggered. It is easy because it would be every where there is a listener method like clickListener, itemSelectedListener and so on.
tracker.trackEvent( "Clicks", // Category "Button", // Action "categories", // Label 66); // ValueCustom Variables
Think of this as meta-data that you want to send to Google Analytics in order to improve the date you send in pageviews and events. According to the documentation the best use case is to track information about full or lite versions of your app so for example you want to check how many lite version have been downloaded or just to filter between versions. For more information about Custom Variables you can head the documentation.
The custom variable has 4 methods: Index ( 1 to 5), Name, Value and Scope. This last parameter is optional but if it null, it takes the page Scope. The other 2 scopes are Visitor Scope and Session Scope. The Visitor Scope gets executed the very first time the application is running on the device so it is perfect to register the version and type of application. Session scope lasts while the application is running and Page Scope it is just between the activity and it is called right before track event or track pageview.
tracker.setCustomVar(1, "Navigation Type", "Button click", 3);Once you have all your data then comes the question, how do i send it? this is a little bit tricky since dispatching the data too often can affect on battery life so you need to very careful. That is why there are two ways to send the data one is manual tracker.dispatch(); and automatically which is set the moment you start the tracker you just need to pass one extra parameter to tell the tracker how often you want to send the data.
//dispatching every 300 seconds tracker.start("UA-YOUR-ACCOUNT-HERE", 300, this);If you are using manual, dispatch I would suggest it to call on the onDestroy() method that way you make sure the user won't notice it.
//@Override protected void onDestroy() { super.onDestroy(); tracker.stop(); }One thing I did not like about this implementation and that is missing in the documentation is that the information does not show up in Google Analytics in real time, for me took 24 hours update the information. So if you want to test this and the data your are sending you might have to wait a lot which sucks! I had to wait one day to realize that only my pageview tracks where working :(
Enjoy the new toy :)
GoogleAnalyticsTracker cannot be resolved to a type in android
ReplyDelete