我在主活动上有几个按钮,我想跟踪每个按钮点击。我使用的是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 " />
答案 0 :(得分:4)
试试这段代码: 首先创建此类以处理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);
}
它为我工作。