我要实现谷歌Analytics(分析)为Android的我的示例应用程序。我怎样才能查看数据或在服务器和放大器分析的变化;我怎么可以发送自定义变量到服务器端。我应该在那之后怎么办?
公共类TestActivity延伸活动{
GoogleAnalyticsTracker跟踪;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
跟踪= GoogleAnalyticsTracker.getInstance();
//开始在手动调度模式下,跟踪...
tracker.startNewSession(UA-34709489-1,这一点);
tracker.trackPageView(/主屏幕);
// ...替代地,跟踪器可以用一个调度间隔(秒)开始。
//tracker.startNewSession("UA-YOUR-ACCOUNT-HERE,20,这一点);
的setContentView(R.layout.main);
按钮createEventButton =(按钮)findViewById(R.id.NewEventButton);
createEventButton.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
tracker.trackEvent(
点击,//分类
按钮,//行动
点击,//标签
77); // 值
}
});
按钮createPageButton =(按钮)findViewById(R.id.NewPageButton);
createPageButton.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//添加自定义变量这一综合浏览量,与中等的名称和值MobileApp
tracker.setCustomVar(1,导航类型,键,2);
tracker.trackPageView(/ testApplicationHomeScreen);
}
});
按钮quitButton =(按钮)findViewById(R.id.QuitButton);
quitButton.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
完();
}
});
按钮dispatchButton =(按钮)findViewById(R.id.DispatchButton);
dispatchButton.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//手动启动调度,如果不需要跟踪已开始调度
// 间隔。
tracker.dispatch();
}
});
}
@覆盖
保护无效的onDestroy(){
super.onDestroy();
//当它不再需要停止跟踪。
tracker.stopSession();
}
}
解决方案
转寄此:谷歌Analytics(分析)为Android
在细节,除了包括上述链接分析罐子,你需要使用下面的code:
进口android.content.Context;
进口com.google.android.apps.analytics.GoogleAnalyticsTracker;
/ **
* @description谷歌分析此类用于创建谷歌Analytics(分析)
*跟踪器实例。这将跟踪在执行的动作
* 应用。
* /
公共final类GoogleAnalytics {
私有静态GoogleAnalyticsTracker跟踪;
私有静态GoogleAnalytics googleAnalytics = NULL;
私人最终静态int值= -1;
私人最终静态字符串类别=应用程序名称;
/ **
*
* @参数方面
* @返回
* /
公共静态GoogleAnalytics getGoogleAnalyticsInstance(上下文的背景下){
如果(googleAnalytics == NULL){
googleAnalytics =新GoogleAnalytics(上下文);
跟踪= GoogleAnalyticsTracker.getInstance();
tracker.startNewSession( - 输入你的章数这里 - ,10,上下文);
}
返回googleAnalytics;
}
私人GoogleAnalytics(上下文的背景下){
}
/ **
*停止当前会话
* /
公共无效stopSession(){
tracker.stopSession();
googleAnalytics = NULL;
}
/ **
*行动轨迹事件进行
*
* @参数动作
* @参数标签
* /
公共无效trackEvent(字符串操作,字符串标签){
tracker.trackEvent(类别,//分类
行动,//行动
标签,//标签
值);
}
}
现在你需要的地方跟踪事件只要致电:
GoogleAnalytics.getGoogleAnalyticsInstance(本).trackEvent(事件名称,事件说明);
为了让您的应用程序registraion号看到: 注册为谷歌分析
另外的记住调用下面,当你想要一个会话结束:
GoogleAnalytics.getGoogleAnalyticsInstance(本).stopSession();
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();
}
}
解决方案
Refer this: Google Analytics For Android
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");
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();