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