How to set up custom user actions in Firebase Analytics? - android

I am a first timer at Firebase Analytics and I need some help with event logging.
I'd like to organize my user actions like "Screen X, event Y".
What I achieved so far is this:
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, screenName);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, actionName);
firebase.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
and I invoke this method every time I want to log something.
In the Analytics console, this translates to a chart with all the screen names organized by Content_type, like this:
but when I click on one of the items (the tutorial screen "TUT" for example), nothing is organized by actionName. It's all bundled in (not_set) like this:
What am I doing wrong?
Is there a better way to handle this type of stuff?

Solved!
It seems like you need to set an ITEM_ID as well, in addition to CONTENT_TYPE and ITEM_NAME.
Hence I got it working by just adding theĀ ITEM_ID parameter like this:
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, screenName);
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, actionName);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, actionName);
firebase.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
I don't know if it is the right way to handle this kind of event logging but it works for now.
Better solutions are welcome!!

Related

Parcelable array event parameter problem for GTM

I try to create a simple custom tag for catching "purchase" events from Firebase Analytics. My tag config is below.
Tag Type : Function Call
ClassPath : com.xx.xx.GTMProvider
Key - Value
items - {{Items-Custom}}
action_type - FBevent
class_name - NmEventPurchase
{{Items-Custom}} is for the key "items" which was added in Variables section.
The problem is whenever I trigger the code below;
Bundle params = new Bundle();
Bundle item1 = new Bundle();
item1.putString(FirebaseAnalytics.Param.ITEM_ID, "ABCD123");
item1.putString(FirebaseAnalytics.Param.ITEM_NAME, "jeggings");
item1.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "pants");
Bundle item2 = new Bundle();
item2.putString(FirebaseAnalytics.Param.ITEM_ID, "1234");
item2.putString(FirebaseAnalytics.Param.ITEM_NAME, "boots");
item2.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "shoes");
Parcelable[] products = new Parcelable[]{item1,item2};
params.putParcelableArray("items",products);
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE, params);
I got the error below;
E/GoogleTagManager: Internal error - Function call: __md_main Type not supported: class [Landroid.os.Parcelable;
If I just send a string (for example json string) instead of parcelable[] for the items param, everything is just working fine. I need to findout how to add a custom variable which contains a mixed array type in GTM Console.
I am really stuck at this point and there is no any explanations about the issue. Any help would be highly appreciated.
PS: Tag Manager Android dependency : 'com.google.android.gms:play-services-tagmanager:17.0.0'
Thank you all..
first of all and just for consistency, where you put your items into the bundle your should use params.putParcelableArray(FirebaseAnalytics.Param.ITEMS, products);
instead of: params.putParcelableArray("items",products);
I need to findout how to add a custom variable which contains a mixed array type in GTM Console.
Unfortunately in enhanced ecommerce you cannot add custom parameters to your bundles.
About your error, you should be using ArrayList instead of Parcelable. Take a look at the documentation. It should look something like this:
ArrayList items = new ArrayList();
items.add(item1);
items.add(item2);
params.putParcelableArrayList(FirebaseAnalytics.Param.ITEMS, items );
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE, params);

Firebase Analytics: "select_content" events with "Content_type" value: (not set)

I'm logging events with this code:
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, value);
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "my type");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
The problem is that in Firebase console I can see some (400) "my type" events but a lot (1200) of (not set) events.
Why are appearing a lot of (not set) events? I searched in all my project the use of FirebaseAnalytics.Event.SELECT_CONTENT and I'm always setting a value for FirebaseAnalytics.Param.CONTENT_TYPE.
You need to set all the same parameters for each event. Parameters are not a sort of secondary category.
In this case, it seems that you are send other event which is not having CONTENT_TYPE.
I had the same problem. In my case, you can see two events, item_view and item_name. As you can see, both events have exactly same amount of event: 4139.
I was used parameters as secondary category, that means I sent an event only with the item_value parameter and another event with only the item_name parameter. But it was a mistake. As a result, there are a lot of "not set" events.
Check your total count of each event. If it is same, this is the cause. You need to all same parameters for each of event.

GTM sets screenname to (not set)

I am trying to send screen-view hits to Google Analytics using Google Tag Manager, but in the real time view it is showing screenName value as (not set). Actual screenNames are also shown but along with them (not set) is also reported. Can someone please point out what I am missing.
The way I have set it up is to add a new tag to be able to track the screen-view separately:
----> Full config image example here <----
New tag in the GTM container with "Screen View" Track type
Add in "Fields to Set":
Field Name
screenName //Yes, exactly like that
Value
{{Item Name}} // Yes, exactly like that
Triggering: add a new trigger configuration with
Trigger Type
Custom
Trigger fires on
Some events
Event Name equals view_item
Then, on the Android tracking code:
Make sure you track it like this:
String yourScreenNameString = "Screen One"; //This is an example
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, yourScreenNameString);
_firebase.logEvent(FirebaseAnalytics.Event.VIEW_ITEM, bundle);
----> Example of how the GTM looks <----

How to add Firebase custom events for analytics?

I've seen some questions and answers about custom events for firebase analytics, but i just wanted to ask you a straight question so you can give me a straight answer :)
So, this is my method for logging:
#Override
public void logFeatureSelectedEvent(String categoryName, String actionName, String labelName) {
Bundle bundle = new Bundle();
bundle.putString(EventTrackingKeys.EventTypes.CATEGORY, categoryName);
bundle.putString(EventTrackingKeys.EventTypes.ACTION, actionName);
bundle.putString(EventTrackingKeys.EventTypes.LABEL, labelName);
mFirebaseAnalytics.logEvent(EventTrackingKeys.EventAnalyticTypes.FEATURE_SELECTED_EVENT, bundle);
}
with custom event/key names:
String CATEGORY = "category";
String ACTION = "action";
String LABEL = "label";
String FEATURE_SELECTED_EVENT = "feature_selected_event";
So, in my firebase console I only get event name "feature_selected_event", without custom parameter names..
I've seen some answers that i should call setUserProperty() method and register that user property in the User Properties tab of Firebase Analytics.
Is this the right way to implement that method? :
#Override
public void logFeatureSelectedEvent(String categoryName, String actionName, long value) {
Bundle bundle = new Bundle();
bundle.putString(EventTrackingKeys.EventTypes.CATEGORY, categoryName);
bundle.putString(EventTrackingKeys.EventTypes.ACTION, actionName);
bundle.putLong(EventTrackingKeys.EventTypes.VALUE, value);
mFirebaseAnalytics.setUserProperty(EventTrackingKeys.EventTypes.CATEGORY, categoryName);
mFirebaseAnalytics.setUserProperty(EventTrackingKeys.EventTypes.ACTION, actionName);
mFirebaseAnalytics.setUserProperty(EventTrackingKeys.EventTypes.VALUE, value);
mFirebaseAnalytics.logEvent(EventTrackingKeys.EventAnalyticTypes.FEATURE_SELECTED_EVENT, bundle);
}
You can see it in console without any hacks, but it is pretty hidden there.
Go to Firebase Analytics -> Stream View -> Select Events -> Top events -> select_content -> there you go
My code:
Bundle params = new Bundle();
params.putString("invalid_url", urlPart);
mFirebaseAnalytics.logEvent("eventInvalidUrl", params);
The custom parameters will not be shown. Only suggested events with suggested parameters are presented in dashboard.
To see the custom parameters, you have to link your project to Big Query (it's not free).
Also Firebase will not show information if the number of user is less than 10.
If you are still looking for answer,
You need to add custom parameters into the event manually once in the dashboard
Like this https://support.google.com/firebase/answer/7397304?hl=en&ref_topic=6317489
Actually there is no need to link BigQuery for this. But it will take several hours to show up custom parameter once you navigate inside the event.
In Firebase, As i know we have to log the events in < Key,value > pair and then need to log.
Like this :
bundle.putString("yourKey","yourValue");
For Example, I have put the Custom event in MyApp to get the AppOpen time as below.
Bundle params = new Bundle();
params.putString("open_time", TimeStampUtil.getTimeStamp(System.currentTimeMillis()));
mFirebaseAnalytics.logEvent("app_open_time", params);
so, in above example i have create a custom event with name app_open_time and put the value with key as open_time.
Try in this way and for more info refer this : https://firebase.google.com/docs/analytics/android/events
Not only do you need to do what Muhammad Riyaz says, you also need to get your user count up above the "threshold". In my case, installing my app on a dozen simulator versions pushed my user count to 12, and voila, suddenly I have custom event parameter data in my Firebase dashboard.
Similar to #Josef Vancura answer, but in Kotlin:
val params = Bundle()
params.putString("invalid_url", urlPart)
mFirebaseAnalytics.logEvent("eventInvalidUrl", params)

Screen tracking support - Firebase 9.8

According Firebase Android SDK Release Notes with 9.8 update we have screen tracking support with android screens and activities... The documentation says that this event works like that:
mFirebaseAnalytics.setCurrentScreen(activity,class_name,class_override_name);
In my case, I don't need overrides class name and I send null value... But i'm waiting 48h and my firebase analytics console doesn't show info about this event, any ideas?
Thanks in advance!
Another very important thing that I've noticed only after two days of intensive struggling: the setCurrentScreen method MUST be called on the UI thread.
I was only able to see that after looking for a light in the Firebase decompiled code:
#MainThread
#Keep
public final void setCurrentScreen(#NonNull Activity var1, #Size(min = 1L,max = 36L) #Nullable String var2, #Size(min = 1L,max = 36L) #Nullable String var3) {
//...
}
Whenever this method is called a event of type screen_view is logged.
And keep in mind the Firebase size restrictions. The maximum size of a screen name is 36 characters long.
First I had the same question: where is my event with current screen name on the Firebase dashboard?
I've called method mFirebaseAnalytics.setCurrentScreen(this, "MainActivity", null); with no result.
Thanks to the comment by Benoit I realized that this method indicates the value of implicit parameter that is automatically attached to any event you send.
That means it's not independent event, it's a parameter that will stick to all your events since you set it.
This will be useful if you have changing screens within single Activity. For example when you have multiple fragments with one hosting Activity. And you call this method in each fragment in onResume().
If you want to have distinct metric with the name of your screen - fire explicitly a new event for that.
Bundle params = new Bundle();
params.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "screen");
params.putString(FirebaseAnalytics.Param.ITEM_NAME, "MainActivity");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM, params);
val bundle = Bundle()
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, "YOUR SCREEN NAME")
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
Also Firebase Analytic's screen tracking is automatic. No need for explicit separate event tracking.
Sets the current screen name, which specifies the current visual context in your app. This helps identify the areas in your app where users spend their time and how they interact with your app.
Note that screen reporting is enabled automatically and records the class name of the current Activity for you without requiring you to call this function. The class name can optionally be overridden by calling this function in the onResume callback of your Activity and specifying the screenClassOverride parameter.
If your app does not use a distinct Activity for each screen, you should call this function and specify a distinct screenName each time a new screen is presented to the user.
The name and classOverride remain in effect until the current Activity changes or a new call to setCurrentScreen is made. I will try to add this method to onResume Method. I do not know the result but i will share my experience.
firebaseAnalytics.setCurrentScreen(activity,screeenName,activity.getClass().getSimpleName());
firebaseAnalytics.setMinimumSessionDuration(100L);
params = new Bundle();
params.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "screen");
params.putString(FirebaseAnalytics.Param.ITEM_NAME, screeenName);
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM, params);
Try using setCurrentScreen as well as manual event fire as firebase doesn't send data immediately to the console...but if event is fired up..all the remaining data is sent to firebase..
Just call that method in onResume(), and check the tracking through DebugView. it worked for me.
Check out the documentation.

Categories

Resources