GoogleAnalyticsAccounts.php in Google Analytics 4.x
File
src/Helpers/GoogleAnalyticsAccounts.php
View source
<?php
namespace Drupal\google_analytics\Helpers;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\PrivateKey;
use Drupal\Core\Site\Settings;
use Drupal\google_analytics\GoogleAnalyticsInterface;
class GoogleAnalyticsAccounts implements GoogleAnalyticsInterface {
protected $privateKey;
private $config;
private $accounts;
public function __construct(ConfigFactoryInterface $config_factory, PrivateKey $private_key) {
$this->config = $config_factory
->get('google_analytics.settings');
$accounts = $this->config
->get('account');
if (strpos($accounts, ',') === FALSE) {
$this->accounts[] = $accounts;
}
else {
$this->accounts = explode(',', $accounts);
}
$this->privateKey = $private_key
->get();
}
public function getUserIdHash($uid) {
return Crypt::hmacBase64($uid, $this->privateKey . Settings::getHashSalt());
}
public function getDefaultMeasurementId() {
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;
}
}