xref: /webtrees/app/Module/GoogleAnalyticsModule.php (revision de8e5a9314a703c1c8e6cddae583c822c4aa45d8)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Tree;
24use Psr\Http\Message\ServerRequestInterface;
25
26use function assert;
27
28/**
29 * Class GoogleAnalyticsModule - add support for Google analytics.
30 */
31class GoogleAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface
32{
33    use ModuleAnalyticsTrait;
34    use ModuleConfigTrait;
35    use ModuleExternalUrlTrait;
36    use ModuleGlobalTrait;
37
38    /**
39     * How should this module be identified in the control panel, etc.?
40     *
41     * @return string
42     */
43    public function title(): string
44    {
45        return 'Google™ analytics';
46    }
47
48    /**
49     * Should this module be enabled when it is first installed?
50     *
51     * @return bool
52     */
53    public function isEnabledByDefault(): bool
54    {
55        return false;
56    }
57
58    /**
59     * Form fields to edit the parameters.
60     *
61     * @return string
62     */
63    public function analyticsFormFields(): string
64    {
65        return view('modules/google-analytics/form', $this->analyticsParameters());
66    }
67
68    /**
69     * Home page for the service.
70     *
71     * @return string
72     */
73    public function externalUrl(): string
74    {
75        return 'https://www.google.com/analytics';
76    }
77
78    /**
79     * The parameters that need to be embedded in the snippet.
80     *
81     * @return string[]
82     */
83    public function analyticsParameters(): array
84    {
85        return [
86            'GOOGLE_WEBMASTER_ID' => $this->getPreference('GOOGLE_WEBMASTER_ID'),
87        ];
88    }
89
90    /**
91     * Embed placeholders in the snippet.
92     *
93     * @param string[] $parameters
94     *
95     * @return string
96     */
97    public function analyticsSnippet(array $parameters): string
98    {
99        $request = app(ServerRequestInterface::class);
100
101        // Add extra dimensions (i.e. filtering categories)
102        $tree = $request->getAttribute('tree');
103        $user = $request->getAttribute('user');
104
105        $parameters['dimensions'] = (object) [
106            'dimension1' => $tree instanceof Tree ? $tree->name() : '-',
107            'dimension2' => $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-',
108        ];
109
110        return view('modules/google-analytics/snippet', $parameters);
111    }
112
113    /**
114     * Raw content, to be added at the end of the <head> element.
115     * Typically, this will be <link> and <meta> elements.
116     *
117     * @return string
118     */
119    public function headContent(): string
120    {
121        if ($this->analyticsCanShow()) {
122            return $this->analyticsSnippet($this->analyticsParameters());
123        }
124
125        return '';
126    }
127}
128