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

TpS

Member
Licensed User
Longtime User
It is not enough the download google play services. Have you copied the jar file to the additional libraries folder?
Yes, downloaded the latest and placed the jar file in Additional Libraries.
Is the path to the Addition Libraries critical?
Right now it something like: "c:\Program files(x86)\Anywhere Software\......" Thought if the space between program and files(x86) can make a difference?
 

Bill Kantz

Member
Licensed User
Longtime User
Erel,

Not sure how to implement with a widget with multiple activities? do I start analytics in the service?

Thanks, Bill
 

TpS

Member
Licensed User
Longtime User
It is better to move it out of program files. Windows can copy the file to a virtualized folder instead of the real one.
Done, but still no success :(

I have the Analytics.xml placed in \Objects\res\xml and it contains this (with the correct ID ofcourse):

B4X:
<?xml version="1.0" encoding="utf-8" ?>

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

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

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

</resources>
Does it look alright?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
@TpS the error you are getting is not related to the xml. It cannot find a class from google-play-services.jar.

Not sure how to implement with a widget with multiple activities? do I start analytics in the service?
You can initialize it from the service. Just make sure not to initialize it multiple times.
 

ilan

Expert
Licensed User
Longtime User
very nice lib, works good, i have installed it to one of my app to try it out.
one thing is not working the REALTIME, if i open my app i dont see 1 user now online, is there a way to do it?

Realtime is also working, seem like i had only to wait until everything worked...
 
Last edited:

ilan

Expert
Licensed User
Longtime User
is possible to add the option of Demographics and Interest Reports to the analystics lib?

this is what google says: Your Demographics and Interest Reports have been enabled, but your Analytics tracking code does not include the necessary support to show them.

what does need to be changed?? https://support.google.com/analytics/answer/2444872?hl=en&utm_id=ad
 

TpS

Member
Licensed User
Longtime User
Possible problems:
- Wrong version of this jar (can be in either the internal libraries folder or the additional folder).
- Java 6 instead of 7.
- Not using B4A v3.82.
Oki, I'm running latest Java 7 u71 and latest B4A 3.82 and latest jar from Sdk.
Can someone with a working Analytics post their jar file so I can try it?
 

TpS

Member
Licensed User
Longtime User
I´we located the error:

I was running an old AdMob library (v1.4) in the same project.
When I disabled that library the Analytics compiled just fine and didn´t throw an error.

Will update to v2 of Admob insted and probably I can have both Analytics and Ad´s this way.
 

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, Im trying this for the first time, and just updated to B4A 4.00 so im not sure if a) i am doing it wrong, or its just not working in B4A 4.00
XML in MYAPP/Objects/res/values and set to R/O all the code in place, and on running the app i get Cannot find analytics.xml ???

Admob works fine! so plays ok, disabled Admob, still same Cannot find analytics.xml ??? is it me?

If it is me forgive me! im new! lol ;)

Thanks

Aidy
 

jmon

Well-Known Member
Licensed User
Longtime User
If it is me forgive me! im new! lol ;)
Yes, I think it's you ;-)

No, seriously, it's the update of Analytics v4. Look at the error, analytics v4.00 isn't looking for the XML in the \Objects\res\values anymore like v3, it's looking for it in \Objects\res\xml.
So just move your analytics.xml to \Objects\res\xml and you should be good to go!

Good luck!
 

aidymp

Well-Known Member
Licensed User
Longtime User
Yes, I think it's you ;-)

No, seriously, it's the update of Analytics v4. Look at the error, analytics v4.00 isn't looking for the XML in the \Objects\res\values anymore like v3, it's looking for it in \Objects\res\xml.
So just move your analytics.xml to \Objects\res\xml and you should be good to go!

Good luck!

Haha! Yes the instructions where confusing, I have sorted it with your help! Thank you
 

Mrjoey

Active Member
Licensed User
Longtime User
hey mr erel , does this library shows app installs? i added it to my project and i can see the active users on my analytics account under real time with no problems .
 

jmon

Well-Known Member
Licensed User
Longtime User

henrywood

Active Member
Licensed User
Longtime User
Cannot get this thing working. Cannot compile my app when AnalyticsV4 is referenced/ticked

I have updated my Google Play Services using the SDK Manager and copied the file googleplayservices.jar to Additional Libs folder

My revision of the GooglePlayServices is rev. 23

Here is the error I am getting:

B4A version 4.30
Parsing code. 0.36
Running custom action. 0.02
Compiling code. 2.21
Compiling layouts code. 0.76
Generating R file. 0.46
Compiling debugger engine code. 8.53
Compiling generated Java code. 9.64
Convert byte code - optimized dex. Error
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/common/internal/safeparcel/SafeParcelable;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
at com.android.dx.command.dexer.Main.processClass(Main.java:685)
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.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
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)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
1 error; aborting
Optimized dexer failed. Switching to Standard dexer.


My #AdditionalJar and #AdditionalRes directives are as follows:

'FACEBOOK related directives
#AdditionalRes: ..\AppConfig
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\facebook\sdk\res
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\facebook\sdk\facebooksdk.jar

'GOOGLE+ related directives
'#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\googleplus\sdk\res, com.google.android.gms
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\googleplus\sdk\res, com.google.android.gms
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\googleplus\sdk\gplussdk.jar

'Twitter related directives
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\twitter\sdk\res
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\twitter\sdk\twittersdk.jar

'Instagram related directives
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\instagram\sdk\res
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\instagram\sdk\instagramsdk.jar

' Google Play Services / GoogleMaps / InApp Billing
#AdditionalRes: C:\adt-bundle-windows-x86_64-20140702\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res



Any suggestions ?
 
Last edited:
Status
Not open for further replies.
Top