File size: 1,153 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace Mautic\CoreBundle\Twig\Helper;

use Mautic\CoreBundle\Helper\CoreParametersHelper;

final class AnalyticsHelper
{
    private string $code;

    public function __construct(CoreParametersHelper $parametersHelper)
    {
        $this->code = htmlspecialchars_decode((string) $parametersHelper->get('google_analytics'));
    }

    public function getCode(): string
    {
        return $this->code;
    }

    /**
     * @param string $content
     */
    public function addCode($content): string
    {
        // Add analytics
        $analytics = $this->getCode();

        // Check for html doc
        if (!str_contains($content, '<html')) {
            $content = "<html>\n<head>{$analytics}</head>\n<body>{$content}</body>\n</html>";
        } elseif (!str_contains($content, '<head>')) {
            $content = str_replace('<html>', "<html>\n<head>\n{$analytics}\n</head>", $content);
        } elseif (!empty($analytics)) {
            $content = str_replace('</head>', $analytics."\n</head>", $content);
        }

        return $content;
    }

    public function getName(): string
    {
        return 'analytics';
    }
}