You are here

class Event in Googalytics - Google Analytics 8

Class Event.

Hierarchy

Expanded class hierarchy of Event

1 file declares its use of Event
EventTest.php in tests/src/Unit/AnalyticsCommand/EventTest.php

File

src/AnalyticsCommand/Event.php, line 8

Namespace

Drupal\ga\AnalyticsCommand
View source
class Event extends Send {

  /**
   * The event category.
   *
   * @var string
   */
  protected $eventCategory;

  /**
   * The event action.
   *
   * @var string
   */
  protected $eventAction;

  /**
   * The event label.
   *
   * @var null|string
   */
  protected $eventLabel;

  /**
   * The event value.
   *
   * @var int
   */
  protected $eventValue;

  /**
   * Create constructor.
   *
   * @param string $event_category
   *   The event category.
   * @param string $event_action
   *   The event action.
   * @param string $event_label
   *   The event label (optional).
   * @param int $event_value
   *   The event value (optional).
   * @param array $fields_object
   *   A map of values for the command's fieldsObject parameter.
   * @param string $tracker_name
   *   The tracker name (optional).
   * @param int $priority
   *   The command priority.
   */
  public function __construct($event_category, $event_action, $event_label = NULL, $event_value = NULL, array $fields_object = [], $tracker_name = NULL, $priority = self::DEFAULT_PRIORITY) {
    if (!is_null($event_value) && (!is_int($event_value) || $event_value < 0)) {
      throw new \InvalidArgumentException("Event value must be a positive integer");
    }
    parent::__construct('event', $fields_object, $tracker_name, $priority);
    $this->eventCategory = $event_category;
    $this->eventAction = $event_action;
    $this->eventLabel = $event_label;
    $this->eventValue = $event_value;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingCommands() {
    $command = [
      ($this->trackerName ? $this->trackerName . '.' : '') . $this->command,
      $this->hitType,
      $this->eventCategory,
      $this->eventAction,
    ];
    $fieldsObject = $this->fieldsObject;
    if (!empty($this->eventLabel)) {
      $command[] = $this->eventLabel;
      if (!is_null($this->eventValue)) {
        $command[] = $this->eventValue;
      }
    }
    elseif (!is_null($this->eventValue)) {

      // If label is not specified but value is, the value must be set in
      // fieldsObject instead.
      $fieldsObject['eventValue'] = $this->eventValue;
    }
    if (!empty($fieldsObject)) {
      $command[] = $fieldsObject;
    }
    return [
      $command,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalSettingCommandsTrait::$priority protected property Priority integer.
DrupalSettingCommandsTrait::getPriority public function An integer value for sorting by priority.
Event::$eventAction protected property The event action.
Event::$eventCategory protected property The event category.
Event::$eventLabel protected property The event label.
Event::$eventValue protected property The event value.
Event::getSettingCommands public function An array of commands to be sent to Google Analytics. Overrides Send::getSettingCommands
Event::__construct public function Create constructor. Overrides Send::__construct
Generic::$command protected property The command name.
Generic::$fieldsObject protected property A map of values for the command's fieldsObject parameter.
Generic::$trackerName protected property The name of the tracker for this command.
Generic::getCommand public function Get the command name.
Generic::getFieldsObject public function Get the map of values for the command's fieldsObject parameter.
Generic::getTrackerName public function The tracker this command will be applied to, if specified.
Send::$hitType protected property The event hitType parameter.
Send::$hitTypes private static property
Send::DEFAULT_PRIORITY constant Overrides Generic::DEFAULT_PRIORITY 1