public class GoogleAnalyticsProvider

  1. Object
  2. AbstractAnalyticsProvider
  3. GoogleAnalyticsProvider

ImplementsAnalyticsProvider

A provider for Google Analytics 4 using the Measurement Protocol (v2). This replaces the retired Universal Analytics / Measurement Protocol v1 endpoint that the legacy AnalyticsService targeted.

Create one with a GA4 measurement id (G-XXXXXXXX) and a Measurement Protocol API secret generated in the GA4 admin console:

Analytics.addProvider(new GoogleAnalyticsProvider("G-XXXXXXXX", "MY_API_SECRET"));

Screen views are sent as the GA4 screen_view event, custom events use the (sanitised) event name, and crashes are reported as app_exception. IP addresses are anonymised by GA4 by default. Consent is enforced upstream by Analytics, so this provider sends whatever it is handed.

Constructors

public GoogleAnalyticsProvider(String measurementId, String apiSecret)Creates a GA4 provider.

Methods

public void setEndpoint(String url)Overrides the collection endpoint.
public String getName()A short, stable, human readable name for this provider (used in logs and diagnostics).
public void setUserId(String id)Associates subsequent activity with a user identifier.
public void setUserProperty(String key, String value)Sets a user-level property / custom dimension.
public void trackScreen(String name, String referrer)Records a screen / page view.
public void trackEvent(AnalyticsEvent event)Records a named event.
public void reportCrash(AnalyticsCrashReport report)Reports a crash or handled exception.
public boolean supports(AnalyticsCapability capability)Indicates whether the provider supports a given capability.

Inherited methods

Constructor details

GoogleAnalyticsProvider

public GoogleAnalyticsProvider(String measurementId, String apiSecret)
Creates a GA4 provider.

Parameters

measurementId String
the GA4 measurement id, e.g. G-XXXXXXXX
apiSecret String
a Measurement Protocol API secret

Method details

setEndpoint

public void setEndpoint(String url)
Overrides the collection endpoint. Useful for routing through a first-party proxy (or a test server).

Parameters

url String
the collection endpoint URL

getName

public String getName()
A short, stable, human readable name for this provider (used in logs and diagnostics).

Returns

the provider name

setUserId

public void setUserId(String id)
Associates subsequent activity with a user identifier.

Parameters

id String
the user id, or null to clear

setUserProperty

public void setUserProperty(String key, String value)
Sets a user-level property / custom dimension.

Parameters

key String
the property name
value String
the property value

trackScreen

public void trackScreen(String name, String referrer)
Records a screen / page view.

Parameters

name String
the screen name
referrer String
the previous screen name, may be null

trackEvent

public void trackEvent(AnalyticsEvent event)
Records a named event.

Parameters

event AnalyticsEvent
the event

reportCrash

public void reportCrash(AnalyticsCrashReport report)
Reports a crash or handled exception.

Parameters

report AnalyticsCrashReport
the crash report

supports

public boolean supports(AnalyticsCapability capability)
Indicates whether the provider supports a given capability.

Parameters

capability AnalyticsCapability
the capability to query

Returns

true if supported