The Google Analytics SDK for Android makes it easy for developers to collect user engagement data form their apps. Through this blog I will explain to you about using Analytics in Android.

Google Analytics for Android is used to fetch certain things:

The number of Active users who are using a particular application – this provides you the active user count for using the application in current time.

From where this application is being used – The country name will be displayed in map/graph, from where the application is being used.

Adoption and usage – How much is the adoption and the time of usage in your apps.

The numbers and types of application crashes – Android Analysis gives you the numbers and types from Google Analytics dashboard for application crashes.

Before starting Android App Analytics just make sure that Android SDK is available. We will be using the existing SDK.

Getting Started

Before starting any project in Android Studio you need to create an Android Analytics ID.

Follow the below-mentioned steps to get an Analytics ID for your Project:

Go to the below-mentioned link and sign into your Google Analytics Account.

https://analytics.google.com/analytics/web/#home/

2. Select the Admin Tab.

3. In the Account drop-down list, click on ‘Create new account’.

4. Select Mobile App.

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 get a Tracking ID. It will be used in your coding section to display Android Analytics.

There are some steps to start an app in Android Studio. Let us now start the code in Android Studio.

The steps are:

1. Update the AndoridManifest.xml

2. Add EasyTracker Method in MainActivity.java

3. Create a xml file for your analytics

After completing these steps, you can do different things with Analytics in Android such as:

Active Users available

Screens and User engagement for your application

Code:

AndroidManifest.xml

<uses-permission android:name = “android.permission.INTERNET” /> <uses-permission android:name = “android.permission.ACCESS_NETWORK_STATE” />

Adding EasyTracker Methods in MainActivity.java

Here we are adding two methods – onStart() and onStop() and add the below mentioned code in MainActivity.java:

public class MainActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onStart() { super.onStart() { //Code you want to add EasyTracker.getInstance().activityStart(this); } @Override public void onStop(){ super.onStop(); EasyTracker.getInstance().activityStop(this); } }

In res/values, just create one file, named analytics.xml

<?xml version = ”1.0” encoding = “utf-8” ?> <resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes > <string name = “trackid”> Your ID </string> <bool name = “autoActivityTracking”>True</bool> <bool name = “UncaughtExceptions”>True</bool> </resources>

Your Google Analytics setup is done. Now if you run the app, you will see one user on Google Analytics Dashboard. In case you are unable to see the user on the dashboard then don’t worry. Sometimes it takes some time to display. You can wait for 2-3 mins in order to see the user(s) on the dashboard.

Tracking Exception

Exceptions allows us to track all known exceptions which we tried to catch using try & catch block.

We are recording a null pointer exception using try catch block. Call trackException() method to record the exception.

Open the MainActivity.java and Add the below code:

btnException.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { String name = null; if (name.equals("ravi")) { /* Never comes here as it throws null pointer exception */ } catch (Exception e) { // Tracking exception MyApplication.getInstance().trackException(e); Toast.makeText(getApplicationContext(), getString(R.string.toast_track_exception), Toast.LENGTH_LONG).show(); Log.e(TAG, "Exception: " + e.getMessage() } } });

Now run the app and press Track Exception button. It will take 24 hours to reflect the exception on Google Analytics Dashboard.

In order to view the Exceptions, you can do the following:

Go to Behaviour → Crashes and exceptions on Google Analytics.

Conclusion

This blog was all about Analytics in Android. I think you can now check how many active users are available in your Android App.

Keep visiting our site www.acadgild.com for more updates on Android and other technologies.Click here to learn Android from our Expert Mentors