在Android中,如何在按钮点击时使用Google Analytics事件跟踪

前端之家收集整理的这篇文章主要介绍了在Android中,如何在按钮点击时使用Google Analytics事件跟踪前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的目标是使用Google Analytics事件跟踪计算按钮点击次数.

如何计算按钮点击次数以及哪个用户(和设备)点击了此按钮?

我正在使用此代码

public class TestActivity extends Activity {
GoogleAnalyticsTracker tracker;
Button clickBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    tracker = GoogleAnalyticsTracker.getInstance();
    tracker.startNewSession("UA-XXXXXXXX-1",30,this);
    tracker.setDebug(true);

    clickBtn = (Button) findViewById(R.id.click);
    setContentView(R.layout.main);
    final Button createEventButton = (Button) findViewById(R.id.NewEventButton);

    createEventButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tracker.trackEvent("Clicks",// Category
                    "Button",// Action
                    "" + clickBtn,// Label
                    77); // Value
            tracker.trackEvent("Clicks","" + createEventButton,"Easy",1);
            tracker.trackEvent("Completions","Game-Deaths","Hard-Level-One",15);
            tracker.trackEvent("Die"," Two",15);
            tracker.trackTransactions();
        }
    });

    Button createPageButton = (Button) findViewById(R.id.NewPageButton);
    createPageButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Add a Custom Variable to this pageview,with name of "Medium"
            // and value "MobileApp"
            tracker.setCustomVar(1,"Medium","Mobile App");
            // Track a page view. This is probably the best way to track
            // which parts of your application
            // are being used.
            // E.g.
            // tracker.trackPageView("/help"); //to track someone looking at
            // the help screen.
            // tracker.trackPageView("/level2"); //to track someone reaching
            // level 2 in a game.
            // tracker.trackPageView("/uploadScreen"); //to track someone
            // using an upload screen.
            tracker.trackPageView("/testApplicationHomeScreen");

        }
    });

    Button quitButton = (Button) findViewById(R.id.QuitButton);
    quitButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    Button dispatchButton = (Button) findViewById(R.id.DispatchButton);
    dispatchButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Manually start a dispatch,not needed if the tracker was
            // started with a dispatch
            // interval.
            tracker.dispatch();
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // Stop the tracker when it is no longer needed.
    tracker.stopSession();
}
}

解决方法

在GA控制台中,您可以获得多个号码. (参与 – >活动)

让我们通过示例解释,如果您将此行放在按钮单击中:

tracker.trackEvent("Completions",15);

用户单击此按钮两次.

您将获得此统计信息:

>总事件= 2 // 2次点击
> Unique Event = 1 // 1个独特的来源
>总值= 30 //用户点击两次,所以15 15 = 30
>平均值= 15 //总值除以事件总数

希望你能清楚.

原文链接:https://www.f2er.com/android/308704.html

猜你在找的Android相关文章