Google Analytics V4 - String xml configuration nam

2019-01-23 22:07发布

问题:

I am trying to include Google Analytics to my Android project. Last time i used it was V2 and worked well.

Now i am having issue with this

11-10 20:15:35.493 1957-1957/com.xxx W/GAv4: Int xml configuration name not recognized: ga_sessionTimeout
11-10 20:15:35.508 1957-1957/com.xxx W/GAv4: Bool xml configuration name not recognized: ga_autoActivityTracking
11-10 20:15:35.508 1957-1957/com.xxx W/GAv4: String xml configuration name not recognized: ga_trackingId

global_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="TypographyDashes">
    <integer name="ga_sessionTimeout">300</integer>
    <bool name="ga_autoActivityTracking">true</bool>
    <string name="ga_trackingId">UA-10009718-55</string>

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

    <!-- how often the dispatcher should fire -->
    <integer name="ga_dispatchPeriod">30</integer>

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

    <screenName name=".MainActivity">MainActivity</screenName>

</resources>

app_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="TypographyDashes">
    <string name="ga_trackingId">UA-10009718-55</string>
    <bool name="ga_autoActivityTracking">true</bool>
    <bool name="ga_reportUncaughtExceptions">true</bool>
    <string name="ga_appName">GoogleAnalyticsApp</string>
    <string name="ga_appVersion">1.1.3</string>
    <bool name="ga_debug">true</bool>
    <item name="ga_dispatchPeriod" format="integer" type="integer">120</item>
    <bool name="ga_anonymizeIp">true</bool>
    <bool name="ga_dryRun">false</bool>
    <string name="ga_sampleFrequency">100.0</string>
    <integer name="ga_sessionTimeout">-1</integer>
    <string name="com.xxx">Sounds</string>

</resources>

Class

package com.xxx;


import java.util.HashMap;
import android.app.Application;
import android.util.Log;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;



public class GoogleAnalyticsApp extends Application {

    private static final String PROPERTY_ID = "UA-10009718-55";

    public static int GENERAL_TRACKER = 0;

    public enum TrackerName {
        APP_TRACKER, GLOBAL_TRACKER, ECOMMERCE_TRACKER,
    }

    public HashMap mTrackers = new HashMap();

    public GoogleAnalyticsApp() {
        super();
    }

    public synchronized Tracker getTracker(TrackerName appTracker) {
        if (!mTrackers.containsKey(appTracker)) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            Tracker t = (appTracker == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID) : (appTracker == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.app_tracker) : analytics.newTracker(R.xml.ecommerce_tracker);


           // Tracker t = analytics.newTracker(PROPERTY_ID);
            mTrackers.put(appTracker, t);
        }
        return (Tracker) mTrackers.get(appTracker);
    }
}

Added necessary lines to AndroidManifest too.

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

<!-- Optionally, register AnalyticsReceiver and AnalyticsService to support background
     dispatching on non-Google Play devices -->
<receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
    </intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.AnalyticsService" android:enabled="true" android:exported="false" />

<!-- Optionally, register CampaignTrackingReceiver and CampaignTrackingService to enable
             installation campaign reporting -->
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver" android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />

When i enable Dry Run i see in log that it collects ID and other info for sending. But when disabled nothing happens.

回答1:

Don't know if it's too late or not, but I had the same things as you, since I was following the tutorial from Google from here and another one here.

However, when testing, I found out that a couple of services were not being defined in the Manifest file, and I found the solution for that here, as you might have also since you are already doing that correctly.

Now, for the part that might be missing, I have two files defined in my res/xml folder, app_tracker.xml and global_tracker.xml.

On the first one, app_tracker.xml I have defined the following:

<!-- Replace placeholder ID with your tracking ID -->
<string name="ga_trackingId" tools:ignore="TypographyDashes" translatable="false">UA-????????-?</string>

<!-- Percentage of events to include in reports -->
<string name="ga_sampleFrequency" translatable="false">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>

<!-- Screen names Override -->
<screenName name="com.example.analytics.MainActivity">Main Screen</screenName>
<!-- other screens -->

And I am using app_tracker.xml when declaring it on the Application class, e.g.

public synchronized Tracker getDefaultTracker() {
    if (mTracker == null) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        mTracker = analytics.newTracker(R.xml.app_tracker);
    }
    return mTracker;
}

As for the global_tracker.xml, I have the following:

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

<!-- how often the dispatcher should fire -->
<integer name="ga_dispatchPeriod">60</integer>

<!-- Send hits to Google Analytics. true = don't send -->
<bool name="ga_dryRun">false</bool>

And I use it by defining it on the Manifest file, such as:

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

And with this, I do not have those configuration name not recognized warnings and I have the hits being received on the Analytics Overview

Hope I was able to help :)