xref: /webtrees/app/Module/MatomoAnalyticsModule.php (revision 0b4092ed8e31e3588eb143d0ade6c4a411367da8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21
22/**
23 * Class MatomoAnalyticsModule - add support for Matomo analytics.
24 */
25class MatomoAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface
26{
27    use ModuleAnalyticsTrait;
28    use ModuleConfigTrait;
29    use ModuleExternalUrlTrait;
30    use ModuleGlobalTrait;
31
32    /**
33     * How should this module be identified in the control panel, etc.?
34     *
35     * @return string
36     */
37    public function title(): string
38    {
39        return 'Matomo™ / Piwik™ analytics';
40    }
41
42    /**
43     * Should this module be enabled when it is first installed?
44     *
45     * @return bool
46     */
47    public function isEnabledByDefault(): bool
48    {
49        return false;
50    }
51
52    /**
53     * Form fields to edit the parameters.
54     *
55     * @return string
56     */
57    public function analyticsFormFields(): string
58    {
59        return view('modules/matomo-analytics/form', $this->analyticsParameters());
60    }
61
62    /**
63     * Home page for the service.
64     *
65     * @return string
66     */
67    public function externalUrl(): string
68    {
69        return 'https://matomo.org';
70    }
71
72    /**
73     * The parameters that need to be embedded in the snippet.
74     *
75     * @return string[]
76     */
77    public function analyticsParameters(): array
78    {
79        return [
80            'MATOMO_SITE_ID' => $this->getPreference('MATOMO_SITE_ID'),
81            'MATOMO_URL'     => $this->getPreference('MATOMO_URL'),
82        ];
83    }
84
85    /**
86     * Embed placeholders in the snippet.
87     *
88     * @param string[] $parameters
89     *
90     * @return string
91     */
92    public function analyticsSnippet(array $parameters): string
93    {
94        $parameters['user'] = Auth::user();
95
96        return view('modules/matomo-analytics/snippet', $parameters);
97    }
98
99    /**
100     * Raw content, to be added at the end of the <head> element.
101     * Typically, this will be <link> and <meta> elements.
102     *
103     * @return string
104     */
105    public function headContent(): string
106    {
107        if ($this->analyticsCanShow()) {
108            return $this->analyticsSnippet($this->analyticsParameters());
109        }
110
111        return '';
112    }
113}
114