Android Integrate Google Analytics Tutorial

Google Analytics allow you to analysis your mobile application or website in almost real time. The benefits of integrate Google Analytics in your mobile application is to understand the behavior of the user when using your application. It is very useful which can help you to improve user experience to the end user. Google Analytics allow you to track event, activity, fragment , exception and crash in the application. In this tutorial, I will teach you how android integrate google analytics in the mobile application so you can view the data.

Create a new property

First, register a google analytics account

After that, you will be in the home tab, go select “Admin” tab, you will see the property section and click the drop down box , press create new property. For example you can see the picture below.

analytics1-a

Fill in property form

Select mobile apps and Choose Google Analytics SDK as tracking method. And Fill in all the required fill then click “Get Tracking ID” button.

analytics2-a

After that you will get the tracking ID, for example UA-77796837-7

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “GoogleAnalyticsExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit build.gradle

Go to your build.gradle (Module:app) and add

in the dependencies section, after that insert

in the last line outside the dependencies and sync your project now.

Create xml folder and create a xml tracker file

Right click the res > new > Android resource directory and change the resource type to xml then click “OK” button. After create the folder, then create a xml file “analytics_tracker” and copy the source code below. Note : the tracking id should be same as your tracking id in the google analytics.

Add extra permissions

In the androidManifest file add the internet and network permissions.

Set your first launch application file

Also in your androidManifest, add the name “BaseApplication” in the application section.

Add broadcastReceiver and service

Lastly, add Analytics broadcast receiver, Analytics services, Campaign Tracking receiver, Campaign Tracking services in your

Create a custom application class

Create a new java file “BaseApplication” and extends application, copy the source code below and paste it in your file.

Create a new Activity

Add a new activity “AnotherActivity” and set the analysis tracker.

Create a new Fragment

Add a new fragment”Fragment” and set the analysis tracker.

Edit acitvity_another.xml layout

Add a textview in this layout to identify the activity.

Edit my_fragment.xml layout

Add a textview and set the background color to identify the fragment is added in the activity.

Edit activity_main.xml layout

I will add 5 buttons which for tracking activity, fragment, event, exception and crash.

Edit MainActivity.java class

This class will perform all the tracking so it can be perform nicely.

Run your Project

Finally, you can run the android project and go to the google analytics and check out the realtime data.

(Android Integrate Google Analytics)

Source Code

(Visited 247 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

GET YOUR EBOOK FOR FREE

Unlock free ebook "Beginning Programming ALL-IN-ONE Desk Reference for DUMMIES" now!

We will send a copy to your email