Reputation: 453
I am trying to implement google analytics for one of my android apps. I am totally new for analytics and android app development. I thought of trying the examples given in the google developers site. When trying to compile their code, I am getting the error pointing the analytics application java file, mTracker = analytics.newTracker(R.xml.global_tracker);
line, .xml is highlighted. I posted the whole code here.
This is AnalyticsApplication.java
package com.google.samples.quickstart.analytics;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.Tracker;
public class AnalyticsApplication extends Application {
private Tracker mTracker;
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
mTracker = analytics.newTracker(R.xml.global_tracker);
}
return mTracker;
}
}
My Global_tracker.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--Replace placeholder ID with your tracking ID-->
<string name="ga_trackingId">UA-XXXXXXXX-X</string>
<!--Enable automatic activity tracking-->
<bool name="ga_autoActivityTracking">true</bool>
<!--Enable automatic exception tracking-->
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>
My manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.samples.quickstart.analytics">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name=".AnalyticsApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<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>
</application>
</manifest>
I have already spent two complete days in this issue, and I need your suggestion to go forwards.
Thanks.
Upvotes: 15
Views: 11487
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: 478
I also faced the same issue with play service library version 10.2.1 for gcm and google analytics and issue is simply fixed by adding global_tracker.xml as discussed in the @Hussein El Feky Answer but only that fix is not solved my issue also i've added two more lines in strings.xml:
<string name="global_tracker">UA-XXXXXXXX-XX</string>
<string name="gcm_defaultSenderId">PROJECT-NO</string>
Upvotes: 0
Reputation: 383
I had the similar issue when was preforming downgrade to lower version from
com.google.gms:google-services:3.0.0
to
com.google.gms:google-services:1.3.1
(Do not ask why(it was an assumption in order to fix CI build script in a hurry))
So, make sure that you did not made downgrade changes
Upvotes: 0
Reputation: 35
Check if you don't forget to put
apply plugin: 'com.google.gms.google-services'
in app level build.gradle
Upvotes: 2
Reputation: 238
I was facing the similar issue.
Use classpath com.android.tools.build:gradle:1.2.3
. That solved my problem.
Upvotes: -2
Reputation: 6707
You need to create a new folder in your res folder called xml
and move your file to that folder. Also make sure you call the layout name correctly because in your question, you wrote Global_tracker.xml
instead of global_tracker.xml
. This is so important.
Hope that helps.
Upvotes: 16