xref: /webtrees/app/Module/GoogleAnalyticsModule.php (revision f7ab47b1fce1949e7b44a4f7d92d527a70deb743)
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 InvalidArgumentException;
25use Psr\Http\Message\ServerRequestInterface;
26
27use function assert;
28
29/**
30 * Class GoogleAnalyticsModule - add support for Google analytics.
31 */
32class GoogleAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface
33{
34    use ModuleAnalyticsTrait;
35    use ModuleConfigTrait;
36    use ModuleExternalUrlTrait;
37    use ModuleGlobalTrait;
38
39    /**
40     * How should this module be identified in the control panel, etc.?
41     *
42     * @return string
43     */
44    public function title(): string
45    {
46        return 'Google™ analytics';
47    }
48
49    /**
50     * Should this module be enabled when it is first installed?
51     *
52     * @return bool
53     */
54    public function isEnabledByDefault(): bool
55    {
56        return false;
57    }
58
59    /**
60     * Form fields to edit the parameters.
61     *
62     * @return string
63     */
64    public function analyticsFormFields(): string
65    {
66        return view('modules/google-analytics/form', $this->analyticsParameters());
67    }
68
69    /**
70     * Home page for the service.
71     *
72     * @return string
73     */
74    public function externalUrl(): string
75    {
76        return 'https://www.google.com/analytics';
77    }
78
79    /**
80     * The parameters that need to be embedded in the snippet.
81     *
82     * @return string[]
83     */
84    public function analyticsParameters(): array
85    {
86        return [
87            'GOOGLE_WEBMASTER_ID' => $this->getPreference('GOOGLE_WEBMASTER_ID'),
88        ];
89    }
90
91    /**
92     * Embed placeholders in the snippet.
93     *
94     * @param string[] $parameters
95     *
96     * @return string
97     */
98    public function analyticsSnippet(array $parameters): string
99    {
100        $request = app(ServerRequestInterface::class);
101
102        // Add extra dimensions (i.e. filtering categories)
103        $tree = $request->getAttribute('tree');
104        assert($tree instanceof Tree, new InvalidArgumentException());
105
106        $user = $request->getAttribute('user');
107
108        $parameters['dimensions'] = (object) [
109            'dimension1' => $tree instanceof Tree ? $tree->name() : '-',
110            'dimension2' => $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-',
111        ];
112
113        return view('modules/google-analytics/snippet', $parameters);
114    }
115
116    /**
117     * Raw content, to be added at the end of the <head> element.
118     * Typically, this will be <link> and <meta> elements.
119     *
120     * @return string
121     */
122    public function headContent(): string
123    {
124        if ($this->analyticsCanShow()) {
125            return $this->analyticsSnippet($this->analyticsParameters());
126        }
127
128        return '';
129    }
130}
131