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