public class GoogleAnalyticsProvider
- Object
- AbstractAnalyticsProvider
- 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
measurementIdString- the GA4 measurement id, e.g.
G-XXXXXXXX apiSecretString- 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
urlString- 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
idString- the user id, or null to clear
setUserProperty
public void setUserProperty(String key, String value)Sets a user-level property / custom dimension.
Parameters
keyString- the property name
valueString- the property value
trackScreen
public void trackScreen(String name, String referrer)Records a screen / page view.
Parameters
nameString- the screen name
referrerString- the previous screen name, may be null
trackEvent
public void trackEvent(AnalyticsEvent event)Records a named event.
Parameters
eventAnalyticsEvent- the event
reportCrash
public void reportCrash(AnalyticsCrashReport report)Reports a crash or handled exception.
Parameters
reportAnalyticsCrashReport- the crash report
supports
public boolean supports(AnalyticsCapability capability)Indicates whether the provider supports a given capability.
Parameters
capabilityAnalyticsCapability- the capability to query
Returns
true if supported