Android google analytics integration error

后端 未结 2 1798
旧时难觅i
旧时难觅i 2021-01-11 23:37

When i am trying to get tracker in my activity it show error that -this method is undefine \"getactivity()\" in google analytic v4

// Get tracker.
    T         


        
相关标签:
2条回答
  • 2021-01-12 00:17

    If you haven't done so already, create a class MyApplication extends Application for your app, and make sure you add it to your manifest as below (the property that matters here is android:name, I've removed the other xml properties for clarity).

    <application
        android:name="mypackagename.MyApplication"
        ... >
    

    Then, in your MyApplication class, create a method getTracker as per Google Analytics v4 documentation https://developers.google.com/analytics/devguides/collection/android/v4/#tracking-methods

    Then, use

    Tracker t = ((MyApplication) getApplication())
            .getTracker(TrackerName.APP_TRACKER);
    
    0 讨论(0)
  • 2021-01-12 00:22

    I too faced this issue a little while back. To solve it I, as mentioned above, removed getActivity() as well as adding .MyApplication to android:name in the manifest. However I needed to add MyApplication before TrackerName as shown below.

    Tracker t = ((MyApplication) getApplication()).getTracker(MyApplication.TrackerName.APP_TRACKER);
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题