27 Apr 2014

Integrating Google Analytics with Android

Hello Friends,

The Google Analytics SDK for Android makes it easy for developers to collect valuable statics about how the users are using their app.

Features Android Google Analytics:

- The number of active users that are using the application
- Usage of specific features in application by any user
- Lock the number and type of application crashes
- From where in the world the application is used
- And many other useful features...

Steps to Integrating Google Analytics with Android application:

1. Create a new Google Analytics Account  Create Account

2. Add a new mobile app in your account(This is the app which you want to track)

3. After you add a app in your account,google provides you a tracking Id unique for that app. Note down the tracking ID , it is usually in  UA-XXXX-Y format

4. Download the Google Analytics jar file Jar Download Link

5. String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

   <!-- Replace placeholder ID with your tracking ID -->
   <string name="ga_trackingId">your tracking ID</string>

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

   <!-- Enable debug -->
   <bool name="ga_debug">true</bool>

   <!--
   The inverval of time after all the collected data
   should be sent to the server, in seconds.
   -->
   <integer name="ga_dispatchPeriod">30</integer>

</resources>

6. DashboardActivity 
public class DashboardActivity extends Activity
{
 private GoogleAnalytics mGaInstance;
 private Tracker mGaTracker;

  @Override
 public void onStart() 
 {
  super.onStart();
  EasyTracker.getInstance().setContext(this);
  EasyTracker.getInstance().activityStart(this);
  mGaTracker.sendView("DashboardActivity Visited...");// Add this method.
 }

  @Override
 public void onStop() 
 {
  super.onStop();
  EasyTracker.getInstance().setContext(this);
  EasyTracker.getInstance().activityStop(this);
  mGaTracker.sendView("DashboardActivity Visited..."));// Add this method.
 }

  @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.act_weblink);

   mGaInstance = GoogleAnalytics.getInstance(this);
  mGaTracker = mGaInstance.getTracker(getString(R.string.tracking_id));  

    // Add tracking functionality to button
      Button btnABC= (Button) findViewById(R.id.btn);
      btnABC.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View arg0) {
            // The rest of your code
            mGaTracker .trackEvent("Screen Name", "btnABC Event", "Android app", Long.parseLong("1"));
         }
      });
 } 

}
Note- Don't forget to add Permission Manifest File-

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

Result of tracing application:

If everything is configured correctly, the reports should appear on live. Usually it takes about few hours to see the data in your account.





More Reference Link:

Get starting:  https://developers.google.com/analytics/devguides/collection/android/v3/
Advance : https://developers.google.com/analytics/devguides/collection/android/v2/advanced

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

No comments:

Post a Comment