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