Class GoogleAnalyticsTracker

java.lang.Object
org.vaadin.googleanalytics.tracking.GoogleAnalyticsTracker
All Implemented Interfaces:
Serializable

public class GoogleAnalyticsTracker extends Object implements Serializable
Sends commands to Google Analytics in the browser. An instance of the tracker can be retrieved from a given UI instance (get(UI)) or for the current UI instance (getCurrent()).

Page view commands will automatically be sent for any navigation if the tracker can be configured.

The first time any command is sent, the tracker will configure itself based on the top-level router layout in the corresponding UI. The layout should be annotated with @EnableGoogleAnalytics or implement TrackerConfigurator for the configuration to succeed.

See Also:
  • Method Details

    • getCurrent

      public static GoogleAnalyticsTracker getCurrent()
      Gets or creates a tracker for the current UI.
      Returns:
      the tracker for the current UI, or null if there is no current UI
      See Also:
      • UI.getCurrent()
    • get

      public static GoogleAnalyticsTracker get(com.vaadin.flow.component.UI ui)
      Gets or creates a tracker for the given UI.
      Parameters:
      ui - the UI for which to get at tracker, not null
      Returns:
      the tracker for the given ui
    • ga

      public void ga(String command, Map<String, Serializable> fieldsObject, Serializable... fields)
      Sends a generic command to Google Analytics. This corresponds to a client-side call to the ga function except that fieldsObject is not the last parameter because of the way varargs work in Java.
      Parameters:
      command - the name of the command to send, not null
      fieldsObject - a map of additional fields, or null to to not send any additional fields
      fields - a list of field values to send
    • sendPageView

      public void sendPageView(String location)
      Sends a page view command to Google Analytics.
      Parameters:
      location - the location of the viewed page, not null
    • sendPageView

      public void sendPageView(String location, Map<String, Serializable> fieldsObject)
      Sends a page view command with arbitrary additional fields to Google Analytics. See the reference documentation for more information about supported additional fields.
      Parameters:
      location - the location of the viewed page, not null
      fieldsObject - map of additional fields to include in the send command
    • sendEvent

      public void sendEvent(String groupId, String eventName)
      Sends an event command with the given category and action. See the reference documentation for information about the semantics of the parameters.
      Parameters:
      groupId - the category name, not null
      eventName - the action name, not null
    • sendEvent

      public void sendEvent(String category, String action, String label)
      Sends an event command with the given category, action and label. See the reference documentation for information about the semantics of the parameters.
      Parameters:
      category - the category name, not null
      action - the action name, not null
      label - the event label, not null
    • sendEvent

      public void sendEvent(String category, String action, String label, int value)
      Sends an event command with the given category, action, label and value. See the reference documentation for information about the semantics of the parameters.
      Parameters:
      category - the category name, not null
      action - the action name, not null
      label - the event label, not null
      value - the event value
    • sendEvent

      public void sendEvent(String category, String action, Map<String, Serializable> fieldsObject)
      Sends an event command with the given category, action and arbitrary additional fields. See the reference documentation for information about the semantics of the parameters.
      Parameters:
      category - the category name, not null
      action - the action name, not null
      fieldsObject - field object
    • isInitialized

      public boolean isInitialized()
      Checks whether this tracker has been initialized.
      Returns:
      true if this tracker is initialized, otherwise false