2015-08-15

Have you ever wondered how to track your android user activity in real time? If yes, well here is the solution you are looking for. User activity tracking can be done using any good analytics tool. There are lot of tools out there like Mixpanel, Flurry etc., to track the user activity in real time but none of them are totally free. Google analytics is a powerful service which comes with great features like real time user count, event tracking, geo tracking, error & crash reporting and lot more and yet it is completely free.

In this article we are going to learn how to integrate Google Analytics in your android app. Although Google Analytics provides huge number of analytical features, we are going to cover only the essential modules necessary for every android app.



DOWNLOAD CODE

VIDEO DEMO

Below is the screenshot of real time users on one of the apps I have worked on.



1. Creating Google Analytics Property

Google Analytics tracking ID identifies the analytics property for which mobile tracking is enabled. Follow the below steps to get your property tracking ID.

1. Sign in to your Google Analytics account.

2. Select the Admin tab.

3. In the ACCOUNT dropdown, click on Create new account. (Or select the property if you created one already)

4. Select Mobile App in the new property form.

5. Enter the App Name.

6. Select Industry Category & Report Time Zone and click on Get Tracking ID.

7. In the next screen you will be shown your property Tracking ID.

The tracking ID should look like UA-XXXXXXXX-X. Note this tracking ID somewhere as we’ll need this in our android project.



2. Creating Android Project

Once you got the tracking ID, let’s start the android project. We’ll create an android app with simple user interface to test the each module.

1. In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Blank Activity and proceed.

3. Turn your app into Material Design supported if you want to. But this step is optional.

5. Create two packages named activity and app to keep the project organized. Move the MainActivity.java under activity package.

4. Open strings.xml located under res ⇒ values and add below string values.

5. Open colors.xml located under res ⇒ values and add below color resources. If you don’t see colors.xml, create a new file with the name.

2.1 Adding Google Analytics

Now follow below steps to add the google analytics to your project.

2. Open build.gradle located under app folder and add google analytics dependency com.google.android.gms:play-services-analytics:7.3.0

4. Create a folder named xml under res. Inside xml folder, create an xml file named app_tracker.xml and add below analytics configuration.

Make sure that you replaced the ga_trackingId with your tracking ID which we retrieved in above section. This step is very important, otherwise you won’t be able to see the stats on your analytics dashboard.

6. Under app package, create a class named AnalyticsTrackers.java with the below code.

8. Create a class named MyApplication.java under app package. This class extends from Application class which needs to be added in AndroidManifest.xml.

This call contains below important methods.

trackScreenView() – Function to track screen view (Activity or Fragment).

trackException() – Function to track exceptions using try & catch.

trackEvent() – Function to track event.

11. Open AndroidManifest.xml and do the below changes.

> Add MyApplication to <application> tag.

> Add INTERNET and ACCESS_NETWORK_STATE permissions.

> Add the receivers and services mentioned below. These are optional, but recommended for accurate results.

Congratulations! your Google Analytics setup is done. Now if you run the app, you should see one user on Google Analytics dashboard. You won’t be able to see the user on the dashboard instantly due to slight delay in google analytics. You will have to wait for 2-3 mins to see the users on the dashboard. If no users are displayed, make sure that you followed the above steps correctly.

Before start discussing about the modules, we’ll do the changes needed to create the interface to demonstrate them.

12. Under res ⇒ layout, create an xml layout named toolbar.xml and add below code.

12. Open the layout of your main activity (activity_main.xml) and do the below changes. This layout contains few buttons to invoke different analytics events.

13. Open MainActivity.java and do the below changes. Here we are declaring few buttons those are defined in the xml layout.

Now if you run the app, it should look like below.

2.1 Tracking Activity

Tracking activity view is very simple. It can be done in two ways. You can choose whichever way suits to your app requirement.

⇒ Automatic Screen Tracking

If you set ga_autoActivityTracking to true in app_tracker.xml, all the activities tracking will be automatically. However if you want to keep a name to screen, you need to mention the activity screen name in app_tracker.xml. If you have multiple activities, you have to mention the screen names for every activity.

Open app_tracker.xml and add below code. (Replace the package name with yours)

⇒ Manual Screen Tracking

If you want to track the activity view manually, call the trackScreenView() in your activity onResume() method.

2.2 Tracking Fragment

Tacking fragment view is same as activity tracking except fragment can’t be tracking automatically. You have to manually invoke the screen view code in fragment onResume() method. We’ll test this by creating simple fragment and add it in our main activity.

11. Create an xml layout under res ⇒ layout named fragment_footer.xml.

17. Create a class named FooterFragment.java and add the below code. You can notice that trackScreenView() is called in onResume() method which records the fragment view whenever the fragment is added or resumed.

15. Now open MainActivity.java and add a click event listener to load the fragment.

Now run the app and press the Fragment Tracking button. And after 2 – 3 mins you can the fragment screen view on Google Analytics dashboard.

2.3 Tracking Event

Event tracking allows us to record any kind of user interaction within the app. The interaction can be anything like button press, selecting a spinner, swiping gestures, completing game level etc.,

Event tracking accepts four parameters. Category, Action, Label and Value. Below is a simple event example when user presses the book download button.

13. Open MainActivity.java and add below button click listener and call trackEvent() to track the event.

Now run the app and press the Track Event button. On the Google Analytics dashboard click on Events to see the event sent from the app.

2.4 Tracking Exception

Exceptions can also be tracked very easily. This allows us to track the all the known exception which we tried to catch using try & catch block.

15. Open your MainActivity.java and add the below code. In this code we are recording a null pointer exception using try catch block. Call trackException() method to record the exception.

Now run the app and press the Track Exception button. You won’t be able to see the recorded exception right away on the dashboard. It normally takes 1 day to reflect on Google Analytics dashboard.

To see exceptions, goto Behaviour ⇒ Crashes and Exceptions on Google Analytics.

2.5 Tracking App Crashes

App crashes happens due many unknown factors. App crash recorded automatically by setting ga_reportUncaughtExceptions to true in app_tracker.xml. We’ll test this by crashing the app intensionally.

17. Open MainActivity.java and add below code. Here we are dividing a number with zero which throws Arithmetic Exception and crashes the app.

Now run the app and press the Track App Crash. Your app should crash after 1.5 sec. This app crash will be recored and will be shown on Google Analytics dashboard. Goto Behaviour ⇒ Crashes and Exceptions section on dashboard to see the exception. This also takes 1 day to reflect on dashboard.

I hope this articles gave you good start with Google Analytics SDK v4. Go through the docs and try other features offered by google analytics.

Show more