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