B4A Library Google Analytics v4 library

Status
Not open for further replies.
Google Analytics V4: https://developers.google.com/analytics/devguides/collection/android/v4/

The instructions were updated to reflect V4 changes. Note that this library will only work with B4A v3.82+.

Setup instructions:
- Download Google Play Services from Android SDK Manager and copy google-play-services.jar to the additional libraries folder.
- Create a Mobile App Analytics account: https://support.google.com/analytics/answer/1042508

The tracker is configured with an XML file. You should create a file with the following text named analytics.xml and put it under Objects\res\xml (create this folder if needed):
<?xml version="1.0" encoding="utf-8" ?>

<resources>
<!--Replace placeholder ID with your tracking ID-->
<string name="ga_trackingId">UA-xxxxx-y</string>

<!--Enable Activity tracking-->
<bool name="ga_autoActivityTracking">true</bool>

<!--Enable automatic exception tracking-->
<bool name="ga_reportUncaughtExceptions">true</bool>

</resources>

Make sure to set this file to be read-only. Otherwise the IDE will delete it during compilation.

You should declare a single AnalyticsTracker in Sub Process_Globals of your main module (or other module). The single tracker should be used from all modules.
B4X:
Sub Process_Globals
   Public tracker As AnalyticsTracker
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     tracker.Initialize
   End If
   tracker.SendScreenView("Main Activity")
End Sub

Other implemented methods: TrackEvent, SendTransaction, SendTiming and SendProduct. Note that you can pass empty strings for optional parameters.
 

Attachments

  • AnalyticsV4.zip
    3.5 KB · Views: 1,031
Last edited:

MMORETTI964

Member
Licensed User
Longtime User
I have tried and experienced different exceptions.
In most cases I see "The program XXXX has been stopped" with OK, then the program seems restart itself.
Something I have seen "Do you want to continue". In that case how can I send this exception to analytics?

Thank you.
 

MMORETTI964

Member
Licensed User
Longtime User
Erel, just for seeing I'm making all things right, can I put some code in my source having an exception that surely goes onto analytics?

Also, AFAYK how much time should passed before I have the reportcrash in the analytics console?

Thank you very much.
 

GMan

Well-Known Member
Licensed User
Longtime User
my datas appear about 6 h after starting the App the first time.
 

MMORETTI964

Member
Licensed User
Longtime User
Yes, seems there is a delay (seems one day), now some crash arrives.
Thank you to all, crashes submitted(?) in the first day after getting our code never appeared.

AFAYK there is a way to trap all exception? Pheraps bulding a java library? Is the google v4 so different?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to test this library out in a small demo app just so I get me head around how it works before I interface it into my app.

I have followed the steps in the first post and added my app ID into the XML file etc.

When I login to Google analytics I see the app and it has 'All Mobile App Data' so I clicked on it.
on the left hand side there is 'App overview' but everything is set to 0.

I have run the app many times etc but it still stays at 0. Is this normal ?

analytics0.PNG


If I click on Real Time > Overview from left menu it shows in real-time how many people are running the app. Soon as I close the app it resets back to 0 again. So I know the app is talking to Google analytics.

analytics1.PNG



I am also trying to work out what this code is doing:
B4X:
tracker.TrackEvent("c1", "a1", "l1", 100)

From what I can see from Real Time > Events it shows 'c1' under the Event Category and when I click that it shows 'a1'.

Should this be displaying somewhere else other then under Real Time ?

(I am guessing this is what is being used to log/track the users actions in the app)

Does this mean I can add this code to a button and will be able to see how often people press a button in the app as well?

I want to be able to login and check in a week/months time and check what people have been doing in the app.

In the IDE when I add tracker.TrackEvent it says to check the Google analytics documentation for more information in regards to the events. Anyone know where to find this documentation ?
 

bluedude

Well-Known Member
Licensed User
Longtime User
To track crashes something like Crashlytics would be much better. iOS developers already use a lot of stuff like:

- Crashlytics
- Testflight

Right now we don't have libraries for that in B4A yet.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

Erel

B4X founder
Staff member
Licensed User
Longtime User
To track crashes something like Crashlytics would be much better. iOS developers already use a lot of stuff like:

- Crashlytics
- Testflight

Right now we don't have libraries for that in B4A yet.
How is this relevant to this thread? Google Analytics is used to track usage and other statistics.

Please start a new thread for this in the wishlist forum.
 

aaronk

Well-Known Member
Licensed User
Longtime User
Yes. It should also be displayed in the standard statistics. Have you waited 24 hours for the statistics to be updated?
I didn't wait 24 hours.
Now that I waited for it to pass midnight (the next day) it is now showing.
 

peacemaker

Expert
Licensed User
Longtime User
Thanks, Erel, for v.4 lib, but any comments to migrate from v3 ?
Is Google.jar of 2MB included into our APK ?!?
Old TrackEvent now works immediately, without Dispatch ?


UPD:
B4X:
Parsing code.                           0.29
Compiling code.                         0.87
Compiling layouts code.                 0.19
Generating R file.                      0.84
Compiling generated Java code.          8.45
Convert byte code - optimized dex.      Error
UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: GC overhead limit exceeded
    at com.android.dx.ssa.RegisterMapper.map(RegisterMapper.java:49)
    at com.android.dx.ssa.NormalSsaInsn.mapSourceRegisters(NormalSsaInsn.java:48)
    at com.android.dx.ssa.SsaRenamer$BlockRenamer.visitNonMoveInsn(SsaRenamer.java:589)
    at com.android.dx.ssa.NormalSsaInsn.accept(NormalSsaInsn.java:206)
    at com.android.dx.ssa.SsaBasicBlock.forEachInsn(SsaBasicBlock.java:989)
    at com.android.dx.ssa.SsaRenamer$BlockRenamer.process(SsaRenamer.java:373)
    at com.android.dx.ssa.SsaRenamer$1.visitBlock(SsaRenamer.java:166)
    at com.android.dx.ssa.SsaMethod.forEachBlockDepthFirstDom(SsaMethod.java:812)
    at com.android.dx.ssa.SsaRenamer.run(SsaRenamer.java:163)
    at com.android.dx.ssa.SsaConverter.convertToSsaMethod(SsaConverter.java:52)
    at com.android.dx.ssa.Optimizer.optimizeMinimizeRegisters(Optimizer.java:133)
    at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:106)
    at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:72)
    at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:299)
    at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:139)
    at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:94)
    at com.android.dx.command.dexer.Main.processClass(Main.java:682)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
    at com.android.dx.command.dexer.Main.access$600(Main.java:78)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:170)
    at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
    at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
    at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
    at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:596)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
    Optimized dexer failed. Switching to Standard dexer.

Please, upload again v.3 lib files :-(
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

Attachments

  • AnalyticsV3.zip
    2.1 KB · Views: 270

korn

Member
Licensed User
Longtime User
i get following errors during runtime

java.lang.NoClassDefFoundError: com.google.android.gms.analytics.GoogleAnalytics
at anywheresoftware.b4a.objects.AnalyticsTracker.Initialize(AnalyticsTracker.java:32)

have already copied jar to additional library folder & analytics.xml in \Objects\res\xml folder

PLEASE HELP.
 

Dadeda

Member
Licensed User
Longtime User
The analytics.xml file is suposed to be created under objects\res\xml or objects\res\values ??
 

aaronk

Well-Known Member
Licensed User
Longtime User
Is the Dispatch feature no longer available in version 4?
How do I track things in real time? Or does it do it automatically ?

In version 3 you used to use the following code but now version 4 doesn't let you:
tracker.Dispatch
 
Last edited:
Status
Not open for further replies.
Top