问题
I run the following code but see nothing in the reporting section of GA.
HashMap<String,String> map = new HashMap<String, String>();
map.put("View start", "MenuActivity");
Application.getGaTracker().send(map);
BUT, when executing a "built-in" event, I do see results. Example:
Application.getGaTracker().send(MapBuilder.createAppView().set(Fields.SCREEN_NAME, "Main").build());
Why is a simple params send isn't working?
EDIT: I use GAv3 SDK for Android.
回答1:
What I missed is the Fields.HIT_TYPE
key and the mandatory params based on the hit type.
Example:
Map<String, String> map = new HashMap<String, String>();
map.put(Fields.HIT_TYPE, com.google.analytics.tracking.android.HitTypes.EVENT);
map.put(Fields.EVENT_CATEGORY, "UX");
map.put(Fields.EVENT_ACTION, "screen_view");
Application.getGaTracker().send(map);
See more here and here for some of the hit types params. Some of the params are mandatory and some optional.
This reminds me very much of javascript API, where everything can be changed and nothing is enforced in the API. Also, the hit type is very implicit.
For conclusion, this basic Tracker.send(...)
API is so implicit and non-descriptive - deserves the definition crap!
I understand if Google keeps this API for backward or cross-language compatibility. What I don't understand is why Google chose not to create a decent descriptive API and deprecate this generic, implicit, non-descriptive, crappy API.
Even the new MapBuilder.createEvent(...)
API doesn't explain what is mandatory and optional, nor enforces it in code.
Another issue is that the GAv3AndroidSdk doesn't give any feedback if something isn't working. Only now did I see that there is a warning in the logcat about a missing param, which only divine beings would understand it's meaning: Missing hit type (&t) parameter
. Why is this a warning and not an ERROR??? If an event was requested to be sent and eventually did not sent, it's an ERROR!!! Also, why not say that the request was not performed??? This logcat feedback is too implicit and elusive.
Rant finished.
回答2:
According to the documentation you can only use supported keys, specified as constants of the Fields
class:
Tracker tracker = GoogleAnalytics.getInstance(this).getTracker("UA-XXXX-Y");
HashMap<String, String> hitParameters = new HashMap<String, String>();
hitParameters.put(Fields.HIT_TYPE, com.google.analytics.tracking.android.HitTypes.APP_VIEW);
hitParameters.put(Fields.SCREEN_NAME, "Home Screen");
tracker.send(hitParameters);
But as they say:
The
MapBuilder
class simplifies the process of building hits and is recommended for most use cases.
回答3:
com.google.analytics.tracking.android.Tracker tracker = GoogleAnalytics.getInstance(YOUR_Activity.this).getTracker("YOUR_ID);
Map<String, String> map = new HashMap<String, String>();
map.put(Fields.HIT_TYPE, com.google.analytics.tracking.android.HitTypes.EVENT);
map.put(Fields.EVENT_CATEGORY, "Home page");
map.put(Fields.EVENT_ACTION, "Trophy");
tracker.send(map);
来源:https://stackoverflow.com/questions/21833193/google-analytics-ga-send-call-with-simple-params-on-android-dont-show-results