GoogleAnalytics.getInstance(this) not respond

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I have an android applcation and I added Google Analytics Tracker to it and it works (I can see the views in the Analytics panel).

The problem is that sometime the application starts to load and then gets stuck and not responds anymore. I tried to debug it and I found out that when it comes to the line

  GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); 

sometime there is no respond.

Why is this happen and how can I fix it?

EDIT:

I added optional analytics_global_config and it still happens

analytics_global_config.xml

HebConvertor1.0verbose1000false

MyApplication:

import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.Tracker; import android.app.Application;  public class MyApplication extends Application  {        // The following line should be changed to include the correct property id.       private static final String PROPERTY_ID = "XX-XXXXXXXX-X";        public enum TrackerName {         APP_TRACKER, // Tracker used only in this app.         GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.         ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.       }        HashMap mTrackers = new HashMap();        public MyApplication() {             super();       }        synchronized Tracker getTracker(TrackerName trackerId) {             if (!mTrackers.containsKey(trackerId)) {                GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);               Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker)                       : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID)                       : analytics.newTracker(R.xml.app_tracker);               mTrackers.put(trackerId, t);             }             return mTrackers.get(trackerId);       } } 

app_tracker.xml:

XX-XXXXXXXX-X100.0truetrue-1MainActivity

MainActivity:

    public class MainActivity extends ActionBarActivity {          @Override         protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             setContentView(R.layout.activity_main);              if (savedInstanceState == null) {                 getSupportFragmentManager().beginTransaction()                         .add(R.id.container, new PlaceholderFragment()).commit();             }             getOverflowMenu();         }          @Override         public void onStart() {             super.onStart();             //Get an Analytics tracker to report app starts & uncaught exceptions etc.             GoogleAnalytics.getInstance(this).reportActivityStart(this);         }          @Override         public void onStop() {             super.onStop();             //Stop the analytics tracking             GoogleAnalytics.getInstance(this).reportActivityStop(this);         }          public static class PlaceholderFragment extends Fragment {              public PlaceholderFragment() {             }              protected  InterstitialAd interstitial;              @Override             public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {                View rootView = inflater.inflate(R.layout.fragment_main, container, false);                 // Get tracker.                Tracker t = ((MyApplication)getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);                // Set screen name.                t.setScreenName("MainActivity");              ...             }          }     } 

回答1:

This is a known issue with Google Play Services 6.5. See Android GoogleAnalytics getInstance for details.

The issue is fixed in Google Play Services 7.0 that was releases on 2015, March 19th. http://developer.android.com/google/play-services/index.html

If you must use Play Services 6.5, the workaround is to either configure Google Analytics from code instead of xml or downgrade to Google Play Services 6.1.

The following code when added to your Application class is equivalent to your manifest configuration:

public class MyApplication extends Application  {   //...   private Tracker mTracker;   public synchronized Tracker getAppTracker() {     if (mTracker == null) {       GoogleAnalytics analytics = GoogleAnalytics.getInstance(this)       mTracker = analytics.newTracker("XX-XXXXXXXX-X"); // Replace with your real tracker id       mTracker.enableAutoActivityTracking(true);       mTracker.enableExceptionReporting(true);       mTracker.setSessionTimeout(-1);        // mTracker.setSampleRate(100.0d); // Not needed. The default sampling rate it 100%     }     return mTracker;    } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!