1

我正在尝试跟踪pageviewsGoogle 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);
            }
        });
    }

在此处输入图像描述

4

2 回答 2

3

您正在尝试在谷歌分析中跟踪按钮点击,但不要在 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
于 2013-04-29T13:32:40.223 回答
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");
于 2013-04-29T13:08:59.367 回答