xref: /webtrees/app/Module/MatomoAnalyticsModule.php (revision c88147bf68caeb2b737b5803b640737427e748ca)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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
25use function str_ends_with;
26
27/**
28 * Class MatomoAnalyticsModule - add support for Matomo analytics.
29 */
30class MatomoAnalyticsModule 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('Matomo™ / Piwik™ 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/matomo-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://matomo.org';
75    }
76
77    /**
78     * The parameters that need to be embedded in the snippet.
79     *
80     * @return array<string>
81     */
82    public function analyticsParameters(): array
83    {
84        $matomo_site_id = $this->getPreference('MATOMO_SITE_ID');
85        $matomo_url     = $this->getPreference('MATOMO_URL');
86
87        if (!str_ends_with($matomo_url, '/')) {
88            $matomo_url .= '/';
89        }
90
91        return [
92            'MATOMO_SITE_ID' => $matomo_site_id,
93            'MATOMO_URL'     => $matomo_url,
94        ];
95    }
96
97    /**
98     * Embed placeholders in the snippet.
99     *
100     * @param array<string> $parameters
101     *
102     * @return string
103     */
104    public function analyticsSnippet(array $parameters): string
105    {
106        $parameters['user'] = Auth::user();
107
108        return view('modules/matomo-analytics/snippet', $parameters);
109    }
110
111    /**
112     * Raw content, to be added at the end of the <head> element.
113     * Typically, this will be <link> and <meta> elements.
114     *
115     * @return string
116     */
117    public function headContent(): string
118    {
119        if ($this->analyticsCanShow()) {
120            return $this->analyticsSnippet($this->analyticsParameters());
121        }
122
123        return '';
124    }
125}
126