How to use Google Analytic for your app

Standard

Introduction

The Google Analytics SDK for Android makes it easy for developers to collect user engagement data from their apps. Developers can then use the Google Analytics app-tracking reports to measure:

  • The number of active users are using their applications.
  • From where in the world the application is being used.
  • Adoption and usage of specific features.
  • In-app purchases and transactions.
  • The number and type of application crashes.
  • And many other useful metrics.

Before you Begin

Before begin implementing the SDK, make sure you have the following:

1. Updating AndroidManifest.xml

Update your AndroidManifest.xml file by adding the following permissions:

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

2. Adding tracking methods

Add the tracking methods to the onStart() and onStop() methods of each of your Activities as in the following example:

/**
 * An example Activity in your app with Analytics
 * implemented.
 */
public class myTrackedActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 }

 @Override
 public void onStart() {
 super.onStart();
 ... // The rest of your onStart() code.
 EasyTracker.getInstance().activityStart(this); // Add this method.
 }

 @Override
 public void onStop() {
 super.onStop();
 ... // The rest of your onStop() code.
 EasyTracker.getInstance().activityStop(this); // Add this method.
 }
}

3. Creating your analytics.xml file

In version 2 of the Google Analytics SDK for Android, your tracking settings and configuration options are managed from an xml resource file, calledanalytics.xml. You’ll need to create this file in your project’s res/values directory and add your tracking ID. The following example shows how you can add your tracking ID, and enable Activity and exception tracking:

<?xml version="1.0" encoding="utf-8" ?>

<resources xmlns:tools="https://schemas.android.com/tools" tools:ignore="TypographyDashes">
 <!--Replace placeholder ID with your tracking ID-->
 <string name="ga_trackingId">UA-XXXX-Y</string>

 <!--Enable Activity tracking-->
 <bool name="ga_autoActivityTracking">true</bool>

 <!--Enable automatic exception tracking-->
 <bool name="ga_reportUncaughtExceptions">true</bool>
</resources>

One thought on “How to use Google Analytic for your app

Leave a Reply

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