xref: /webtrees/app/Module/StatcounterModule.php (revision 2ebcf907ed34213f816592af04e6c160335d6311)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\I18N;
23
24/**
25 * Class StatcounterModule - add support for statcounter.
26 */
27class StatcounterModule 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 I18N::translate('Statcounter™');
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/statcounter/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://statcounter.com';
72    }
73
74    /**
75     * The parameters that need to be embedded in the snippet.
76     *
77     * @return array<string>
78     */
79    public function analyticsParameters(): array
80    {
81        return [
82            'STATCOUNTER_PROJECT_ID' => $this->getPreference('STATCOUNTER_PROJECT_ID'),
83            'STATCOUNTER_SECURITY_ID' => $this->getPreference('STATCOUNTER_SECURITY_ID'),
84        ];
85    }
86
87    /**
88     * Embed placeholders in the snippet.
89     *
90     * @param array<string> $parameters
91     *
92     * @return string
93     */
94    public function analyticsSnippet(array $parameters): string
95    {
96        return view('modules/statcounter/snippet', $parameters);
97    }
98
99    /**
100     * Raw content, to be added at the end of the <body> element.
101     * Typically, this will be <script> elements.
102     *
103     * @return string
104     */
105    public function bodyContent(): string
106    {
107        if ($this->analyticsCanShow()) {
108            return $this->analyticsSnippet($this->analyticsParameters());
109        }
110
111        return '';
112    }
113}
114