137eb8894SGreg Roach<?php 2*3976b470SGreg Roach 337eb8894SGreg Roach/** 437eb8894SGreg Roach * webtrees: online genealogy 537eb8894SGreg Roach * Copyright (C) 2019 webtrees development team 637eb8894SGreg Roach * This program is free software: you can redistribute it and/or modify 737eb8894SGreg Roach * it under the terms of the GNU General Public License as published by 837eb8894SGreg Roach * the Free Software Foundation, either version 3 of the License, or 937eb8894SGreg Roach * (at your option) any later version. 1037eb8894SGreg Roach * This program is distributed in the hope that it will be useful, 1137eb8894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1237eb8894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1337eb8894SGreg Roach * GNU General Public License for more details. 1437eb8894SGreg Roach * You should have received a copy of the GNU General Public License 1537eb8894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1637eb8894SGreg Roach */ 1737eb8894SGreg Roachdeclare(strict_types=1); 1837eb8894SGreg Roach 1937eb8894SGreg Roachnamespace Fisharebest\Webtrees\Module; 2037eb8894SGreg Roach 2176fa3e53SGreg Roachuse Fisharebest\Webtrees\Auth; 2276fa3e53SGreg Roach 2337eb8894SGreg Roach/** 2437eb8894SGreg Roach * Class MatomoAnalyticsModule - add support for Matomo analytics. 2537eb8894SGreg Roach */ 26abafa13cSGreg Roachclass MatomoAnalyticsModule extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface 2737eb8894SGreg Roach{ 2837eb8894SGreg Roach use ModuleAnalyticsTrait; 298e5c5efeSGreg Roach use ModuleConfigTrait; 308e5c5efeSGreg Roach use ModuleExternalUrlTrait; 31abafa13cSGreg Roach use ModuleGlobalTrait; 3237eb8894SGreg Roach 3337eb8894SGreg Roach /** 340cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 3537eb8894SGreg Roach * 3637eb8894SGreg Roach * @return string 3737eb8894SGreg Roach */ 3837eb8894SGreg Roach public function title(): string 3937eb8894SGreg Roach { 4037eb8894SGreg Roach return 'Matomo™ / Piwik™ analytics'; 4137eb8894SGreg Roach } 4237eb8894SGreg Roach 4337eb8894SGreg Roach /** 44abafa13cSGreg Roach * Should this module be enabled when it is first installed? 45abafa13cSGreg Roach * 46abafa13cSGreg Roach * @return bool 47abafa13cSGreg Roach */ 48abafa13cSGreg Roach public function isEnabledByDefault(): bool 49abafa13cSGreg Roach { 50abafa13cSGreg Roach return false; 51abafa13cSGreg Roach } 52abafa13cSGreg Roach 53abafa13cSGreg Roach /** 5437eb8894SGreg Roach * Form fields to edit the parameters. 5537eb8894SGreg Roach * 5637eb8894SGreg Roach * @return string 5737eb8894SGreg Roach */ 5837eb8894SGreg Roach public function analyticsFormFields(): string 5937eb8894SGreg Roach { 608e5c5efeSGreg Roach return view('modules/matomo-analytics/form', $this->analyticsParameters()); 6137eb8894SGreg Roach } 6237eb8894SGreg Roach 6337eb8894SGreg Roach /** 6437eb8894SGreg Roach * Home page for the service. 6537eb8894SGreg Roach * 6637eb8894SGreg Roach * @return string 6737eb8894SGreg Roach */ 688e5c5efeSGreg Roach public function externalUrl(): string 6937eb8894SGreg Roach { 7037eb8894SGreg Roach return 'https://matomo.org'; 7137eb8894SGreg Roach } 7237eb8894SGreg Roach 7337eb8894SGreg Roach /** 7437eb8894SGreg Roach * The parameters that need to be embedded in the snippet. 7537eb8894SGreg Roach * 7637eb8894SGreg Roach * @return string[] 7737eb8894SGreg Roach */ 7837eb8894SGreg Roach public function analyticsParameters(): array 7937eb8894SGreg Roach { 8037eb8894SGreg Roach return [ 8126420f84SGreg Roach 'MATOMO_SITE_ID' => $this->getPreference('MATOMO_SITE_ID'), 8226420f84SGreg Roach 'MATOMO_URL' => $this->getPreference('MATOMO_URL'), 8337eb8894SGreg Roach ]; 8437eb8894SGreg Roach } 8537eb8894SGreg Roach 8637eb8894SGreg Roach /** 8737eb8894SGreg Roach * Embed placeholders in the snippet. 8837eb8894SGreg Roach * 8937eb8894SGreg Roach * @param string[] $parameters 9037eb8894SGreg Roach * 9137eb8894SGreg Roach * @return string 9237eb8894SGreg Roach */ 9337eb8894SGreg Roach public function analyticsSnippet(array $parameters): string 9437eb8894SGreg Roach { 9576fa3e53SGreg Roach $parameters['user'] = Auth::user(); 9676fa3e53SGreg Roach 978e5c5efeSGreg Roach return view('modules/matomo-analytics/snippet', $parameters); 9837eb8894SGreg Roach } 99abafa13cSGreg Roach 100abafa13cSGreg Roach /** 101abafa13cSGreg Roach * Raw content, to be added at the end of the <head> element. 102abafa13cSGreg Roach * Typically, this will be <link> and <meta> elements. 103abafa13cSGreg Roach * 104abafa13cSGreg Roach * @return string 105abafa13cSGreg Roach */ 106abafa13cSGreg Roach public function headContent(): string 107abafa13cSGreg Roach { 108abafa13cSGreg Roach if ($this->analyticsCanShow()) { 109abafa13cSGreg Roach return $this->analyticsSnippet($this->analyticsParameters()); 110abafa13cSGreg Roach } 111abafa13cSGreg Roach 112abafa13cSGreg Roach return ''; 113abafa13cSGreg Roach } 11437eb8894SGreg Roach} 115