android analytics v4 most simplified

你离开我真会死。 提交于 2019-12-08 19:16:56

问题


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:

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

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>

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

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

Additional questions:

  • screennames in xml file are needed?

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


回答1:


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

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>

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



来源:https://stackoverflow.com/questions/24804684/android-analytics-v4-most-simplified

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