You are here

public function GoogleAnalyticResponseSubscriber::getTag 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::getTag()

Return the text for the tag.

Parameters

bool $compact: Whether or not the tag should be compacted (whitespace removed).

Return value

string The full text of the Google Analytic script/embed.

1 call to GoogleAnalyticResponseSubscriber::getTag()
GoogleAnalyticResponseSubscriber::addTag in src/EventSubscriber/GoogleAnalyticResponseSubscriber.php
Add a tags in boby.

File

src/EventSubscriber/GoogleAnalyticResponseSubscriber.php, line 83

Class

GoogleAnalyticResponseSubscriber
Class GoogleAnalyticResponseSubscriber.

Namespace

Drupal\multidomain_google_analytics\EventSubscriber

Code

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;
}