xref: /webtrees/app/Module/GoogleAnalyticsModule.php (revision abafa13c257ec871f1fb1a1926f33c2748685c38)
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
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Contracts\UserInterface;
22use Fisharebest\Webtrees\Tree;
23
24/**
25 * Class GoogleAnalyticsModule - add support for Google analytics.
26 */
27class GoogleAnalyticsModule 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 'Google™ analytics';
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/google-analytics/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://www.google.com/analytics';
72    }
73
74    /**
75     * The parameters that need to be embedded in the snippet.
76     *
77     * @return string[]
78     */
79    public function analyticsParameters(): array
80    {
81        return [
82            'GOOGLE_WEBMASTER_ID' => $this->getPreference('GOOGLE_WEBMASTER_ID'),
83        ];
84    }
85
86    /**
87     * Embed placeholders in the snippet.
88     *
89     * @param string[] $parameters
90     *
91     * @return string
92     */
93    public function analyticsSnippet(array $parameters): string
94    {
95        // Add extra dimensions (i.e. filtering categories)
96        $tree = app(Tree::class);
97        $user = app(UserInterface::class);
98
99        $parameters['dimensions'] = (object) [
100            'dimension1' => $tree instanceof Tree ? $tree->name() : '-',
101            'dimension2' => $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-',
102        ];
103
104        return view('modules/google-analytics/snippet', $parameters);
105    }
106
107    /**
108     * Raw content, to be added at the end of the <head> element.
109     * Typically, this will be <link> and <meta> elements.
110     *
111     * @return string
112     */
113    public function headContent(): string
114    {
115        if ($this->analyticsCanShow()) {
116            return $this->analyticsSnippet($this->analyticsParameters());
117        }
118
119        return '';
120    }
121}
122