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; 24 25use function str_ends_with; 26 27/** 28 * Class MatomoAnalyticsModule - add support for Matomo analytics. 29 */ 30class MatomoAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface 31{ 32 use ModuleAnalyticsTrait; 33 use ModuleConfigTrait; 34 use ModuleExternalUrlTrait; 35 use ModuleGlobalTrait; 36 37 /** 38 * How should this module be identified in the control panel, etc.? 39 * 40 * @return string 41 */ 42 public function title(): string 43 { 44 return I18N::translate('Matomo™ / Piwik™ analytics'); 45 } 46 47 /** 48 * Should this module be enabled when it is first installed? 49 * 50 * @return bool 51 */ 52 public function isEnabledByDefault(): bool 53 { 54 return false; 55 } 56 57 /** 58 * Form fields to edit the parameters. 59 * 60 * @return string 61 */ 62 public function analyticsFormFields(): string 63 { 64 return view('modules/matomo-analytics/form', $this->analyticsParameters()); 65 } 66 67 /** 68 * Home page for the service. 69 * 70 * @return string 71 */ 72 public function externalUrl(): string 73 { 74 return 'https://matomo.org'; 75 } 76 77 /** 78 * The parameters that need to be embedded in the snippet. 79 * 80 * @return array<string> 81 */ 82 public function analyticsParameters(): array 83 { 84 $matomo_site_id = $this->getPreference('MATOMO_SITE_ID'); 85 $matomo_url = $this->getPreference('MATOMO_URL'); 86 87 // Reports on the webtrees forum say that a trailing slash is required. 88 if (!str_ends_with($matomo_url, '/')) { 89 $matomo_url .= '/'; 90 } 91 92 return [ 93 'MATOMO_SITE_ID' => $matomo_site_id, 94 'MATOMO_URL' => $matomo_url, 95 ]; 96 } 97 98 /** 99 * Embed placeholders in the snippet. 100 * 101 * @param array<string> $parameters 102 * 103 * @return string 104 */ 105 public function analyticsSnippet(array $parameters): string 106 { 107 $parameters['user'] = Auth::user(); 108 109 return view('modules/matomo-analytics/snippet', $parameters); 110 } 111 112 /** 113 * Raw content, to be added at the end of the <head> element. 114 * Typically, this will be <link> and <meta> elements. 115 * 116 * @return string 117 */ 118 public function headContent(): string 119 { 120 if ($this->analyticsCanShow()) { 121 return $this->analyticsSnippet($this->analyticsParameters()); 122 } 123 124 return ''; 125 } 126} 127