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