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:

airblaster

Active Member
Licensed User
Longtime User
Great work, thanks a lot!
Unfortunately Google released beta 4 just yesterday, which changes some of the interfaces.
If I'm not mistaken, the new beta doesn't work with the library due to that.
Beta 3 isn't available on Googles servers anymore.
Could you maybe Upload beta 3 of Google's library here?
 

JonPM

Well-Known Member
Licensed User
Longtime User
Even when I set analytics.xml to read only the IDE appears to delete the file and folder ("values"). Any ideas?
 

JonPM

Well-Known Member
Licensed User
Longtime User
I can't get this to work. Have you successfully tested this in 2.5?

Steps I'm using:
Create analytics.xml, save to desktop, set read only in file properties.
Create folder: myapp/Main/Objects/res/values/
Copy analytics.xml to values folder, confirm it is set to read-only

On compile I can see the folder 'values' being deleted still. I have tried using the CustomBuildAction code in my Main activity.
I've opened the APK and can confirm analytics.xml is not in there.
 

JonPM

Well-Known Member
Licensed User
Longtime User
Ok I found the problem. I keep all of my projects on an external wifi hard drive, when I copied to project locally and compiled it did not delete the read-only file. Seems to only do it when I compile over the network.
 

androh

Member
Licensed User
Longtime User
Hi Erel,
I want to use sendView as PageView that old Analitcs library method.
I used Pageview at my apps. But in new library, I can not find a right method for it.
 

QLogic

Member
Licensed User
Longtime User
Noted.

But I get an error when trying to use the TrackEvent method.

B4X:
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


startService: class qlgc.pkg.pushservice


** Service (pushservice) Create **
** Service (pushservice) Start **
** Activity (main) Pause, UserClosed = true **


** Activity (portal) Create, isFirst = true **


** Activity (portal) Resume **


portal_imghpcross_click (java line: 485)


java.lang.NoSuchMethodError: com.google.analytics.tracking.android.Tracker.trackEvent
   at anywheresoftware.b4a.objects.AnalyticsTracker.TrackEvent(AnalyticsTracker.java:26)
   at qlgc.pkg.portal._imghpcross_click(portal.java:485)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:55)
   at android.view.View.performClick(View.java:4202)
   at android.view.View$PerformClick.run(View.java:17340)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5039)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)

Here is the code snippet:

B4X:
Sub imgHPCross_Click
   tracker.TrackEvent("c1", "a1", "l1", 100)
   tracker.Dispatch
   Activity.RemoveAllViews
   Activity.Finish
   StartActivity("HPPortal")   
End Sub
 
Status
Not open for further replies.
Top