Reputation: 83
i want to attach google analytics google analytics in my app.
i'm following how to adding google analytics in the google GA homepages. (https://developers.google.com/analytics/devguides/collection/android/v4/#manifest)
but I have an error in ApplicationTracker.java
in R.xml.global_tracker and R.xml.ecommerce_tracker, xml have a red line(have an error)
and error content is "xml cannot be resolved or is not a field"
i'm aleady deal with googling and stackoverfliw searching and so many searching.
and many people say this is about android.R
but i'm not add android. i'm use com.myPackageName.R well.
Can somebody explain this please ?
behind is my code
ApplicationTracker.java
package com.zla.android;
import java.util.HashMap;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.zla.android.R;
public class ApplicationTrackers extends Application{
private static final String PROPERTY_ID = "UA-XXXXXXXX-2";
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>();
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
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>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<string name="com.zla.android.MainActivity">MainActivity</string>
<string name="ga_trackingId">UA-XXXXXXXX-2</string>
</resources>
ecommerce_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<string name="ga_trackingId">UA-XXXXXXXX-2</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zla.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<!-- about google analytics -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:largeHeap="true" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zla.android.SearchBrandActivity"
android:label="search page" />
<activity
android:name="com.zla.android.SearchFrameActivity"
android:label="search page" />
<activity
android:name="com.zla.android.ListActivity"
android:label="list page"
android:parentActivityName="com.zla.android.MainActivity" >
</activity>
<activity
android:name="com.zla.android.ListActivity_WishList"
android:label="list page"
android:parentActivityName="com.zla.android.MainActivity" >
</activity>
<activity
android:name="com.zla.android.DetailActivity"
android:label="detail page"
android:parentActivityName="com.zla.android.ListActivity" />
<!-- about google analytics -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.zla.android.ApplicationTrackers"
android:label="google analytics"
android:parentActivityName="com.zla.android.MainActivity" />
</application>
</manifest>
Upvotes: 6
Views: 7296
Reputation: 1561
If you are not included the configuration file 'google-services.json' inside the 'app' folder or the already exists configuration file don't contain supporting code for Google Analytic, R.xml.global_tracker will lead to error.
NB: If you have already included the 'google-services.json' for GCM or other services, don't ignore this. Because you have to rebuild the 'google-services.json' by including the Analytic. Otherwise, error will not be solved.
For creating configuration file please click "Get a configuration file" button inside analytic developer document of click the below link.
Also, use the following dependencies Inside Project Gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta2'
classpath 'com.google.gms:google-services:2.0.0-beta2'
}
Inside App Gradle
dependencies {
compile 'com.google.android.gms:play-services-analytics:8.4.0'
}
apply plugin: 'com.google.gms.google-services'
Upvotes: 0
Reputation: 14929
The brute force solution is to simply provide the actual analytics code.
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker tracker = analytics.newTracker("UA-XXXXXXXXX-X");
rather than following the android conventional procedure
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker tracker = analytics.newTracker(R.xml.global_tracker);
It's a pretty savage way of fixing the issue but it surely works because the newTracker() method only needs the actual string value of the Google Analytics tracking code.
The official reference document even have the same paradigm example.
Upvotes: 1
Reputation: 668
As per documentation it is mandatory to create file
Create global_tracker.xml
Create the file app/src/res/xml/global_tracker.xml with the following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ga_trackingId" translatable="false">${YOUR_TRACKING_ID}</string>
</resources>
Replace ${YOUR_TRACKING_ID} with your tracking ID.
Upvotes: 1
Reputation: 1296
I had the same problem but i had no issues with google-services.json
or the package name
. However, updating this in project level build.gradle
fixed it for me.
'com.google.gms:google-services:1.5.0-beta2'
'com.google.gms:google-services:2.1.0'
Upvotes: 0
Reputation: 21
Don't forget to add this
Add the dependency to your project-level build.gradle:
classpath 'com.google.gms:google-services:1.5.0-beta2'
Add the plugin to your app-level build.gradle:
apply plugin: 'com.google.gms.google-services'
I forgot to add this in my code, when I sync, and it's no error.
Upvotes: 2
Reputation: 61
I had the same error and this solution (adding a global_tracker.xml) file in a new res/xml directory worked. Clean and rebuild. Note, in the solution the poster has capitalised the G in the global_tracker.xml filename. This should be lowercase. Link to solution is here
Upvotes: 0
Reputation: 21
I had the same error. Your Client Package name in google-services.json file and package name in your app must be same. It helped me.
Upvotes: 2
Reputation: 6245
I had the same error and when i make res/xml folder & rebuild project it's helped to me.
Upvotes: 8