Google analytics in android

Google analytics in android

Google Analytics provides powerful digital analytics for anyone with a web presence, large or small. It’s one of the most powerful digital analytics solutions available – and it’s free for anyone to use.

Read more about google analytics.

Step 1: Generate track Id.

First, access your Google analytics home page.Then click “Create New Account” button.

Google analytics in android

Select “Mobile App”, then fill all other columns.

Google analytics in android

Finally, click generate trackId button.

after that, it will show your application TrackId.

 

Google analytics in android

Step 2: Download “Google analytics lib”

Click here to download “Google analytics lib”


Step 3:analytics.xml

Create analytics.xml in your layout folder and paste the following code.

<?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 4:Helper.java

Create Helper.java class and paste the following code.

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 Helper{
                   
                      /**
                       * 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()
                                           );
                      }
}

Step 5: MainActivity.java

package com.example.googleanalyticsexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.analytics.tracking.android.EasyTracker;

public class MainActivity extends Activity {

Button btnClickEvent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnClickEvent=(Button)findViewById(R.id.button1);
        btnClickEvent.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Helper.pushbtnClickedEvent(MainActivity.this, "Button Clicked");

}
});
    }

    @Override
    protected void onStart() {
       super.onStart();
       EasyTracker.getInstance(this).activityStart(this);
       Helper.pushOpenScreenEvent(this, "Main Activity");
    
    }
 @Override
 protected void onStop() {
                      // TODO Auto-generated method stub
                      
                      super.onStop();
                       EasyTracker.getInstance(this).activityStop(this); // Add this method
                    
                    
                    
                      
 }

}

Now, your application is ready, simply press the “Button” then, open your Google analytics dashboard.
The dashboard has two type of view that is screen view and event view.

Screen view:

Google analytics in android

Event view:

Google analytics in android

Don’t forget to add these permissions in manifest.xml

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

Download

One Response

Leave a Reply

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

Back to Top