Monday, November 18, 2013

Google Analytics Event Tracking on button click from Android Application

Integrate Google Analytics Event Tracking on button click from Android Application

Hi folks this post about how to track from our android application button click event or How many time opend our application we can track..etc..

First you need to create Goolge analytics account and then create one application get track app id.
Below see how to create Googel analytics account.
Google Analytics site
Step 1
After create account . Create new application get track id
Step 2

Select Mobile app tab.Check the screen below


Step 3 ;
Get tracking ID



Now come to Android.How to Configure google analytics into our android application.
Download latest Google Analytics Jar  and put this jar into your lib folder


Step 1

Goto res/values folder ->Create file name called  analytics.xml . Then paste this code into that file.

Paste this code into analytics.xml file. and add your track id here
<?xml version="1.0" encoding="utf-8" ?>



<resources>

  <!--Replace placeholder ID with your tracking ID-->

  <string name="ga_trackingId">UA-XXXX-Y</string>



  <!--Enable automatic activity tracking-->

  <bool name="ga_autoActivityTracking">true</bool>



  <!--Enable automatic exception tracking-->

  <bool name="ga_reportUncaughtExceptions">true</bool>

</resources>


Step 2

Add following permission into your android manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Step 3
Then come to activity . Add click event in btn click and track page event add in onStart() and onStop method.
MainActivity .class
<?xml version="1.0" encoding="utf-8" ?>

package com.example.googleanalyticssample;
 
 
import com.google.analytics.tracking.android.EasyTracker;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
                    Button btnClick;
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                                         super.onCreate(savedInstanceState);
                                         setContentView(R.layout.activity_main);
                                         btnClick = (Button)findViewById(R.id.button1);
                                         btnClick.setOnClickListener(new OnClickListener() {
                                                             
                                                             @Override
                                                             public void onClick(View v) {
                                                             
                                                                                 Utils.pushbtnClickedEvent(MainActivity.this, "Button Clicked");
                                                             }
                                         });
                    }
 
                    @Override
                    public boolean onCreateOptionsMenu(Menu menu) {
                                         // Inflate the menu; this adds items to the action bar if it is present.
                                         getMenuInflater().inflate(R.menu.main, menu);
                                         return true;
                    }
 
                    
                    
                    @Override
                       protected void onStart() {
                          super.onStart();
                          EasyTracker.getInstance(this).activityStart(this); 
                          Utils.pushOpenScreenEvent(this, "Main Activity Opened");
                         
                       }
                    @Override
                    protected void onStop() {
                                         // TODO Auto-generated method stub
                                           
                                         super.onStop();
                                          Utils.pushCloseScreenEvent(this, "Main Activity Closed");
                                          EasyTracker.getInstance(this).activityStop(this); // Add this method
                                         
                                         
                                         
                                           
                    }
 
}



  

Utils.class
this class contain track btn event and page event method.
package com.example.googleanalyticssample;
import android.content.Context;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.google.tagmanager.DataLayer;
import com.google.tagmanager.TagManager;
public class Utils {
                   
                      /**
                       * Push an "openScreen" event with the given screen name. Tags that match that event will fire.
                       */
                     private static Tracker tracker;
                      public static void pushOpenScreenEvent(Context context, String screenName) {
                                          
                          // Instantiate the Tracker
                                           tracker =  EasyTracker.getInstance(context);
                                           tracker.set(Fields.SCREEN_NAME, screenName);
                                         // Send a screenview.
                                           tracker.send(MapBuilder
                                             .createAppView()
                                             .build()
                                           );
                      }
                     
                      /**
                       * Push an "Button clicked" event with the given screen name. Tags that match that event will fire.
                       */
                      public static void pushbtnClickedEvent(Context context, String clickE) {
                                           tracker =  EasyTracker.getInstance(context);
                                         // Values set directly on a tracker apply to all subsequent hits.
                                           tracker.set(Fields.SCREEN_NAME, "Home Screen");

                                           // This screenview hit will include the screen name "Home Screen".
                                           tracker.send(MapBuilder.createAppView().build());

                                           // And so will this event hit.
                                           tracker.send(MapBuilder
                                             .createEvent("UI", "click", "my btn clicked", null)
                                             .build()
                                           );
                      }
                      /**
                       * Push a "closeScreen" event with the given screen name. Tags that match that event will fire.
                       */
                      public static void pushCloseScreenEvent(Context context, String screenName) {
                                        
                          // Instantiate the Tracker
                                           tracker =  EasyTracker.getInstance(context);
                                           tracker.set(Fields.SCREEN_NAME, screenName);
                                         // Send a screenview.
                                           tracker.send(MapBuilder
                                             .createAppView()
                                             .build()
                                           );
                      }
}



Click the button then u can able to see in analytics page how many time you clicked button.

Analytics page report

In report you can able to see event name and whcih categry and action. then screen name


Screen view report


*********************
if you have more doubt see official document  HERE.

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...