简体   繁体   中英

Google Analytics v4 Cast Error in my android app

EDIT: I fixed error. But still I can not see real time visitors on Google Analytics window. I check three times my tracking id. It is correct, there is no error in code. But why I cant stil my visitors? Min 1 visitor should be in real time visitor area. Because i'm on my app.

I want to use google analytics v4 for my application. I add a class according to google doc.

GoogleAnal.java

public class GoogleAnal extends Application {
    public GoogleAnal() {
        super();
        }
    public enum TrackerName {
        APP_TRACKER, // Tracker used only in this app.
    }

    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();


    synchronized Tracker getTracker(TrackerName trackerId) {
        if (!mTrackers.containsKey(trackerId)) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            if(trackerId == TrackerName.APP_TRACKER){
                Tracker t = analytics.newTracker(R.xml.app_tracker);
                mTrackers.put(trackerId, t);
            }         
        }
        return mTrackers.get(trackerId);
    }

}

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
....
....
....
//Below line gives Error: 
//11-26 09:52:35.005: E/AndroidRuntime(2239): Caused by: java.lang.ClassCastException:    android.app.Application cannot be cast to com.xxx.helloworld.GoogleAnal

((GoogleAnal) getApplication()).getTracker(GoogleAnal.TrackerName.APP_TRACKER);

}

    @Override
    protected void onStart() {
        super.onStart();
        GoogleAnalytics.getInstance(this).reportActivityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        GoogleAnalytics.getInstance(this).reportActivityStop(this);
    }

Where I'm doing wrong? What should I write instead of ((GoogleAnal) getApplication()).getTracker(GoogleAnal.TrackerName.APP_TRACKER);

If my way is all wrong, can you give some suggestions?

OPTION 1: Have you added GoogleAnal in your manifest??

<application
    android:name="com.xxx.helloworld.GoogleAnal"
    ...

OPTION 2: this is my getTracker method inside my Application:

synchronized Tracker getTracker(TrackerName trackerId) {

    if (!mTrackers.containsKey(trackerId)) {

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        analytics.setLocalDispatchPeriod(15);
        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
        Tracker t = analytics.newTracker(PROPERTY_ID);
        t.setAppName(getResources().getString(R.string.app_name));
        t.enableAdvertisingIdCollection(true);
        t.enableAutoActivityTracking(true);
        t.enableExceptionReporting(true);
        mTrackers.put(trackerId, t);
    }
    return mTrackers.get(trackerId);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM