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