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