XML error when adding Google Analytics to my application

I want Google Analytics Analytics to use Google Analytics in my application.

I am following how to add Google Analytics to Google 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 contents: "xml cannot be resolved or is not a field"

I am engaged in search and search by search query and searches.

and many say it concerns android.R

but i am not adding android. I use com.myPackageName.R well.

Can someone explain this please?

behind 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> 
+6
source share
8 answers

I had the same error, and when I did the res / xml folder and rebuilt the project, it helped me.

+8
source

I had the same error. The name of your client package in google-services.json must be the same as the name of the package in your application. It helps me.

+2
source

Do not forget to add this

  • Add the dependency to your build.gradle project:

     classpath 'com.google.gms:google-services:1.5.0-beta2' 
  • Add the plugin to your build level:

     apply plugin: 'com.google.gms.google-services' 

I forgot to add this to my code when I sync, and this is not an error.

+2
source

I had the same error and this solution (adding global_tracker.xml file) to the new res / xml file worked. Clean and restore. Notice that in the solution, the poster titled G in the global_tracker.xml file. This should be lowercase. Link to solution here

0
source

If you did not include the โ€œgoogle-services.jsonโ€ configuration file in the โ€œappโ€ folder or if the already existing configuration file does not contain auxiliary code for Google Analytic, R.xml.global_tracker will result in an error.

NB. If you have already enabled google-services.json for GCM or other services, do not ignore this. Because you need to rebuild "google-services.json" by enabling Analytic. Otherwise, the error will not be resolved.

To create a configuration file, click the "Get configuration file" button inside the analytics developer document or click the link below.

https://developers.google.com/mobile/add?platform=android&cntapi=analytics&cnturl=https:%2F%2Fdevelopers.google.com%2Fanalytics%2Fdevguides%2Fcollection%2Fandroid%2Fv4%2Fapp%3Fconfigured%3uet set % 20Analytics

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' 
0
source

I had the same problem, but I had no problems with google-services.json or package name . However, updating this level at the project level build.gradle fixed it for me.

  • classpath 'com.google.gms:google-services:1.5.0-beta2'
  • classpath 'com.google.gms:google-services:2.1.0'
0
source

According to the documentation, it is mandatory to create a file

Create global_tracker.xml

Create the app / src / res / xml / global_tracker.xml file with the following contents:

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

analytics

0
source

The solution to the brute force problem is simply to provide real analytic code.

 GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); Tracker tracker = analytics.newTracker("UA-XXXXXXXXX-X"); 

instead of following the usual Android procedure

 GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); Tracker tracker = analytics.newTracker(R.xml.global_tracker); 

This is a rather wild way to solve the problem, but it certainly works because the newTracker () method only requires the actual string value of the Google Analytics tracking code.

The official background document even has the same paradigm example.

0
source

Source: https://habr.com/ru/post/984685/


All Articles