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