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