gpt4 book ai didi

android - 如何在 android analytics api v4 中跟踪按钮点击事件

转载 作者:行者123 更新时间:2023-11-29 15:55:27 27 4
gpt4 key购买 nike

我在 Main Activity 上有几个按钮,我想跟踪每个按钮的点击。我正在使用 Android Google Analytics API v4。

这是我的代码:

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());

这是按钮代码:

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 button")
.build());

像这样

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());

}

但它总是给我错误:

t cannot be resolved

这是我的 xml 文件代码:

<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 " />

最佳答案

试试这个代码:首先创建此类来处理与 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());

}
}

然后将此代码添加到您的主要 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);
}

这对我有用。

关于android - 如何在 android analytics api v4 中跟踪按钮点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28321759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com