GoogleAnalytics.getInstance(这个)不响应 [英] GoogleAnalytics.getInstance(this) not respond

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

问题描述

我有一个机器人的applcation我加入谷歌分析跟踪它和它的工作原理(我可以看到在分析小组的意见)。

问题是,有时应用程序启动时加载,然后被卡住,而不是响应了。我试图调试它,我发现,当涉及到行

  GoogleAnalytics分析= GoogleAnalytics.getInstance(本);
 

一段时间没有响应。

为什么出现这种情况,我该如何解决这个问题?

编辑:

我添加可选analytics_global_config和它仍然发生

 <元数据
     机器人:名称=com.google.android.gms.analytics.globalConfigResource
     机器人:资源=@ XML / analytics_global_config/>
 

analytics_global_config.xml

 < XML版本=1.0编码=UTF-8&GT?;
 <资源>
     <字符串名称=ga_appName> HebConvertor< /串>
     <字符串名称=ga_appVersion> 1.0< /串>
     <字符串名称=ga_logLevel>详细< /串>
     <整数NAME =ga_dispatchPeriod> 1000℃; /整数GT;
     <布尔名=ga_dryRun>假< /布尔>
 < /资源>
 


MyApplication的:

 进口com.google.android.gms.analytics.GoogleAnalytics;
进口com.google.android.gms.analytics.Tracker;
进口android.app.Application;

公共类MyApplication的扩展应用{

      //下面一行应改为包括正确的属性ID。
      私有静态最后弦乐PROPERTY_ID =XX-XXXXXXXX-X;

      公共枚举TrackerName {
        APP_TRACKER,//跟踪器只用在这个应用程序。
        GLOBAL_TRACKER,//使用由一家公司所有的应用程序跟踪。例如:卷起跟踪。
        ECOMMERCE_TRACKER,//跟踪从一个公司所使用的所有电子商务交易。
      }

      HashMap的< TrackerName,跟踪> mTrackers =新的HashMap< TrackerName,跟踪>();

      公共所有MyApplication(){
        超();
      }

      同步跟踪getTracker(TrackerName trackerId){
            如果(!mTrackers.containsKey(trackerId)){

              GoogleAnalytics分析= GoogleAnalytics.getInstance(本);
              跟踪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);
            }
            返回mTrackers.get(trackerId);
      }
}
 

app_tracker.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<资源的xmlns:工具=htt​​p://schemas.android.com/tool​​s工具:忽略=TypographyDashes>
    <  - 工具:忽略=TypographyDashes - >
    <! - 这些应用分析跟踪ID  - >
    <字符串名称=ga_trackingId> XX-XXXXXXXX-X< /串>

    <! - 事件的百分比,包括在报告 - >
    <字符串名称=ga_sampleFrequency> 100.0< /串>

    <! - 启用自动测量活动 - >
    <布尔名=ga_autoActivityTracking>真< /布尔>

    <! - 捕获和报告从应用程序捕获的异常 - >
    <布尔名=ga_reportUncaughtExceptions>真< /布尔>

    <! - 多长时间会存在放弃之前 - >
    <整数NAME =ga_sessionTimeout> -1 LT; /整数GT;

    <! - 如果ga_autoActivityTracking启用,备用网名可以被指定为
    替代屏幕视图命中全长规范的活动名称。为了
    指定备用网名使用<屏幕名>元素,name属性
    指定的规范名称,值别名使用代替。 - >
    <屏幕名NAME =com.example.myapplication.MainActivity> MainActivity< /屏幕名>

< /资源>
 

MainActivity:

 公共类MainActivity扩展ActionBarActivity {

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);

            如果(savedInstanceState == NULL){
                getSupportFragmentManager()的BeginTransaction()
                        。新增(R.id.container,新PlaceholderFragment())提交()。
            }
            getOverflowMenu();
        }

        @覆盖
        公共无效的OnStart(){
            super.onStart();
            //获取Analytics(分析)跟踪报告应用程序启动和放大器;未捕获的异常等。
            GoogleAnalytics.getInstance(本).reportActivityStart(本);
        }

        @覆盖
        公共无效的onStop(){
            super.onStop();
            //停止分析跟踪
            GoogleAnalytics.getInstance(本).reportActivityStop(本);
        }

        公共静态类PlaceholderFragment扩展片段{

            公共PlaceholderFragment(){
            }

            保护InterstitialAd间质性;

            @覆盖
            公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
               查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);

               //获取跟踪。
               跟踪T =((所有MyApplication)getActivity()getApplication()。)getTracker(TrackerName.APP_TRACKER)。
               //设置的网名。
               t.se​​tScreenName(MainActivity);

            ...
            }

        }
    }
 

解决方案

这是一个已知的问题与谷歌播放服务6.5。请参见的Andr​​oid GoogleAnalytics的getInstance 了解详细信息。

的问题已在谷歌播放服务7.0 这是发布于2015年3月19日。 http://developer.android.com/google/play-services/index.html

如果你必须使用播放服务6.5的解决方法是配置谷歌Analytics(分析)code,而不是XML或降级到谷歌播放服务6.1。

在添加到您的应用程序类下面的code就相当于你的配置清单:

 公共类MyApplication的扩展应用{
  // ...
  私人跟踪mTracker;
  公共同步跟踪getAppTracker(){
    如果(mTracker == NULL){
      GoogleAnalytics分析= GoogleAnalytics.getInstance(本)
      mTracker = analytics.newTracker(XX-XXXXXXXX-X); //你的真实跟踪ID替换
      mTracker.enableAutoActivityTracking(真正的);
      mTracker.enableExceptionReporting(真正的);
      mTracker.setSessionTimeout(-1);

      // mTracker.setSampleRate(100.0d); // 不需要。默认采样率是100%
    }
    返回mTracker;
  }
}
 

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

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


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

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

            ...
            }

        }
    }

解决方案

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

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

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