Google Analytics导入无法解析

时间:2013-02-26 18:24:01

标签: java android import google-analytics

我正在尝试使用pageviews跟踪Google Analytics,但我一直在导入时遇到错误。我在下面的代码中列出了错误所在的位置。

我还将jar文件放在java构建路径中,并在Android Manifest中添加了两行。

我的问题是如何让下面的代码正确编译。

import com.google.android.apps.analytics.GoogleAnalyticsTracker;  //Error:  "The import com.google.android.apps cannot be resolved"

public class MainMenu extends Activity {

    GoogleAnalyticsTracker tracker;  //Error:  "The import com.google.android.apps cannot be resolved to a type"

    final Context context = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mainmenumain);

        tracker = GoogleAnalytics.getInstance();
        tracker.startSession("UA-38788135-1", this);

        btn1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                tracker.trackPageView("/Categories");  //Error:  "The import com.google.android.apps cannot be resolved to a type"
                Intent intent = new Intent(MainMenu.this, Categories.class);
                startActivity(intent);
            }
        });

        btn2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                tracker.trackPageView("/Highscores");  //Error:  "The import com.google.android.apps cannot be resolved to a type"
                Intent intent = new Intent(MainMenu.this, Highscores.class);
                startActivity(intent);
            }
        });

        btn3.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                tracker.trackPageView("/About");  //Error:  "The import com.google.android.apps cannot be resolved to a type"
                Intent intent = new Intent(MainMenu.this, About.class);
                startActivity(intent);
            }
        });

        btn4.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                tracker.trackPageView("/ComingSoon");  //Error:  "The import com.google.android.apps cannot be resolved to a type"
                Intent intent = new Intent(MainMenu.this, ComingSoon.class);
                startActivity(intent);
            }
        });
    }

enter image description here

2 个答案:

答案 0 :(得分:3)

您正试图在Google Analytics中跟踪按钮点击次数,但不使用onClick()内的trackPageView来跟踪按钮事件

btn1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                tracker.trackPageView("/Categories");  //Error:  "The import com.google.android.apps cannot be resolved to a type"
                Intent intent = new Intent(MainMenu.this, Categories.class);
                startActivity(intent);
            }
        });

将此代码用于onClick()内部的按钮事件跟踪,而不是上面的onClick()代码

GaTracker.trackEvent("Your Buttons Category", "Your event name", "", 0L);
GAServiceManager.getInstance().dispatch();

声明

private Tracker GaTracker;
private GoogleAnalytics GaInstance;

在onCreate()方法中使用

GaInstance = GoogleAnalytics.getInstance(this);
GaTracker  = GaInstance.getTracker("YOUR UA-Here");
GaTracker.sendView("/YourActivity"); // Include this line if you want to track page view

答案 1 :(得分:2)

GoogleAnalyticsTracker用于libGoogleAnalyticsV1.jar,但您使用的是最新版本的libGoogleAnalyticsV2.jar。要在libGoogleAnalyticsV2中跟踪页面视图,请使用以下代码 声明

private Tracker GaTracker;
private GoogleAnalytics GaInstance;

在onCreate()方法中

GaInstance = GoogleAnalytics.getInstance(this);
GaTracker  = GaInstance.getTracker("YOUR UA-Here");
GaTracker.sendView("/YourActivity");