1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\Tree; 25use Fisharebest\Webtrees\Validator; 26use Psr\Http\Message\ServerRequestInterface; 27 28use function assert; 29 30/** 31 * Class GoogleAnalyticsModule - add support for Google analytics. 32 */ 33class GoogleAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface 34{ 35 use ModuleAnalyticsTrait; 36 use ModuleConfigTrait; 37 use ModuleExternalUrlTrait; 38 use ModuleGlobalTrait; 39 40 /** 41 * How should this module be identified in the control panel, etc.? 42 * 43 * @return string 44 */ 45 public function title(): string 46 { 47 return I18N::translate('Google™ analytics'); 48 } 49 50 /** 51 * Should this module be enabled when it is first installed? 52 * 53 * @return bool 54 */ 55 public function isEnabledByDefault(): bool 56 { 57 return false; 58 } 59 60 /** 61 * Form fields to edit the parameters. 62 * 63 * @return string 64 */ 65 public function analyticsFormFields(): string 66 { 67 return view('modules/google-analytics/form', $this->analyticsParameters()); 68 } 69 70 /** 71 * Home page for the service. 72 * 73 * @return string 74 */ 75 public function externalUrl(): string 76 { 77 return 'https://www.google.com/analytics'; 78 } 79 80 /** 81 * The parameters that need to be embedded in the snippet. 82 * 83 * @return array<string> 84 */ 85 public function analyticsParameters(): array 86 { 87 return [ 88 'GOOGLE_ANALYTICS_ID' => $this->getPreference('GOOGLE_ANALYTICS_ID'), 89 ]; 90 } 91 92 /** 93 * Embed placeholders in the snippet. 94 * 95 * @param array<string> $parameters 96 * 97 * @return string 98 */ 99 public function analyticsSnippet(array $parameters): string 100 { 101 $request = app(ServerRequestInterface::class); 102 assert($request instanceof ServerRequestInterface); 103 104 // Add extra dimensions (i.e. filtering categories) 105 $tree = Validator::attributes($request)->treeOptional(); 106 $user = Validator::attributes($request)->user(); 107 108 $parameters['dimensions'] = (object) [ 109 'dimension1' => $tree instanceof Tree ? $tree->name() : '-', 110 'dimension2' => $tree instanceof Tree ? Auth::accessLevel($tree, $user) : '-', 111 ]; 112 113 return view('modules/google-analytics/snippet', $parameters); 114 } 115 116 /** 117 * Raw content, to be added at the end of the <head> element. 118 * Typically, this will be <link> and <meta> elements. 119 * 120 * @return string 121 */ 122 public function headContent(): string 123 { 124 if ($this->analyticsCanShow()) { 125 return $this->analyticsSnippet($this->analyticsParameters()); 126 } 127 128 return ''; 129 } 130} 131