GoogleAnalytics.getInstance(this) 没有响应 [英] GoogleAnalytics.getInstance(this) not respond

查看:12
本文介绍了GoogleAnalytics.getInstance(this) 没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 android 应用程序,我向它添加了 Google Analytics Tracker 并且它工作正常(我可以在分析面板中看到视图).

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

有时没有响应.

为什么会发生这种情况,我该如何解决?

Why is this happen and how can I fix it?

我添加了可选的 analytics_global_config 并且它仍然发生

I added optional analytics_global_config and it still happens

<meta-data
     android:name="com.google.android.gms.analytics.globalConfigResource"
     android:resource="@xml/analytics_global_config" />

analytics_global_config.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string name="ga_appName">HebConvertor</string>
     <string name="ga_appVersion">1.0</string>
     <string name="ga_logLevel">verbose</string>
     <integer name="ga_dispatchPeriod">1000</integer>
     <bool name="ga_dryRun">false</bool>
 </resources>

<小时>

我的应用程序:

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<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

      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:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
    <!-- tools:ignore="TypographyDashes" -->
    <!-- The apps Analytics Tracking Id -->
    <string name="ga_trackingId">XX-XXXXXXXX-X</string>

    <!-- Percentage of events to include in reports -->
    <string name="ga_sampleFrequency">100.0</string>

    <!-- Enable automatic Activity measurement -->
    <bool name="ga_autoActivityTracking">true</bool>

    <!-- catch and report uncaught exceptions from the app -->
    <bool name="ga_reportUncaughtExceptions">true</bool>

    <!-- How long a session exists before giving up -->
    <integer name="ga_sessionTimeout">-1</integer>

    <!-- If ga_autoActivityTracking is enabled, an alternate screen name can be specified to
    substitute for the full length canonical Activity name in screen view hit. In order to
    specify an alternate screen name use an <screenName> element, with the name attribute
    specifying the canonical name, and the value the alias to use instead. -->
    <screenName name="com.example.myapplication.MainActivity">MainActivity</screenName>

</resources>

主要活动:

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

            ...
            }

        }
    }

推荐答案

这是 Google Play Services 6.5 的一个已知问题.请参阅 Android GoogleAnalytics getInstance 了解详情.

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

该问题已在 2015 年 3 月 19 日发布的 Google Play Services 7.0 中得到解决.http://developer.android.com/google/play-services/index.htmlhtml

如果您必须使用 Play Services 6.5,解决方法是通过代码而不是 xml 配置 Google Analytics,或者降级到 Google Play Services 6.1.

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

这篇关于GoogleAnalytics.getInstance(this) 没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆