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: 930
Last edited:

yiankos1

Well-Known Member
Licensed User
Longtime User
Thank you very much Erel! That was the solution.
Because i am new to Analytics, i want to ask you at Analytics suite what do i have to look to see which ad is clicked more?
(At my app, i have two admob ads and i want through analytics to see which ad is more profitable(clicked more) in order the other one delete.)

Thats the code to fire analytics when ad clicked:
B4X:
Sub Ad_PresentScreen
    'manually send an event
    tracker.TrackEvent("c1", "a1", "l1", 100)
    tracker.Dispatch 'don't wait for the automatic dispatch (useful for the real-time view).
End Sub
 
Last edited:

airblaster

Active Member
Licensed User
Longtime User
Hi Erel,

I'm currently trying to remove as many permissions from my app as possible, because of negative user feedback.
During this I stumbled over the permission "android.permission.ACCESS_NETWORK_STATE", which currently is only used by AnalyticsTracker in my project.
What kind of disadvantages would it have to forcefully remove this permission by calling RemovePermission("android.permission.ACCESS_NETWORK_STATE")?
 

brelto85

Active Member
Licensed User
Longtime User
I am no expert on this topic

how the library updates the statistics analytics account?
if the data mobile connectivity is off, throws an exception?
 

korshkov

Member
Licensed User
Longtime User
Hello!
Please help me... and add Method to this library.
In SDK i see code for add screens events:
myTracker.sendView("Home Screen"); // Where myTracker is an instance of Tracker.

Is that possible?

Thanks!
 

corwin42

Expert
Licensed User
Longtime User
Just a small tip for this library.

If you want to use it from a background service and not from an activity just call the following sub before calling tracker.TrackEvent()

B4X:
Public Sub SetAnalyticsContext
    Dim r As Reflector
   
    r.Target = r.RunStaticMethod("com.google.analytics.tracking.android.EasyTracker", "getInstance", Null, Null)
    r.Target = r.RunMethod4("setContext", Array As Object(r.GetContext), Array As String("android.content.Context"))
End Sub

Otherwise an exception will be thrown.

@Erel:
Perhaps this info can be added to the first post?
 

Ratna Fang

Member
Licensed User
Longtime User
hi erel,

i followed from the top, but i have a little bit confused about deploying google analytics.

here are the steps
1. declaring in global: Dim Tracker As AnalyticsTracker
2. Put this code in Activity_Resume: GoogleTracker.Start
3. Put this code in Activity_Pause: Tracker.Stop
4. if i want to track automatically, not manual, can i just put this code:
B4X:
Tracker.TrackEvent("c1", "a1", "l1", 100)
Tracker.Dispatch
in Activity_Created ?

is there any link which i can find the documentation about the trackevent parameters?

thanks
 
Status
Not open for further replies.
Top