1<?php 2/* 3 * vim:set softtabstop=4 shiftwidth=4 expandtab: 4 * 5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 6 * Copyright 2001 - 2020 Ampache.org 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Affero General Public License for more details. 17 * 18 * You should have received a copy of the GNU Affero General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 * 21 */ 22declare(strict_types=0); 23 24namespace Ampache\Plugin; 25 26use Ampache\Repository\Model\Preference; 27use Ampache\Repository\Model\User; 28 29class AmpacheGoogleAnalytics 30{ 31 public $name = 'GoogleAnalytics'; 32 public $categories = 'stats'; 33 public $description = 'Google Analytics statistics'; 34 public $url = ''; 35 public $version = '000001'; 36 public $min_ampache = '370034'; 37 public $max_ampache = '999999'; 38 39 // These are internal settings used by this class, run this->load to fill them out 40 private $user; 41 private $tracking_id; 42 43 /** 44 * Constructor 45 * This function does nothing... 46 */ 47 public function __construct() 48 { 49 $this->description = T_('Google Analytics statistics'); 50 51 return true; 52 } 53 54 /** 55 * install 56 * This is a required plugin function. It inserts our preferences 57 * into Ampache 58 */ 59 public function install() 60 { 61 // Check and see if it's already installed 62 if (Preference::exists('googleanalytics_tracking_id')) { 63 return false; 64 } 65 66 Preference::insert('googleanalytics_tracking_id', T_('Google Analytics Tracking ID'), '', 100, 'string', 67 'plugins', $this->name); 68 69 return true; 70 } 71 72 /** 73 * uninstall 74 * This is a required plugin function. It removes our preferences from 75 * the database returning it to its original form 76 */ 77 public function uninstall() 78 { 79 Preference::delete('googleanalytics_tracking_id'); 80 81 return true; 82 } 83 84 /** 85 * upgrade 86 * This is a recommended plugin function 87 */ 88 public function upgrade() 89 { 90 return true; 91 } 92 93 /** 94 * display_user_field 95 * This display the module in user page 96 */ 97 public function display_on_footer() 98 { 99 echo "<!-- Google Analytics -->\n"; 100 echo "<script>\n"; 101 echo "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n"; 102 echo "(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n"; 103 echo "m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n"; 104 echo "})(window,document, 'script', '//www.google-analytics.com/analytics.js', 'ga');\n"; 105 echo "ga('create', '" . scrub_out($this->tracking_id) . "', 'auto');\n"; 106 echo "ga('send', 'pageview');\n"; 107 echo "</script>\n"; 108 } 109 110 /** 111 * load 112 * This loads up the data we need into this object, this stuff comes 113 * from the preferences. 114 * @param User $user 115 * @return boolean 116 */ 117 public function load($user) 118 { 119 $this->user = $user; 120 $user->set_preferences(); 121 $data = $user->prefs; 122 // load system when nothing is given 123 if (!strlen(trim($data['googleanalytics_tracking_id']))) { 124 $data = array(); 125 $data['googleanalytics_tracking_id'] = Preference::get_by_user(-1, 'googleanalytics_tracking_id'); 126 } 127 128 $this->tracking_id = trim($data['googleanalytics_tracking_id']); 129 if (!strlen($this->tracking_id)) { 130 debug_event('googleanalytics.plugin', 'No Tracking ID, user field plugin skipped', 3); 131 132 return false; 133 } 134 135 return true; 136 } 137} 138