【问题标题】:How to implement Google Analytics in android application? [closed]如何在 Android 应用程序中实现 Google Analytics? [关闭]
【发布时间】:2012-09-04 02:05:17
【问题描述】:

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

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-analytics


    【解决方案1】:

    请参考:Google Analytics For Android

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

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

    现在,您可以在任何需要跟踪事件的地方调用:

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

    要获取您的应用程序注册号,请参见: Register for Google Analytics

    当你想结束一个会话时,还记得调用以下函数:

    GoogleAnalytics.getGoogleAnalyticsInstance(this).stopSession();
    

    【讨论】:

    • 我试过但没用
    • 你遇到了什么问题?
    • 如何使用此代码?我应该在我的其他活动中调用这个类吗
    • 是的。使用最后提到的代码行。
    • 我已经在 myactivity 中的按钮 onclick 中调用了您的代码,但在那之后我应该怎么做
    猜你喜欢
    • 1970-01-01
    • 2014-09-07
    • 2014-10-25
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多