Class GoogleAnalyticsProvider
- All Implemented Interfaces:
AnalyticsProvider
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.
-
Constructor Summary
ConstructorsConstructorDescriptionGoogleAnalyticsProvider(String measurementId, String apiSecret) Creates a GA4 provider. -
Method Summary
Modifier and TypeMethodDescriptiongetName()A short, stable, human readable name for this provider (used in logs and diagnostics).voidreportCrash(AnalyticsCrashReport report) Reports a crash or handled exception.voidsetEndpoint(String url) Overrides the collection endpoint.voidAssociates subsequent activity with a user identifier.voidsetUserProperty(String key, String value) Sets a user-level property / custom dimension.booleansupports(AnalyticsCapability capability) Indicates whether the provider supports a given capability.voidtrackEvent(AnalyticsEvent event) Records a named event.voidtrackScreen(String name, String referrer) Records a screen / page view.Methods inherited from class AbstractAnalyticsProvider
flush, getContext, init, onConsentChanged
-
Constructor Details
-
GoogleAnalyticsProvider
-
-
Method Details
-
setEndpoint
Overrides the collection endpoint. Useful for routing through a first-party proxy (or a test server).
Parameters
url: the collection endpoint URL
-
getName
Description copied from interface:AnalyticsProviderA short, stable, human readable name for this provider (used in logs and diagnostics).
Returns
the provider name
- Specified by:
getNamein interfaceAnalyticsProvider- Specified by:
getNamein classAbstractAnalyticsProvider
-
setUserId
Description copied from interface:AnalyticsProviderAssociates subsequent activity with a user identifier.
Parameters
id: the user id, or null to clear
- Specified by:
setUserIdin interfaceAnalyticsProvider- Overrides:
setUserIdin classAbstractAnalyticsProvider
-
setUserProperty
Description copied from interface:AnalyticsProviderSets a user-level property / custom dimension.
Parameters
-
key: the property name -
value: the property value
- Specified by:
setUserPropertyin interfaceAnalyticsProvider- Overrides:
setUserPropertyin classAbstractAnalyticsProvider
-
-
trackScreen
Description copied from interface:AnalyticsProviderRecords a screen / page view.
Parameters
-
name: the screen name -
referrer: the previous screen name, may be null
- Specified by:
trackScreenin interfaceAnalyticsProvider- Overrides:
trackScreenin classAbstractAnalyticsProvider
-
-
trackEvent
Description copied from interface:AnalyticsProviderRecords a named event.
Parameters
event: the event
- Specified by:
trackEventin interfaceAnalyticsProvider- Overrides:
trackEventin classAbstractAnalyticsProvider
-
reportCrash
Description copied from interface:AnalyticsProviderReports a crash or handled exception.
Parameters
report: the crash report
- Specified by:
reportCrashin interfaceAnalyticsProvider- Overrides:
reportCrashin classAbstractAnalyticsProvider
-
supports
Description copied from interface:AnalyticsProviderIndicates whether the provider supports a given capability.
Parameters
capability: the capability to query
Returns
true if supported
- Specified by:
supportsin interfaceAnalyticsProvider- Overrides:
supportsin classAbstractAnalyticsProvider
-