1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Registry; 25use Fisharebest\Webtrees\Tree; 26use Fisharebest\Webtrees\Validator; 27use Psr\Http\Message\ServerRequestInterface; 28 29use function view; 30 31/** 32 * Class GoogleAnalyticsModule - add support for Google analytics. 33 */ 34class GoogleAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface 35{ 36 use ModuleAnalyticsTrait; 37 use ModuleConfigTrait; 38 use ModuleExternalUrlTrait; 39 use ModuleGlobalTrait; 40 41 /** 42 * How should this module be identified in the control panel, etc.? 43 * 44 * @return string 45 */ 46 public function title(): string 47 { 48 return I18N::translate('Google™ analytics'); 49 } 50 51 /** 52 * Should this module be enabled when it is first installed? 53 * 54 * @return bool 55 */ 56 public function isEnabledByDefault(): bool 57 { 58 return false; 59 } 60 61 /** 62 * Form fields to edit the parameters. 63 * 64 * @return string 65 */ 66 public function analyticsFormFields(): string 67 { 68 return view('modules/google-analytics/form', $this->analyticsParameters()); 69 } 70 71 /** 72 * Home page for the service. 73 * 74 * @return string 75 */ 76 public function externalUrl(): string 77 { 78 return 'https://www.google.com/analytics'; 79 } 80 81 /** 82 * The parameters that need to be embedded in the snippet. 83 * 84 * @return array<string> 85 */ 86 public function analyticsParameters(): array 87 { 88 return [ 89 'GOOGLE_ANALYTICS_ID' => $this->getPreference('GOOGLE_ANALYTICS_ID'), 90 ]; 91 } 92 93 /** 94 * Embed placeholders in the snippet. 95 * 96 * @param array<string> $parameters 97 * 98 * @return string 99 */ 100 public function analyticsSnippet(array $parameters): string 101 { 102 $request = Registry::container()->get(ServerRequestInterface::class); 103 104 // Add extra dimensions (i.e. filtering categories) 105 $tree = Validator::attributes($request)->treeOptional(); 106 $user = Validator::attributes($request)->user(); 107 108 if (str_starts_with($parameters['GOOGLE_ANALYTICS_ID'], 'UA-')) { 109 $parameters['dimensions'] = (object) [ 110 'dimension1' => $tree instanceof Tree ? $tree->name() : '-', 111 'dimension2' => $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-', 112 ]; 113 114 return view('modules/google-analytics/snippet', $parameters); 115 } 116 117 $parameters['tree_name'] = $tree instanceof Tree ? $tree->name() : '-'; 118 $parameters['access_level'] = $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-'; 119 120 return view('modules/google-analytics/snippet-v4', $parameters); 121 } 122 123 /** 124 * Raw content, to be added at the end of the <head> element. 125 * Typically, this will be <link> and <meta> elements. 126 * 127 * @return string 128 */ 129 public function headContent(): string 130 { 131 if ($this->analyticsCanShow()) { 132 return $this->analyticsSnippet($this->analyticsParameters()); 133 } 134 135 return ''; 136 } 137} 138