Matt
Matt

Reputation: 3912

Google Analytics import cannot be resolved

I am trying to track pageviews with Google Analytics but I keep getting an error on the import. I have listed in the code below where the errors are.

I also have put the jar file in the java build path and added the two lines in the Android Manifest.

My question is how to get the below code to compile correctly.

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

Upvotes: 1

Views: 7286

Answers (2)

Sakthimuthiah
Sakthimuthiah

Reputation: 2636

You are trying to track Button clicks in google analytics but don't use trackPageView inside onClick() to track button events

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

Use this code for Button events tracking inside onClick() instead of above onClick() code

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

Declare

private Tracker GaTracker;
private GoogleAnalytics GaInstance;

Inside onCreate() method use

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

Upvotes: 3

Sakthimuthiah
Sakthimuthiah

Reputation: 2636

GoogleAnalyticsTracker is used in libGoogleAnalyticsV1.jar but you are using libGoogleAnalyticsV2.jar which is the latest version. To track a page view in libGoogleAnalyticsV2 use the following code Declare

private Tracker GaTracker;
private GoogleAnalytics GaInstance;

Inside onCreate() method

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

Upvotes: 2

Related Questions