Android的分析v4的最简化Android

2023-09-09 21:11:56 作者:`_ァ絕版侽♂

我想谷歌Analytics(分析)连接到我的应用程序在可能的最简单的方法。我想要实现的分析v4的,因为谷歌表示,他们会强迫升级快,所以我不想做两次同样的工作。

I'm trying to connect google Analytics to my apps in the simplest way possible. I want to implement analytics v4, because google said that they'll force to upgrade to it soon, so I don't want to do twice the same work.

谷歌的教程,在这种情况下,不是非常有效。合并他们说有什么我发现在互联网上,我做了这样的步骤:

Google's tutorial, in this case, is not very efficient. Merging what they said there and what I've found on the internet, I've made this steps:

在Android清单,我添加此权限:

In Android Manifest, I've add this permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

而这一点,在应用程序标记下:

and this, under the Application tag:

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

我不知道第二元的必要性......

I'm not sure of the necessity of the second meta...

我已经写了一个XML文件,命名为analytics.xml(/ RES / XML /)包含:

I've wrote an xml file, named "analytics.xml" (/res/xml/) containing:

<!-- the Local LogLevel for Analytics -->
<string name="ga_logLevel">verbose</string>

<!-- Treat events as test events and don't send to google -->
<bool name="ga_dryRun">false</bool>

<!-- <integer name="ga_sessionTimeout">300</integer> -->

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

<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtExceptions">true</bool>

<string name="ga_trackingId">UA-52900555-1</string>

<!-- The screen names that will appear in reports -->
<screenName name="com.maik.adbconnect.EmpityActivity">
    Adb Activation
</screenName>
<screenName name="com.maik.adbconnect.wid_class">
    Widget call
</screenName>

现在,在EmpityActivity.java,我已经写了这个,在onCreate方法的开头:

Now, in EmpityActivity.java, I've wrote this, at beginning of onCreate method:

GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = analytics.newTracker(R.xml.analytics);
t.send(new HitBuilders.AppViewBuilder().build());

在LogCat中,分析说,这是启动并命中...但在我的分析评审,在用户实时,说零....

In LogCat, analytics says that is started and hits... but in my analytics review, under "real time users", says zero....

其他问题:

需要在XML文件中的昵称?

screennames in xml file are needed?

ga_logLevel和ga_dryRun都去precated?因为logcat的警告我说,这些资源无法识别

ga_logLevel and ga_dryRun are deprecated? Because logcat warn me that those resources are not recognized

推荐答案

设置谷歌分析v4的最简单的方法是使用Java code和Application.onCreate配置单跟踪()。这也是最快的,并配置Analytics(分析)最安全的方式。您code是这样的:

The simplest way to setup Google Analytic v4 is to use Java code and configure single tracker from Application.onCreate(). This is also the fastest and the safest way to configure Analytics. You code would look like this:

public class MyApp extends Application {
    private final String TRACKER_ID = "UA-54994796-4";

    private static GoogleAnalytics analytics;
    private static Tracker tracker;
    public static GoogleAnalytics analytics() {return analytics;}
    public static Tracker tracker() {return tracker;}

    @Override
    public void onCreate() {
        super.onCreate();
        analytics = GoogleAnalytics.getInstance(this);
        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
        tracker = analytics.newTracker("UA-00000000-1");
        tracker.enableExceptionReporting(true);
        tracker.enableAutoActivityTracking(true);
    }
}

从任何地方在您的应用程序发送一个事件,你可以使用:

To send an event from anywhere in your app you can use:

MyApp.tracker().send(new HitBuilders.EventBuilder(
    "some category", " some action").build());

您需要将您的ApplicationManifest.xml一些变化,以及:

You need few changes to your ApplicationManifest.xml as well:

<manifest>
...
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  ...
  <application
        android:name=".MyApp">
    ...
  </application>
  ...
</manifest>

我会使用任何XML配置文件,它容易得到的XML配置错误望而却步。从Java配置让编译器验证你的电话的好处。使用code配置您的分析也更快,因为它避免了XML解析开销。

I would stay away from using any XML configuration files as its easy to get the XML configuration wrong. Configuring from Java have the benefit of the compiler validating your calls. Using code to configure your Analytics is also faster as it avoids the XML parsing overhead.

屏幕名称是可选的。您可以使用类名坚持下去。您的报告将显示类名称,而不是更人性化友好的名称,但它的简单,以避免额外的映射。

Screen names are optional. You can stick with using class names. Your report will show the class names instead of more human friendly names but its simpler to avoid the extra mapping.

DRYRUN和LOGLEVEL不pciated作为谷歌播放服务7.0代$ P $。您正在使用不正确的名称,提供的将它们放置在XML的错误的部分不正确的值。这是与XML配置的问题。目前仅限于运行时验证其容易把事情搞错了,并没有注意到这个问题。

dryRun and logLevel are not depreciated as of Google Play Services 7.0. You are using either incorrect name, provided incorrect value of placed them in the wrong section of the XML. This is the problem with XML configuration. There is only limited runtime validation and its easy to get things wrong and not notice the problem.