You are here

class GoogleAnalyticResponseSubscriber in Multidomain Google Analytics 8.2

Same name and namespace in other branches
  1. 8 src/EventSubscriber/GoogleAnalyticResponseSubscriber.php \Drupal\multidomain_google_analytics\EventSubscriber\GoogleAnalyticResponseSubscriber

Class GoogleAnalyticResponseSubscriber.

@package Drupal\multidomain_google_analytics\EventSubscriber

Hierarchy

  • class \Drupal\multidomain_google_analytics\EventSubscriber\GoogleAnalyticResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of GoogleAnalyticResponseSubscriber

1 string reference to 'GoogleAnalyticResponseSubscriber'
multidomain_google_analytics.services.yml in ./multidomain_google_analytics.services.yml
multidomain_google_analytics.services.yml
1 service uses GoogleAnalyticResponseSubscriber
multidomain_google_analytics.response_subscriber in ./multidomain_google_analytics.services.yml
Drupal\multidomain_google_analytics\EventSubscriber\GoogleAnalyticResponseSubscriber

File

src/EventSubscriber/GoogleAnalyticResponseSubscriber.php, line 16

Namespace

Drupal\multidomain_google_analytics\EventSubscriber
View source
class GoogleAnalyticResponseSubscriber implements EventSubscriberInterface {

  /**
   * The config object for the multidomain_google_analytics settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The DomainNegotiator.
   *
   * @var \Drupal\domain\DomainNegotiatorInterface
   */
  protected $activeDomain;

  /**
   * Constructs a new Google Analytics response subscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory service.
   * @param \Drupal\domain\DomainNegotiatorInterface $negotiator
   *   The domain negotiator service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, DomainNegotiatorInterface $negotiator) {
    $this->config = $configFactory
      ->get('multidomain_google_analytics.settings');
    if ($negotiator
      ->getActiveDomain()) {
      $this->activeDomain = $negotiator
        ->getActiveId();
    }
  }

  /**
   * Add a tags in boby.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *
   *   Set response.
   */
  public function addTag(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    $response = $event
      ->getResponse();
    $compact = '';
    if ($this->activeDomain) {
      $compact = $this->config
        ->get($this->activeDomain);
    }

    // Insert snippet after the opening body tag.
    if ($compact) {
      $response_text = preg_replace('@<body[^>]*>@', '$0' . $this
        ->getTag($compact), $response
        ->getContent(), 1);
      if ($response_text) {
        $response
          ->setContent($response_text);
      }
    }
  }

  /**
   * Return the text for the tag.
   *
   * @param bool $compact
   *   Whether or not the tag should be compacted (whitespace removed).
   *
   * @return string
   *   The full text of the Google Analytic script/embed.
   */
  public function getTag($compact = FALSE) {

    // Build script tags.
    $script = [];
    $script = <<<EOS
      <script>
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
        ga('create', '{<span class="php-variable">$compact</span>}', 'auto');
        ga('require', 'displayfeatures');
        ga('send', 'pageview');
      </script>
EOS;
    if ($compact) {
      $script = str_replace([
        "\n",
        '  ',
      ], '', $script);
    }
    $script = <<<EOS
    <!-- Google Analytics -->
    {<span class="php-variable">$script</span>}
    <!-- End Google Analytics -->
EOS;
    return $script;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'addTag',
      -500,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleAnalyticResponseSubscriber::$activeDomain protected property The DomainNegotiator.
GoogleAnalyticResponseSubscriber::$config protected property The config object for the multidomain_google_analytics settings.
GoogleAnalyticResponseSubscriber::addTag public function Add a tags in boby.
GoogleAnalyticResponseSubscriber::getSubscribedEvents public static function
GoogleAnalyticResponseSubscriber::getTag public function Return the text for the tag.
GoogleAnalyticResponseSubscriber::__construct public function Constructs a new Google Analytics response subscriber.