xref: /webtrees/app/Module/MatomoAnalyticsModule.php (revision a2a24e20da9f20295005e2bd13644c6fa2f64c4c)
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_SITE_ID' => $this->getPreference('MATOMO_SITE_ID'),
79            'MATOMO_URL'     => $this->getPreference('MATOMO_URL'),
80        ];
81    }
82
83    /**
84     * Embed placeholders in the snippet.
85     *
86     * @param string[] $parameters
87     *
88     * @return string
89     */
90    public function analyticsSnippet(array $parameters): string
91    {
92        return view('modules/matomo-analytics/snippet', $parameters);
93    }
94
95    /**
96     * Raw content, to be added at the end of the <head> element.
97     * Typically, this will be <link> and <meta> elements.
98     *
99     * @return string
100     */
101    public function headContent(): string
102    {
103        if ($this->analyticsCanShow()) {
104            return $this->analyticsSnippet($this->analyticsParameters());
105        }
106
107        return '';
108    }
109}
110