# Class GoogleAnalyticsProvider

java.lang.Object
com.codename1.analytics.AbstractAnalyticsProvider
com.codename1.analytics.GoogleAnalyticsProvider

All Implemented Interfaces:
  `AnalyticsProvider`

public class GoogleAnalyticsProvider extends AbstractAnalyticsProvider

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

  Constructors
  Constructor
  Description
  `GoogleAnalyticsProvider(String measurementId, String apiSecret)`
  Creates a GA4 provider.

- ## Method Summary

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

  ### Methods inherited from class AbstractAnalyticsProvider

  `flush, getContext, init, onConsentChanged`

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### GoogleAnalyticsProvider

    public GoogleAnalyticsProvider(String measurementId, String apiSecret)

    Creates a GA4 provider.

    ###### Parameters

    - `measurementId`: the GA4 measurement id, e.g. `G-XXXXXXXX`

    - `apiSecret`: 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`: the collection endpoint URL

  - ### getName

    public String getName()
    Description copied from interface: `AnalyticsProvider`

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

    ###### Returns

    the provider name

    Specified by:
      `getName` in interface `AnalyticsProvider`
    Specified by:
      `getName` in class `AbstractAnalyticsProvider`

  - ### setUserId

    public void setUserId(String id)
    Description copied from interface: `AnalyticsProvider`

    Associates subsequent activity with a user identifier.

    ###### Parameters

    - `id`: the user id, or null to clear
    Specified by:
      `setUserId` in interface `AnalyticsProvider`
    Overrides:
      `setUserId` in class `AbstractAnalyticsProvider`

  - ### setUserProperty

    public void setUserProperty(String key, String value)
    Description copied from interface: `AnalyticsProvider`

    Sets a user-level property / custom dimension.

    ###### Parameters

    - `key`: the property name

    - `value`: the property value

    Specified by:
      `setUserProperty` in interface `AnalyticsProvider`
    Overrides:
      `setUserProperty` in class `AbstractAnalyticsProvider`

  - ### trackScreen

    public void trackScreen(String name, String referrer)
    Description copied from interface: `AnalyticsProvider`

    Records a screen / page view.

    ###### Parameters

    - `name`: the screen name

    - `referrer`: the previous screen name, may be null

    Specified by:
      `trackScreen` in interface `AnalyticsProvider`
    Overrides:
      `trackScreen` in class `AbstractAnalyticsProvider`

  - ### trackEvent

    public void trackEvent(AnalyticsEvent event)
    Description copied from interface: `AnalyticsProvider`

    Records a named event.

    ###### Parameters

    - `event`: the event
    Specified by:
      `trackEvent` in interface `AnalyticsProvider`
    Overrides:
      `trackEvent` in class `AbstractAnalyticsProvider`

  - ### reportCrash

    public void reportCrash(AnalyticsCrashReport report)
    Description copied from interface: `AnalyticsProvider`

    Reports a crash or handled exception.

    ###### Parameters

    - `report`: the crash report
    Specified by:
      `reportCrash` in interface `AnalyticsProvider`
    Overrides:
      `reportCrash` in class `AbstractAnalyticsProvider`

  - ### supports

    public boolean supports(AnalyticsCapability capability)
    Description copied from interface: `AnalyticsProvider`

    Indicates whether the provider supports a given capability.

    ###### Parameters

    - `capability`: the capability to query

    ###### Returns

    true if supported

    Specified by:
      `supports` in interface `AnalyticsProvider`
    Overrides:
      `supports` in class `AbstractAnalyticsProvider`