Class GoogleAnalytics4Provider
- java.lang.Object
-
- ubic.gemma.rest.analytics.ga4.GoogleAnalytics4Provider
-
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean
,org.springframework.beans.factory.InitializingBean
,AnalyticsProvider
public class GoogleAnalytics4Provider extends Object implements AnalyticsProvider, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
Implementation of theAnalyticsProvider
interface for Google Analytics 4 collect API.General outline:
Events are queued and ordered by date. Events that share the same date within a configured time resolution, client ID and user ID are batched together. The time resolution can be configured via
setResolutionMillis(long)
.A task is run regularly to flush pending events regardless of the queue size and can be configured with
setPollingIntervalMillis(long)
.If a full batch can be sent after
sendEvent(String, Date, Map)
is invoked and no manual flush is currently in-progress, a manual flush is scheduled. This will only flush a single batch of events.When the bean is destroyed, all pending events are flushed.
- Author:
- poirigui
-
-
Constructor Summary
Constructors Constructor Description GoogleAnalytics4Provider(String measurementId, String apiSecret)
GoogleAnalytics4Provider(org.springframework.web.client.RestTemplate restTemplate, String measurementId, String apiSecret)
Create a new Google Analytics 4 provider.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
void
destroy()
void
sendEvent(String eventName, Date date, Map<String,String> params)
Publish an event.void
setClientIdRetrievalStrategy(ClientIdRetrievalStrategy clientIdRetrievalStrategy)
Set the strategy used to retrieve client IDs.void
setDebug(boolean debug)
Enable the debug mode.void
setPollingIntervalMillis(long pollingIntervalMillis)
Interval at which events are polled and flushed.void
setResolutionMillis(long resolutionMillis)
Time resolution for regrouping events in the same batch.void
setUserIdRetrievalStrategy(UserIdRetrievalStrategy userIdRetrievalStrategy)
Set the strategy used to retrieve user IDs.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ubic.gemma.rest.analytics.AnalyticsProvider
sendEvent, sendEvent
-
-
-
-
Constructor Detail
-
GoogleAnalytics4Provider
public GoogleAnalytics4Provider(org.springframework.web.client.RestTemplate restTemplate, String measurementId, String apiSecret)
Create a new Google Analytics 4 provider.- Parameters:
restTemplate
- a REST template for performing requests with the collect APImeasurementId
- a measurement ID which may be emptyapiSecret
- an API secret, must be non-empty if measurementId is supplied
-
-
Method Detail
-
afterPropertiesSet
public void afterPropertiesSet()
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
destroy
public void destroy() throws Exception
- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
- Throws:
Exception
-
setClientIdRetrievalStrategy
public void setClientIdRetrievalStrategy(ClientIdRetrievalStrategy clientIdRetrievalStrategy)
Set the strategy used to retrieve client IDs.
-
setUserIdRetrievalStrategy
public void setUserIdRetrievalStrategy(UserIdRetrievalStrategy userIdRetrievalStrategy)
Set the strategy used to retrieve user IDs.
-
setDebug
public void setDebug(boolean debug)
Enable the debug mode.When debug mode is enabled, a testing GA4 endpoint will be used and
sendEvent(String, Date, Map)
may raise exception instead of simply warning.
-
setResolutionMillis
public void setResolutionMillis(long resolutionMillis)
Time resolution for regrouping events in the same batch.Events with the same client and user IDs are batched together if they are less than the resolution apart and reported at the time the first event in the batch occurred.
Setting this to zero effectively disable batching of events unless they occur at exactly the same time. This can be achieved by reusing the
Date
object when sending multiple events.
-
setPollingIntervalMillis
public void setPollingIntervalMillis(long pollingIntervalMillis)
Interval at which events are polled and flushed.
-
sendEvent
public void sendEvent(String eventName, Date date, Map<String,String> params)
Description copied from interface:AnalyticsProvider
Publish an event.- Specified by:
sendEvent
in interfaceAnalyticsProvider
- Parameters:
eventName
- a name for the eventdate
- an exact moment when the event occurredparams
- additional parameters for the event
-
-