xref: /webtrees/app/Module/GoogleAnalyticsModule.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
137eb8894SGreg Roach<?php
23976b470SGreg Roach
337eb8894SGreg Roach/**
437eb8894SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
637eb8894SGreg Roach * This program is free software: you can redistribute it and/or modify
737eb8894SGreg Roach * it under the terms of the GNU General Public License as published by
837eb8894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
937eb8894SGreg Roach * (at your option) any later version.
1037eb8894SGreg Roach * This program is distributed in the hope that it will be useful,
1137eb8894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1237eb8894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1337eb8894SGreg Roach * GNU General Public License for more details.
1437eb8894SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1637eb8894SGreg Roach */
17fcfa147eSGreg Roach
1837eb8894SGreg Roachdeclare(strict_types=1);
1937eb8894SGreg Roach
2037eb8894SGreg Roachnamespace Fisharebest\Webtrees\Module;
2137eb8894SGreg Roach
2237eb8894SGreg Roachuse Fisharebest\Webtrees\Auth;
230ad7d82dSGreg Roachuse Fisharebest\Webtrees\I18N;
24*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
2537eb8894SGreg Roachuse Fisharebest\Webtrees\Tree;
26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
270c0910bfSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
28f3874e19SGreg Roach
29f9f6fffdSGreg Roachuse function view;
30b55cbc6bSGreg Roach
3137eb8894SGreg Roach/**
3237eb8894SGreg Roach * Class GoogleAnalyticsModule - add support for Google analytics.
3337eb8894SGreg Roach */
34abafa13cSGreg Roachclass GoogleAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface
3537eb8894SGreg Roach{
3637eb8894SGreg Roach    use ModuleAnalyticsTrait;
378e5c5efeSGreg Roach    use ModuleConfigTrait;
388e5c5efeSGreg Roach    use ModuleExternalUrlTrait;
39abafa13cSGreg Roach    use ModuleGlobalTrait;
4037eb8894SGreg Roach
4137eb8894SGreg Roach    /**
420cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4337eb8894SGreg Roach     *
4437eb8894SGreg Roach     * @return string
4537eb8894SGreg Roach     */
465699f0a8SGreg Roach    public function title(): string
475699f0a8SGreg Roach    {
480ad7d82dSGreg Roach        return I18N::translate('Google™ analytics');
4937eb8894SGreg Roach    }
5037eb8894SGreg Roach
5137eb8894SGreg Roach    /**
52abafa13cSGreg Roach     * Should this module be enabled when it is first installed?
53abafa13cSGreg Roach     *
54abafa13cSGreg Roach     * @return bool
55abafa13cSGreg Roach     */
56abafa13cSGreg Roach    public function isEnabledByDefault(): bool
57abafa13cSGreg Roach    {
58abafa13cSGreg Roach        return false;
59abafa13cSGreg Roach    }
60abafa13cSGreg Roach
61abafa13cSGreg Roach    /**
6237eb8894SGreg Roach     * Form fields to edit the parameters.
6337eb8894SGreg Roach     *
6437eb8894SGreg Roach     * @return string
6537eb8894SGreg Roach     */
6637eb8894SGreg Roach    public function analyticsFormFields(): string
6737eb8894SGreg Roach    {
688e5c5efeSGreg Roach        return view('modules/google-analytics/form', $this->analyticsParameters());
6937eb8894SGreg Roach    }
7037eb8894SGreg Roach
7137eb8894SGreg Roach    /**
7237eb8894SGreg Roach     * Home page for the service.
7337eb8894SGreg Roach     *
7437eb8894SGreg Roach     * @return string
7537eb8894SGreg Roach     */
768e5c5efeSGreg Roach    public function externalUrl(): string
7737eb8894SGreg Roach    {
7837eb8894SGreg Roach        return 'https://www.google.com/analytics';
7937eb8894SGreg Roach    }
8037eb8894SGreg Roach
8137eb8894SGreg Roach    /**
8237eb8894SGreg Roach     * The parameters that need to be embedded in the snippet.
8337eb8894SGreg Roach     *
8424f2a3afSGreg Roach     * @return array<string>
8537eb8894SGreg Roach     */
8637eb8894SGreg Roach    public function analyticsParameters(): array
8737eb8894SGreg Roach    {
8837eb8894SGreg Roach        return [
89afc4f177SGreg Roach            'GOOGLE_ANALYTICS_ID' => $this->getPreference('GOOGLE_ANALYTICS_ID'),
9037eb8894SGreg Roach        ];
9137eb8894SGreg Roach    }
9237eb8894SGreg Roach
9337eb8894SGreg Roach    /**
9437eb8894SGreg Roach     * Embed placeholders in the snippet.
9537eb8894SGreg Roach     *
9609482a55SGreg Roach     * @param array<string> $parameters
9737eb8894SGreg Roach     *
9837eb8894SGreg Roach     * @return string
9937eb8894SGreg Roach     */
10037eb8894SGreg Roach    public function analyticsSnippet(array $parameters): string
10137eb8894SGreg Roach    {
102*d35568b4SGreg Roach        $request = Registry::container()->get(ServerRequestInterface::class);
1030c0910bfSGreg Roach
10437eb8894SGreg Roach        // Add extra dimensions (i.e. filtering categories)
105b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
106b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
10737eb8894SGreg Roach
108f9f6fffdSGreg Roach        if (str_starts_with($parameters['GOOGLE_ANALYTICS_ID'], 'UA-')) {
10937eb8894SGreg Roach            $parameters['dimensions'] = (object) [
11037eb8894SGreg Roach                'dimension1' => $tree instanceof Tree ? $tree->name() : '-',
11137eb8894SGreg Roach                'dimension2' => $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-',
11237eb8894SGreg Roach            ];
11337eb8894SGreg Roach
1148e5c5efeSGreg Roach            return view('modules/google-analytics/snippet', $parameters);
11537eb8894SGreg Roach        }
116abafa13cSGreg Roach
117f9f6fffdSGreg Roach        $parameters['tree_name'] = $tree instanceof Tree ? $tree->name() : '-';
118f9f6fffdSGreg Roach        $parameters['access_level'] = $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-';
119f9f6fffdSGreg Roach
120f9f6fffdSGreg Roach        return view('modules/google-analytics/snippet-v4', $parameters);
121f9f6fffdSGreg Roach    }
122f9f6fffdSGreg Roach
123abafa13cSGreg Roach    /**
124abafa13cSGreg Roach     * Raw content, to be added at the end of the <head> element.
125abafa13cSGreg Roach     * Typically, this will be <link> and <meta> elements.
126abafa13cSGreg Roach     *
127abafa13cSGreg Roach     * @return string
128abafa13cSGreg Roach     */
129abafa13cSGreg Roach    public function headContent(): string
130abafa13cSGreg Roach    {
131abafa13cSGreg Roach        if ($this->analyticsCanShow()) {
132abafa13cSGreg Roach            return $this->analyticsSnippet($this->analyticsParameters());
133abafa13cSGreg Roach        }
134abafa13cSGreg Roach
135abafa13cSGreg Roach        return '';
136abafa13cSGreg Roach    }
13737eb8894SGreg Roach}
138