Pimcore\Google\Analytics::getCode PHP Метод

getCode() публичный статический Метод

public static getCode ( null $config = null ) : string
$config null
Результат string
    public static function getCode($config = null)
    {
        if (is_null($config)) {
            $config = self::getSiteConfig();
        }
        // do nothing if not configured
        if (!$config || !$config->trackid) {
            return "";
        }
        $codeBeforeInit = $config->additionalcodebeforeinit;
        $codeBeforePageview = $config->additionalcodebeforepageview;
        $codeBeforeEnd = $config->additionalcode;
        if (!empty(self::$additionalCodes["beforeInit"])) {
            $codeBeforeInit .= "\n" . implode("\n", self::$additionalCodes["beforeInit"]);
        }
        if (!empty(self::$additionalCodes["beforePageview"])) {
            $codeBeforePageview .= "\n" . implode("\n", self::$additionalCodes["beforePageview"]);
        }
        if (!empty(self::$additionalCodes["beforeEnd"])) {
            $codeBeforeEnd .= "\n" . implode("\n", self::$additionalCodes["beforeEnd"]);
        }
        $code = "";
        if ($config->asynchronouscode || $config->retargetingcode) {
            $typeSrc = "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';";
            if ($config->retargetingcode) {
                $typeSrc = "ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';";
            }
            $code .= "\n            <script type=\"text/javascript\">\n\n              " . $codeBeforeInit . "\n              var _gaq = _gaq || [];\n              _gaq.push(['_setAccount', '" . $config->trackid . "']);\n              _gaq.push (['_gat._anonymizeIp']);\n              " . $codeBeforePageview . "\n              if (typeof _gaqPageView != \"undefined\"){\n                _gaq.push(['_trackPageview',_gaqPageView]);\n              } else {\n                _gaq.push(['_trackPageview'" . (self::$defaultPath ? ",'" . self::$defaultPath . "'" : "") . "]);\n              }\n\n              " . $codeBeforeEnd . "\n\n              (function() {\n                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n                " . $typeSrc . "\n                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n              })();\n            </script>";
        } else {
            $code .= "\n            <script>\n              (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n              (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n              m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n              })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n\n              " . $codeBeforeInit . "\n\n              ga('create', '" . $config->trackid . "'" . ($config->universal_configuration ? "," . $config->universal_configuration : "") . ");\n              ga('set', 'anonymizeIp', true);\n              " . $codeBeforePageview . "\n              if (typeof _gaqPageView != \"undefined\"){\n                ga('send', 'pageview', _gaqPageView);\n              } else {\n                ga('send', 'pageview'" . (self::$defaultPath ? ",'" . self::$defaultPath . "'" : "") . ");\n              }\n\n              " . $codeBeforeEnd . "\n            </script>";
        }
        return $code;
    }

Usage Example

Пример #1
0
 /**
  *
  */
 public function dispatchLoopShutdown()
 {
     if (!Tool::isHtmlResponse($this->getResponse())) {
         return;
     }
     if ($this->enabled && ($code = AnalyticsHelper::getCode())) {
         // analytics
         $body = $this->getResponse()->getBody();
         // search for the end <head> tag, and insert the google analytics code before
         // this method is much faster than using simple_html_dom and uses less memory
         $headEndPosition = stripos($body, "</head>");
         if ($headEndPosition !== false) {
             $body = substr_replace($body, $code . "</head>", $headEndPosition, 7);
         }
         $this->getResponse()->setBody($body);
     }
 }