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 the AnalyticsProvider 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 Details

    • 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 API
      measurementId - a measurement ID which may be empty
      apiSecret - an API secret, must be non-empty if measurementId is supplied
    • GoogleAnalytics4Provider

      public GoogleAnalytics4Provider(String measurementId, String apiSecret)
      See Also:
  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface org.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 interface AnalyticsProvider
      Parameters:
      eventName - a name for the event
      date - an exact moment when the event occurred
      params - additional parameters for the event