B4A Library FirebaseAnalytics

This library requires B4A v6+.

Adding support for Firebase analytics:

1. Follow the Firebase integration tutorial: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/#content


2. In Starter service:
B4X:
Sub Process_Globals
   Public analytics As FirebaseAnalytics
End Sub

Sub Service_Create
   analytics.Initialize
End Sub

That's it.

You can manually send events with:
B4X:
Starter.analytics.SendEvent("login", CreateMap ("additional parameter": 100))

Note that it can take several hours until the data is available in Firebase console:

SS-2016-06-09_10.03.24.png


SS-2016-06-09_10.03.58.png
 

Attachments

  • FirebaseAnalytics.zip
    2.5 KB · Views: 1,381
Last edited:

daniel69

Member
Licensed User
Longtime User
Hi!
I'm facing a strange behavior on stats....

Testing my app I get no errors (compiling or running), on the FireBase consolle I can see the stats regarding any crash (generated on purpose) but I do not get any other stats after 24h ... (I've embedded the following line in the app create sub : Starter.analytics.SendEvent("app_open", null)

The firebase notification messages are fine (I can send them successfully to the app), the firebase crash stats are fine and the Admob/Firebase link is correct ...

The manifest is identical to the one suggested in the tutorial (without the authoring section), the json file is placed in the app dir, the FireBase libs are of course included (admob, analytics, notification), the services (Starter & FirebaseMessaging) are identical to the one suggested, google play and suggested repository are updated and the testing device has the correct google play (IsGooglePlayServicesAvailable is true)

Development enviroment:
B4A>> 6.0
Android Support Repository >> 36
Google Repository>> 32
Google Play services>> 29

Any suggestions?
 

daniel69

Member
Licensed User
Longtime User
Hi Erel ... thank you for your answer ... still no data ... I've got the feeling that it may depend on the app package name .. it has some CAPITAL letters (it starts with B4A...) ... it is my first app and I did not know at that time about problems that may arise with capital letters ... I've checked the Jason file and the project info and the client info are correct (and the package name starts with B4A.)
 

daniel69

Member
Licensed User
Longtime User
Hi Erel .. I was checking other values of the google-service.json file .. is the following OK? or should I change it to "status":2 ...

"services": {

"analytics_service": {

"status": 1

},
 

daniel69

Member
Licensed User
Longtime User
In release mode sometimes (1 out of many) I get the following line

LogCat connected to: B4A-Bridge: samsung GT-I9505
--------- beginning of /dev/log/main
** Service (starter) Create **
Failed to ensure directory: /storage/extSdCard/Android/data/anywheresoftware.b4a.b4abridge/files
** Service (starter) Start **

<qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936)
....


In debug mode I get the following

** Activity (main) Resume **
Installing file.
GC_CONCURRENT freed 7253K, 32% free 18164K/26612K, paused 7ms+5ms, total 79ms
WAIT_FOR_CONCURRENT_GC blocked 25ms
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:B4A.software
Copying updated assets files (46)
** Service (starter) Create **
** Service (starter) Start **

** Activity (main) Create, isFirst = true **
PhoneModel=GT-I9505
** Activity (main) Resume **
** Service (firebasemessaging) Create **
** Service (firebasemessaging) Start **

...
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello,

I have the same problem as described above:

- firebase messaging runs fine
- firebase storage runs fine
- crash reporting runs fine

but when I do this:

'test analytics
Dim parameters As Map
parameters.Initialize
parameters.Put("version", NameVersion)
parameters.Put("date", DateTime.Date(DateTime.Now))
parameters.Put("time", DateTime.Time(DateTime.Now))
analytics.SendEvent("test", parameters)

nothing to see in the firebase console........

I also noticed an interaction between the "starter" service and receiving messages:

when you switch off the phone and send messages to the phone, those messages are waiting on the server for delivery to the phone.

when you switch on the phone, the messages are delivered as soon as the phone makes contact with the internet.

So, firebasemessaging starts the app, but that causes starter to run first.

And that sequence results in the loss of 1 message!!!

Note however that this is due to the underlying gcm-system: I have an app running with google gcm and that has the same problem.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

I also noticed an interaction between the "starter" service and receiving messages:
Push messages are not related to this thread.

1. Monitor the unfiltered logs. There might be more information about the missing events.

2. Test it with this event:
B4X:
analytics.SendEvent("search", CreateMap("search_term": "test"))
 

daniel69

Member
Licensed User
Longtime User
Hi Erel, thank you for your suggestion .. maybe before I'll try with a different event ...
 

daniel69

Member
Licensed User
Longtime User
Hi Erel, today for the first time I can see the stats (relative to today, October 19th 12:00 AM), and nothing about the previous days (it looks like it started to collect data after some days: about 2 weeks!
 

daniel69

Member
Licensed User
Longtime User
Hi Erel, with another app it took about 24 hours to see the first stats ... most likely the stats of the other app (app name with capital letters) was fixed by firebase support ...
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello,

a question: is FirebaseAnalytics also working if one is using no starter service?

I've included the code for FirebaseAnalytics in a service that is always running.

Now it seems that fatal errors in this service are correctly reported but fatal errors in other services and activities are not reported!

Or should I include FirebaseAnalytics in all services and activities when not using the starter service?
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Why aren't you using the starter service? It will only make your code simpler.

Indeed, using the starter service would make my code a lot simpler.

Unfortunately, there is an interaction between firebasemessaging and the starter service:

- one sends a message to the phone while the phone is switched off
- switch on the phone
- the message is received by the phone and the app is automatically started by Android
- now it seems to be that starting the service that handles the incoming firebase message causes the starter service to start and somewhere in this process, the message is lost.

Note that this seems to be not specific for firebase messaging because I've seen the same effect happen using google GCM

In a nutshell, that's the reason for not using the starter service...
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
I'm not familiar with this issue. Many developers use push notifications with the starter service and as far as I remember you are the only one to report such issue.

It is better to start a new thread and explain the issue.

I will test the issue again, one never knows if something has changed, and start a new thread if the issue is still the same
 

Alpandino

Member
Licensed User
2. Test it with this event:
B4X:
analytics.SendEvent("search", CreateMap("search_term": "test"))

So I must use a "Prescribed parameters" (as "search_term") in order to see the result in the firebase analytic console?
I'm sending a lot of events with associated parameters but I can't see anything......
 

Alpandino

Member
Licensed User
It should work with custom events as well: https://firebase.google.com/docs/analytics/android/events#log_events

You can see the list of predefined events here: https://firebase.google.com/docs/re...FirebaseAnalytics.Event.html#constant-summary

Note that you should not use the constant names but rather the values:

SS-2017-05-12_07.55.32.png
Thank you Erel.
I logged the "search" event with the standard parameters "search_term". In the console I can see the "search" event, but I can't see the parameter. This is a limit of firebase console as reported here: https://groups.google.com/forum/#!topic/firebase-talk/W9w-YKkW4fc
Here you can read: "Currently, not all suggested events provide parameter reporting, but we aim to add parameter reporting to all of them. In the meantime, your best options for viewing parameter reporting on these is to either use a different event (like select_content) or to view your data in BigQuery."
I'll try the event "select_content"...

Bye
 
Top