xref: /webtrees/app/Module/MatomoAnalyticsModule.php (revision 203ccf04b147f0ec286f7c6e225c38732cc1e521)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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;
24
25/**
26 * Class MatomoAnalyticsModule - add support for Matomo analytics.
27 */
28class MatomoAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface
29{
30    use ModuleAnalyticsTrait;
31    use ModuleConfigTrait;
32    use ModuleExternalUrlTrait;
33    use ModuleGlobalTrait;
34
35    /**
36     * How should this module be identified in the control panel, etc.?
37     *
38     * @return string
39     */
40    public function title(): string
41    {
42        return I18N::translate('Matomo™ / Piwik™ analytics');
43    }
44
45    /**
46     * Should this module be enabled when it is first installed?
47     *
48     * @return bool
49     */
50    public function isEnabledByDefault(): bool
51    {
52        return false;
53    }
54
55    /**
56     * Form fields to edit the parameters.
57     *
58     * @return string
59     */
60    public function analyticsFormFields(): string
61    {
62        return view('modules/matomo-analytics/form', $this->analyticsParameters());
63    }
64
65    /**
66     * Home page for the service.
67     *
68     * @return string
69     */
70    public function externalUrl(): string
71    {
72        return 'https://matomo.org';
73    }
74
75    /**
76     * The parameters that need to be embedded in the snippet.
77     *
78     * @return array<string>
79     */
80    public function analyticsParameters(): array
81    {
82        return [
83            'MATOMO_SITE_ID' => $this->getPreference('MATOMO_SITE_ID'),
84            'MATOMO_URL'     => $this->getPreference('MATOMO_URL'),
85        ];
86    }
87
88    /**
89     * Embed placeholders in the snippet.
90     *
91     * @param array<string> $parameters
92     *
93     * @return string
94     */
95    public function analyticsSnippet(array $parameters): string
96    {
97        $parameters['user'] = Auth::user();
98
99        return view('modules/matomo-analytics/snippet', $parameters);
100    }
101
102    /**
103     * Raw content, to be added at the end of the <head> element.
104     * Typically, this will be <link> and <meta> elements.
105     *
106     * @return string
107     */
108    public function headContent(): string
109    {
110        if ($this->analyticsCanShow()) {
111            return $this->analyticsSnippet($this->analyticsParameters());
112        }
113
114        return '';
115    }
116}
117