Reputation: 21
I'm newcomer in Google Analytics. I have a problem when I initialize a tracker. I want to initialize a tracker using EasyTracker.getInstance(this);
in onCreate()
method but when I run it and access the menu that already set the tracker, I've got warn message in logcat, that is "missing tracking id (&id) parameter."
. But if I use GoogleAnalytics.getInstance(this).getTracker("UA-xxxxxxxx-y");
to initialize the tracker, the message doesn't appear. I've set EasyTracker.getInstance(this).activityStart(this);
in onStart()
method and EasyTracker.getInstance(this).activityStop(this);
in onStop()
method.
This is my analytics.xml. Btw, I've already set the id.
<?xml version="1.0" encoding="utf-8"?>
<!-- The comicjp app Analytics Tracking Id -->
<string name="ga_trackingId">UA-xxxxxxxx-y</string>
<!-- Enable automatic activity tracking -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtException">true</bool>
<!-- Set the log level to verbose -->
<string name="ga_logLevel">verbose</string>
<!-- The interval of time after all the collected data
should be sent to the server, in seconds -->
<integer name="ga_dispatchPeriod">30</integer>
<!-- The screen name that will appear in reports -->
<string name="com.example.gaaplication.MainActivity">MainActivity</string>
Why the warn message always appear if I'm using EasyTracker.getInstance(this);
? How to use EasyTracker.getInstance(this);
properly?
Upvotes: 1
Views: 3354
Reputation: 385
For google analytics v4, you should add global_tracker.xml. I think Google missing it in their Github repo.
<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>
<bool name="ga_debug">true</bool>
<string name="ga_logLevel">verbose</string>
<string name="ga_dryrun">true</string>
<!-- The screen names that will appear in reports -->
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-xxxxxxx-1</string>
</resources>
Upvotes: 0
Reputation: 11555
In v4 I was getting GAv4: Discarding hit. Missing tracking id parameter
because instead of:
GoogleAnalytics.getInstance(context).newTracker(R.xml.app_tracker);
I've added
GoogleAnalytics.getInstance(context).newTracker(R.string.ga_trackingId);
Note the difference xml.app_tracker vs string.ga_trackingId!
Use xml.app_tracker
configuration file!
Upvotes: 3
Reputation: 13647
I had a similar issue, very strange, but i only solved by seeing the source code of the EasyTracker class. It fetches the is like this, using the resource name:
String s = mParameterFetcher.getString("ga_trackingId");
AND if If you're using Android Maven plugin with package renaming, it will mess up (read this later: http://www.piwai.info/renaming-android-manifest-package/)...So in order to fix it, you have to READ the link + call the method EasyTracker.setResourcePackageName()
// ensures that EasyTracker uses the overrider package.
String appPackageName = getPackageNameAccordingToWhatIsWrittenInThatBlogPost();
// Log.i(TAG, "loading appPackageName: " + appPackageName);
EasyTracker.setResourcePackageName(appPackageName);
Good luck!
Upvotes: 0
Reputation: 2707
For google analytics you need to register you application here Google Analytics as all mobile application then it wil gives you ga_trackingId id
Now make one analytics.xml fine values folder in analytics file put below content
<?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>
<string name="ga_appName">Location Serivce</string>
<string name="ga_appVersion">1.1.2</string>
<bool name="ga_debug">true</bool>
<item name="ga_dispatchPeriod" format="integer" type="integer">120</item>
<string name="ga_sampleFrequency">90</string>
<bool name="ga_anonymizeIp">true</bool>
<bool name="ga_dryRun">false</bool>
<string name="com.example.HomeActivity">HomeActivity</string>
<string name="com.example.SettingActivity">SettingActivity</string>
Now in home activity put below code for activity display
private Tracker tracker;
private String TAG_Tracker="HomeActivity";
MyApp.getGaTracker().set(Fields.SCREEN_NAME, TAG_Tracker);
tracker = GoogleAnalytics.getInstance(this).getTracker(MyApp.GA_PROPERTY_ID);
HashMap<String, String> hitParameters = new HashMap<String, String>();
hitParameters.put(Fields.HIT_TYPE, "ActivityView");
hitParameters.put(Fields.SCREEN_NAME, TAG_Tracker);
tracker.send(hitParameters);
//for button click and other event fire you need to put below code:
public void onClick(View v) {
tracker.send(MapBuilder.createEvent("UI", "OnClick", "btnAdd", null).build());
}
Now MyApp.java
public class MyApp extends Application {
private static GoogleAnalytics mGa;
private static Tracker mTracker;
/*
* Google Analytics configuration values.
*/
// Placeholder property ID.
public static final String GA_PROPERTY_ID = "UA-XXXXXXXX-X";
// Dispatch period in seconds.
private static final int GA_DISPATCH_PERIOD = 30;
// Prevent hits from being sent to reports, i.e. during testing.
private static final boolean GA_IS_DRY_RUN = false;
// GA Logger verbosity.
private static final LogLevel GA_LOG_VERBOSITY = LogLevel.INFO;
// Key used to store a user's tracking preferences in SharedPreferences.
private static final String TRACKING_PREF_KEY = "trackingPreference";
/*
* Method to handle basic Google Analytics initialization. This call will
* not block as all Google Analytics work occurs off the main thread.
*/
@SuppressWarnings("deprecation")
private void initializeGa() {
mGa = GoogleAnalytics.getInstance(this);
mTracker = mGa.getTracker(GA_PROPERTY_ID);
// Set dispatch period.
GAServiceManager.getInstance().setLocalDispatchPeriod(GA_DISPATCH_PERIOD);
// Set dryRun flag.
mGa.setDryRun(GA_IS_DRY_RUN);
// Set Logger verbosity.
mGa.getLogger().setLogLevel(GA_LOG_VERBOSITY);
// Set the opt out flag when user updates a tracking preference.
SharedPreferences userPrefs = PreferenceManager.getDefaultSharedPreferences(this);
userPrefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(TRACKING_PREF_KEY)) {
GoogleAnalytics.getInstance(getApplicationContext()).setAppOptOut(sharedPreferences.getBoolean(key, false));
}
}
});
}
@Override
public void onCreate() {
super.onCreate();
initializeGa();
}
/*
* Returns the Google Analytics tracker.
*/
public static Tracker getGaTracker() {
return mTracker;
}
/*
* Returns the Google Analytics instance.
*/
public static GoogleAnalytics getGaInstance() {
return mGa;
}
}
you need to define this myApp.java file in manifiest file like
<application
android:name="com.example.MyApp"
Upvotes: 1
Reputation: 6201
you need to set Own id here
<!-- The comicjp app Analytics Tracking Id -->
<string name="ga_trackingId">place your tracking id here</string>
Upvotes: 1