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