如何在 android 应用程序中实现 Google Analytics? [英] How to implement Google Analytics in android application?

查看:17
本文介绍了如何在 android 应用程序中实现 Google Analytics?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Android 中为我的示例应用实施 Google Analytics.如何查看服务器和服务器中的数据或分析变化如何将自定义变量发送到服务器端.之后我该怎么办?

I want to implement Google Analytics for my sample app in Android. How can I view the data or analytic changes in server & how can I send the custom variables to server side. What should I do after that?

public class TestActivity extends Activity {

  GoogleAnalyticsTracker tracker;

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

    tracker = GoogleAnalyticsTracker.getInstance();

    // Start the tracker in manual dispatch mode...
    tracker.startNewSession("UA-34709489-1", this);
    tracker.trackPageView("/HomeScreen"); 
    // ...alternatively, the tracker can be started with a dispatch interval (in seconds).
    //tracker.startNewSession("UA-YOUR-ACCOUNT-HERE", 20, this);

    setContentView(R.layout.main);
    Button createEventButton = (Button)findViewById(R.id.NewEventButton);
    createEventButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        tracker.trackEvent(
            "Clicks",  // Category
            "Button",  // Action
            "clicked", // Label
            77);       // Value
      }
    });

    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, "Navigation Type", "Button click",2);
        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();
  }
}

推荐答案

参考:Android 版 Google 分析

详细来说,除了包含上面链接中的解析jar,还需要使用如下代码:

In details, apart from including the analytic jar from the above link, you need to use the following code:

import android.content.Context;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

/**
 * @description Google analytics This class is used to create Google Analytics
 *              tracker instance. This will track actions performed in the
 *              application.
 */
 public final class GoogleAnalytics {

private static GoogleAnalyticsTracker tracker;
private static GoogleAnalytics googleAnalytics = null;
private final static int VALUE = -1;
private final static String CATEGORY = "Application Name";

/**
 * 
 * @param context
 * @return
 */
public static GoogleAnalytics getGoogleAnalyticsInstance(Context context) {
    if (googleAnalytics == null) {
        googleAnalytics = new GoogleAnalytics(context);
        tracker = GoogleAnalyticsTracker.getInstance();
        tracker.startNewSession(--enter your reg number here--, 10, context);
    }
    return googleAnalytics;

}

private GoogleAnalytics(Context context) {
}

/**
 * Stop current session
 */
public void stopSession() {
    tracker.stopSession();
    googleAnalytics = null;
}

/**
 * Tracks Event of actions performed
 * 
 * @param action
 * @param label
 */
public void trackEvent(String action, String label) {
    tracker.trackEvent(CATEGORY, // Category
            action, // Action
            label, // Label
            VALUE);
}
   }

现在在任何需要跟踪事件的地方只需调用:

Now anywhere you need to track an event just call:

GoogleAnalytics.getGoogleAnalyticsInstance(this).trackEvent("Event Name", "Event Desc");

要获取您的申请注册号,请参阅:注册 Google Analytics

To get your application registraion number see this: Register for Google Analytics

还记得在您想要结束单个会话时调用以下方法:

Also remeber to call following when you want a single session to end:

GoogleAnalytics.getGoogleAnalyticsInstance(this).stopSession();

这篇关于如何在 android 应用程序中实现 Google Analytics?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆