Working with Google Analytics GA4 in Delphi 10.4.2

  

I’m trying to send events from my mobile application to Google analytics.
External library connected:
com-google-android-gms.play-services-analytics-impl.16.0.8.jar
Androidapi.JNI.PlayServices
After launching the application, I get the following.
04-23 15:03:50.835 30878 30878 I GAv4 : adb shell setprop log.tag.GAv4 DEBUG
04-23 15:03:50.835 30878 30878 I GAv4 : adb logcat -s GAv4
04-23 15:03:50.869 30878 30878 D GAv4 : setLocalDispatchPeriod (sec): 5
04-23 15:03:51.506 30878 30986 D GAv4 : Sending first hit to property: 269XX9293
04-23 15:03:51.508 30878 30986 D GAv4 : Hit delivery requested: ht=1619179430868, _s=0, _v=ma12.4.51, a=2027768350, adid=7b03fae1-XXXX-4820-892a-ba3983eb6b7e, aid=com.XXXXXXX, an=XXXXXXX, ate=1, av=1.0.1, cid=24213dea-e355-XXXX-8b91-17087584684a, ea=lo
gin, ec=login, el=login, ev=87, sr=1080×2030, t=event, tid=269XX9293, ul=ru-ru, v=1
04-23 15:03:51.737 30878 30986 D GAv4 : Hit sent to the device AnalyticsService for delivery

Added to AndroidManifest.xml
<receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.AnalyticsService"
android:enabled="true"
android:exported="false"/>

<!– Optionally, register CampaignTrackingReceiver and CampaignTrackingService to enable
installation campaign reporting –>
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />

To work with the Java class, the following class com-google-android-gms.play-services-analytics-impl.16.0.8.jar is used:
unit GoogleAnalytics;

interface

uses
System.SysUtils,

System.Types, System.UITypes, System.Classes, System.Variants,
{$IFDEF ANDROID}
Androidapi.JNI.PlayServices,
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText,
{$ENDIF}
FMX.Types;

type
TwGoogleAnalytics=class
private
FCollectDataPermission
: boolean;
FGoogleAppId,
fClient_Id
: String;
{$IFDEF ANDROID}
FTracker : Janalytics_Tracker;
FGoogleAId, JClient_Id : JString;
{$ENDIF}
public
{$IFDEF ANDROID}
JGA : Janalytics_GoogleAnalytics;
property CollectDataPermission: boolean read FCollectDataPermission write FCollectDataPermission;
{$ENDIF}
constructor Create;
procedure SetParams(const aGoogleAppID, aClient_Id:string);

property GoogleAppId:string read FGoogleAppId;
end;

implementation

constructor TwGoogleAnalytics.Create;
begin
FGoogleAppId := ”;
fClient_Id := ”;
end;

procedure TwGoogleAnalytics.SetParams(const aGoogleAppID, aClient_Id : String);
var
Map1 : JMap;
Hit : JHitBuilders_HitBuilder;
EventHit : JHitBuilders_EventBuilder;
Loger : Janalytics_Logger;
begin
{$IFDEF ANDROID}
CollectDataPermission := true;//(Ini.CollectDataPermission = PERMISSION_YES);

JGA := TJanalytics_GoogleAnalytics.JavaClass.getInstance( TAndroidHelper.Context );
JGA.setDryRun(false);
JGA.reportActivityStart( SharedActivity );

FGoogleAId := StringToJString(aGoogleAppID);
JClient_Id := StringToJString(aClient_Id);
FTracker := JGA.newTracker(FGoogleAid);

if FTracker <> nil then
Begin
FTracker.enableAutoActivityTracking(true);
FTracker.enableAdvertisingIdCollection(true);

EventHit:= TJHitBuilders_EventBuilder.Create;

EventHit.setAction(StringToJString(‘login’));
EventHit.setCategory(StringToJString(‘login’));
EventHit.setLabel(StringToJString(‘login’));
EventHit.setValue(87);

Map1 := EventHit.build;

FTracker.send(Map1);

JGA.setLocalDispatchPeriod(5);
End;
{$ENDIF}
FGoogleAppId:=aGoogleAppID;
end;

end.

But no data comes to analytics. There is a chance that the library is out of date. But how to download a new one, I do not know, in the description of the SDK there is an option to work only with gradle. As far as I know, this is not possible in Delphi. I will wait for any advice.

Comments are closed.