You are here

class GoogleAnalyticsAccounts in Google Analytics 4.x

Hierarchy

Expanded class hierarchy of GoogleAnalyticsAccounts

1 file declares its use of GoogleAnalyticsAccounts
GoogleAnalyticsAdminSettingsForm.php in src/Form/GoogleAnalyticsAdminSettingsForm.php
1 string reference to 'GoogleAnalyticsAccounts'
google_analytics.services.yml in ./google_analytics.services.yml
google_analytics.services.yml
1 service uses GoogleAnalyticsAccounts
google_analytics.accounts in ./google_analytics.services.yml
Drupal\google_analytics\Helpers\GoogleAnalyticsAccounts

File

src/Helpers/GoogleAnalyticsAccounts.php, line 13

Namespace

Drupal\google_analytics\Helpers
View source
class GoogleAnalyticsAccounts implements GoogleAnalyticsInterface {

  /**
   * @var string
   */
  protected $privateKey;

  /**
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  private $config;

  /**
   * @var array
   */
  private $accounts;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\PrivateKey $private_key
   *   The private key service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, PrivateKey $private_key) {
    $this->config = $config_factory
      ->get('google_analytics.settings');
    $accounts = $this->config
      ->get('account');

    // Create the accounts array from either a single gtag id or multiple ones.
    if (strpos($accounts, ',') === FALSE) {
      $this->accounts[] = $accounts;
    }
    else {
      $this->accounts = explode(',', $accounts);
    }
    $this->privateKey = $private_key
      ->get();
  }

  /**
   * Generate user id hash to implement USER_ID.
   *
   * The USER_ID value should be a unique, persistent, and non-personally
   * identifiable string identifier that represents a user or signed-in
   * account across devices.
   *
   * @param int $uid
   *   User id.
   *
   * @return string
   *   User id hash.
   */
  public function getUserIdHash($uid) {
    return Crypt::hmacBase64($uid, $this->privateKey . Settings::getHashSalt());
  }
  public function getDefaultMeasurementId() {

    // The top UA- or G- Account is the default measurement ID.
    foreach ($this->accounts as $account) {
      if (preg_match(self::GOOGLE_ANALYTICS_TRACKING_MATCH, $account)) {
        return $account;
      }
    }
  }
  public function getAdditionalAccounts() {
    return array_filter($this->accounts, function ($v) {
      return $v !== $this
        ->getDefaultMeasurementId();
    });
  }
  public function getAccounts() {
    return $this->accounts;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleAnalyticsAccounts::$accounts private property
GoogleAnalyticsAccounts::$config private property
GoogleAnalyticsAccounts::$privateKey protected property
GoogleAnalyticsAccounts::getAccounts public function
GoogleAnalyticsAccounts::getAdditionalAccounts public function
GoogleAnalyticsAccounts::getDefaultMeasurementId public function
GoogleAnalyticsAccounts::getUserIdHash public function Generate user id hash to implement USER_ID.
GoogleAnalyticsAccounts::__construct public function Constructor.
GoogleAnalyticsInterface::GOOGLE_ANALYTICS_GTAG_MATCH constant Define the Acceptable GA ID Patterns
GoogleAnalyticsInterface::GOOGLE_ANALYTICS_TRACKFILES_EXTENSIONS constant Define the default file extension list that should be tracked as download.
GoogleAnalyticsInterface::GOOGLE_ANALYTICS_TRACKING_MATCH constant Define the Acceptable tracking ID patterns