abcd abcd - 3 months ago 11
Android Question

How to track button click events in android analyitcs api v4

i have few buttons on mainactivity, i want to track each button click

i am using android google analytics api v4

this is the cod i am trying

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName("Home");
t.send(new HitBuilders.AppViewBuilder().build());


here is the button code

public void button1(View v){
//Intent intent = new Intent(this, SplashActivity.class);
Intent intent = new Intent(this, CategoryListActivity.class);
intent.putExtra("name", "button1");
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

}


i have tried adding this

t.send(new HitBuilders.EventBuilder()
.setCategory("Events")
.setAction("Pressed button")
.build());


like this

public void button1(View v){
//Intent intent = new Intent(this, SplashActivity.class);
Intent intent = new Intent(this, CategoryListActivity.class);
intent.putExtra("name", "button1");
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

t.send(new HitBuilders.EventBuilder()
.setCategory("Events")
.setAction("Pressed Start Button")
.build());

}


but it always gives me error

t cannot be resolved


this is my xml file code

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttonshape4"
android:onClick="Button1"
android:text=" Button1 " />

Answer

try this code: first of all create this class to handle of all transaction with Google Analytics:

public class GoogleAnalyticsHelper {

private Tracker mGaTracker = null;
private static String TAG = "GoogleAnalyticsHelper";
private static final String PROPERTY_ID = "UA-xxxxxxxx-x";

public GoogleAnalyticsHelper()    
{
public void init(Context ctx) {
    try {

        if (mGaTracker == null && ctx != null)
        {
            mGaTracker = GoogleAnalytics.getInstance(ctx).newTracker(PROPERTY_ID);
        }
    } catch (Exception e) {
        Log.d(GoogleAnalyticsHelper.TAG,"init, e="+e);
    }
}

public void SendScreenNameGoogleAnalytics(String screenName, Context iCtx)
{
    init(iCtx);

    mGaTracker.setScreenName(screenName);
    mGaTracker.send(new HitBuilders.AppViewBuilder().build());

}


public void SendEventGoogleAnalytics(Context iCtx,String iCategoryId, String iActionId, String iLabelId)
{
    init(iCtx);

        // Build and send an Event.
        mGaTracker.send(new HitBuilders.EventBuilder()
            .setCategory(iCategoryId)
            .setAction(iActionId)
            .setLabel(iLabelId)
            .build());

     }
  }

then add this code to your main Activity:

public class MainActivity extends Activity {

  GoogleAnalyticsHelper mGoogleHelper;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InitGoogleAnalytics();
    SendScreenNameGoogleAnalytics();
}


private void InitGoogleAnalytics()
{           
    mGoogleHelper = new GoogleAnalyticsHelper();        
    mGoogleHelper.init(MainActivity.this);      
}

private void SendScreenNameGoogleAnalytics()
{

    mGoogleHelper.SendScreenNameGoogleAnalytics("MainActivity 1",MainActivity.this);
}

private void SendEventGoogleAnalytics(String iCategoryId, String iActionId,    String iLabelId)
{

   mGoogleHelper.SendEventGoogleAnalytics(MainActivity.this,iCategoryId,iActionId,iLabelId );
}



public void button1(View v){

  SendEventGoogleAnalytics("Main","btn1","button1 clicked" );

    Intent intent = new Intent(this, CategoryListActivity.class);
    intent.putExtra("name", "button1");
    startActivity(intent);
    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}

It's work for me.

Comments