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