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:

JonPM

Well-Known Member
Licensed User
Longtime User
I think a disclaimer should be placed in first post stating adding google-play-services.jar will increase your APK size by at least 1.5mb. Unless you are using Google Play Services for something else in your app, this analytics library isn't worth the file size inflation
 

DonManfred

Expert
Licensed User
Longtime User

Leni Berry

Active Member
Licensed User
Longtime User
hi Erel,

is there an example that i can download? because your tutorial on first post not suitable for my b4a version.
B4A version 5.5

If FirstTime Then
tracker.Initialize("UA-xxxxxxxx-1",1)
End If

tracker.TrackPageView("Main Activity")
 

Leni Berry

Active Member
Licensed User
Longtime User
oh my mistake... now i use analytics v4. but after compiling and run the apps, it show error that the program cannot find analytics.xml.
but, under Objects\src\xml the file exist. and xml folder already read only attribute.
 

JohnC

Expert
Licensed User
Longtime User
Even using B4A v6, I was getting the error:

java.lang.NoClassDefFoundError: com.google.android.gms.analytics.GoogleAnalytics

Apparently the latest play services download does NOT include a file called "google-play-services.jar" anywhere.

So I spent a bunch of time and found that I needed to add this line in my main activity to fix this issue:

B4X:
#AdditionalJar: com.google.android.gms:play-services-analytics
 

tucano2000

Active Member
Licensed User
Longtime User
Even using B4A v6, I was getting the error:

java.lang.NoClassDefFoundError: com.google.android.gms.analytics.GoogleAnalytics

Apparently the latest play services download does NOT include a file called "google-play-services.jar" anywhere.

So I spent a bunch of time and found that I needed to add this line in my main activity to fix this issue:

B4X:
#AdditionalJar: com.google.android.gms:play-services-analytics


I removed .measurement of #ExcludeClasse in Main Activity to resolve this issue:

https://www.b4x.com/android/forum/t...-removes-analytics-classes.67056/#post-424419

#ExcludeClasses: .games, .drive, .fitness, .wearable, .cast, .auth, .nearby
#ExcludeClasses: .tagmanager, .wallet, .plus, .vision, .gcm
 

tucano2000

Active Member
Licensed User
Longtime User
As far as i know with b4a 6+ the ExcludeClasses command is no more needed as the google-play-services.jar is now splitted into smaller pieces.
I guess you can totally remove the commands.

I do this with google-play-services.jar extrated of library and puted into libraries folder of b4A 6.0 but don't works. My version is r29.

Show this message during compilation process if I try to remove ExcludeClasses:

Compiling layouts code. (0.06s)
Generating R file. (0.72s)
Compiling generated Java code. (5.67s)
Convert byte code - optimized dex. Error
trouble writing output: Too many method references: 76283; max is 65536.
You may try using --multi-dex option.
 

DonManfred

Expert
Licensed User
Longtime User
My version is r29.
using b4a 6 you should update to newest version of this. This is suggested
The newest is splitted in smaller pieces and exclude classes is no more needed.

if you use r29 you need to use exclude classes as there is only one big googleplayservices.jar.
 
Status
Not open for further replies.
Top