xref: /webtrees/app/Module/GoogleAnalyticsModule.php (revision ad7270802ba57a28d195fc58fcc55f26458a779a)
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
28{
29    use ModuleAnalyticsTrait;
30    use ModuleConfigTrait;
31    use ModuleExternalUrlTrait;
32
33    /**
34     * How should this module be labelled on tabs, menus, etc.?
35     *
36     * @return string
37     */
38    public function title(): string
39    {
40        return 'Google™ analytics';
41    }
42
43    /**
44     * Form fields to edit the parameters.
45     *
46     * @return string
47     */
48    public function analyticsFormFields(): string
49    {
50        return view('modules/google-analytics/form', $this->analyticsParameters());
51    }
52
53    /**
54     * Home page for the service.
55     *
56     * @return string
57     */
58    public function externalUrl(): string
59    {
60        return 'https://www.google.com/analytics';
61    }
62
63    /**
64     * The parameters that need to be embedded in the snippet.
65     *
66     * @return string[]
67     */
68    public function analyticsParameters(): array
69    {
70        return [
71            'GOOGLE_WEBMASTER_ID' => $this->getPreference('GOOGLE_WEBMASTER_ID'),
72        ];
73    }
74
75    /**
76     * Embed placeholders in the snippet.
77     *
78     * @param string[] $parameters
79     *
80     * @return string
81     */
82    public function analyticsSnippet(array $parameters): string
83    {
84        // Add extra dimensions (i.e. filtering categories)
85        $tree = app()->make(Tree::class);
86        $user = app()->make(UserInterface::class);
87
88        $parameters['dimensions'] = (object) [
89            'dimension1' => $tree instanceof Tree ? $tree->name() : '-',
90            'dimension2' => $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-',
91        ];
92
93        return view('modules/google-analytics/snippet', $parameters);
94    }
95}
96