PluginProbe
Simple Matomo Tracking Code / 0.5.2
Simple Matomo Tracking Code v0.5.2
trunk 0.5.1 0.5.2 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1
simple-matomo-tracking-code / simple-matomo-tracking-code.php

simple-matomo-tracking-code.php in Simple Matomo Tracking Code 0.5.2, at simple-matomo-tracking-code.php

298 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Simple Matomo Tracking Code
4 * Plugin URI: http://www.rolandbaer.ch/software/wordpress/simple-matomo-tracking-code/
5 * Description: This plugin makes it simple to add Matomo Web Analytics code to your WebSite.
6 * Version: 0.5.2
7 * Author: Roland Bär
8 * Author URI: http://www.rolandbaer.ch/
9 * Text Domain: simple-matomo-tracking-code
10 * License: GPLv3
11 *
12 * Based on Jules Stuifbergen's Piwik Analytics plugin
13 */
14
15 // If this file is called directly, abort.
16 if ( ! defined( 'WPINC' ) ) {
17 die;
18 }
19
20 /*
21 * Admin User Interface
22 */
23
24 if ( ! class_exists( 'SMTC_Admin' ) ) {
25
26 class SMTC_Admin {
27
28 static function add_config_page() {
29 global $wpdb;
30 if ( function_exists('add_options_page') ) {
31 add_options_page('Simple Matomo Tracking Code Configuration', 'Simple Matomo Tracking Code', 'manage_options', basename(__FILE__), array('SMTC_Admin','config_page'));
32 }
33 }
34
35 function restore_defaults() {
36 $options['siteid'] = 1;
37 $options['matomo_host'] = '';
38 $options['matomo_baseurl'] = '/matomo/';
39 $options['admintracking'] = false;
40 $options['dltracking'] = true;
41 update_option('MatomoAnalyticsPP',$options);
42 }
43
44 function init() {
45 $options = get_option('MatomoAnalyticsPP');
46 if (empty($options)) {
47 $this->restore_defaults();
48 }
49 }
50
51 static function sanitize_siteid( $value ) {
52 // remove invalid characters
53 $value = preg_replace( '$[^0-9]*$', '', $value );
54 return (int) $value;
55 }
56
57 static function config_page() {
58 if (isset($_GET['reset']) && $_GET['reset'] == "true") {
59 restore_defaults();
60 }
61
62 if ( isset($_POST['submit']) ) {
63 if (!current_user_can('manage_options')) die(__('You cannot edit the Simple Matomo Tracking Code options.'));
64 check_admin_referer('analyticspp-config');
65 $siteid = SMTC_Admin::sanitize_siteid($_POST['siteid']);
66 if( $siteid> 0 ) {
67 $options['siteid'] = $siteid;
68 }
69
70 if (isset($_POST['matomo_baseurl'])) {
71 $options['matomo_baseurl'] = strtolower(sanitize_text_field($_POST['matomo_baseurl']));
72 }
73
74 if (isset($_POST['matomo_host'])) {
75 $options['matomo_host'] = strtolower(sanitize_text_field($_POST['matomo_host']));
76 }
77
78 if (isset($_POST['dltracking'])) {
79 $options['dltracking'] = true;
80 } else {
81 $options['dltracking'] = false;
82 }
83
84 if (isset($_POST['admintracking'])) {
85 $options['admintracking'] = true;
86 } else {
87 $options['admintracking'] = false;
88 }
89
90 update_option('MatomoAnalyticsPP', $options);
91 }
92
93 $options = get_option('MatomoAnalyticsPP');
94 ?>
95 <div class="wrap">
96 <script type="text/javascript">
97 function toggle_help(ele, ele2) {
98 var expl = document.getElementById(ele2);
99 if (expl.style.display == "block") {
100 expl.style.display = "none";
101 ele.innerHTML = "What's this?";
102 } else {
103 expl.style.display = "block";
104 ele.innerHTML = "Hide explanation";
105 }
106 }
107 </script>
108 <h2>Simple Matomo Tracking Code Configuration</h2>
109 <form action="" method="post" id="analytics-conf">
110 <table class="form-table" style="width:100%;">
111 <?php
112 if ( function_exists('wp_nonce_field') )
113 wp_nonce_field('analyticspp-config');
114 ?>
115 <p>Matomo, formerly known as Piwik, is a downloadable web analytics software platform
116 free of charge under the GPL license.<br />
117 If you don't have Matomo installed, you can get it at
118 <a href="https://matomo.org/">matomo.org</a>.</p>
119
120 <tr>
121 <th scope="row" valign="top">
122 <label for="siteid">Matomo site id</label>
123 </th>
124 <td>
125 <input id="siteid" name="siteid" class="small-text" type="number" size="3" maxlength="4" value="<?php echo $options['siteid']; ?>" /><br/>
126 <div id="expl">
127 <p>In the Matomo interface, when you "Add Website"
128 you are shown a piece of JavaScript that
129 you are told to insert into the page, in that script is a
130 unique string that identifies the website you
131 just defined, that is your site ID (usually "1").
132 <p>Once you have entered your site id in
133 the box above your pages will be trackable by
134 Matomo Web Analytics.</p>
135 </div>
136 </td>
137 </tr>
138 <tr>
139 <th scope="row" valign="top">
140 <label for="dltracking">Track downloads</label><br/>
141 <small>(default is YES)</small>
142 </th>
143 <td>
144 <input type="checkbox" id="dltracking" name="dltracking" <?php if ($options['dltracking']) echo ' checked="unchecked" '; ?>/>
145 </td>
146 </tr>
147 <tr>
148 <th scope="row" valign="top">
149 <label for="matomo_host">Hostname of the matomo server (optional)</label>
150 </th>
151 <td>
152 <input id="matomo_host" name="matomo_host" type="text" size="40" maxlength="99" value="<?php echo $options['matomo_host']; ?>" /><br/>
153 <div id="expl3">
154 <p>Example: www.yourdomain.com -- Leave blank (default) if this is the same as your website.
155 Do NOT include the http(s):// bit.</p>
156 </div>
157 </td>
158 </tr>
159 <tr>
160 <th scope="row" valign="top">
161 <label for="matomo_baseurl">Base URL path of matomo installation</label>
162 </th>
163 <td>
164 <input id="matomo_baseurl" name="matomo_baseurl" type="text" size="40" maxlength="99" value="<?php echo $options['matomo_baseurl']; ?>" /><br/>
165 <div id="expl2" style="display:none;">
166 <p>The URL path to the matomo installation. E.g. /matomo/ or /stats/. Don't forget the trailing slash!</p>
167 </div>
168 </td>
169 </tr>
170 <tr>
171 <th scope="row" valign="top">
172 <label for="admintracking">Track the admin user too</label><br/>
173 <small>(default is not to)</small>
174 </th>
175 <td>
176 <input type="checkbox" id="admintracking" name="admintracking" <?php if ($options['admintracking']) echo ' checked="checked" '; ?>/>
177 </td>
178 </tr>
179 </table>
180 <p style="border:0;" class="submit"><input type="submit" name="submit" value="Update Settings &raquo;" /></p>
181 </form>
182 <p>All options set? Then <a href="http://<?php if ($options['matomo_host']) { echo $options['matomo_host']; }else{ echo $_SERVER['HTTP_HOST'];}; echo $options['matomo_baseurl']; ?>" title="Matomo admin url" target="_blank">check out your stats!</a>
183 </div>
184 <?php
185 if (isset($options['siteid'])) {
186 if ($options['siteid'] == "") {
187 add_action('admin_footer', array('SMTC_Admin','warning'));
188 } else {
189 if (isset($_POST['submit'])) {
190 if ($_POST['siteid'] != $options['siteid'] ) {
191 add_action('admin_footer', array('SMTC_Admin','success'));
192 }
193 }
194 }
195 } else {
196 add_action('admin_footer', array('SMTC_Admin','warning'));
197 }
198
199 } // end config_page()
200
201 function success() {
202 echo "
203 <div id='analytics-warning' class='updated fade-ff0000'><p><strong>Congratulations! You have just activated Matomo Web Analytics.</p></div>
204 <style type='text/css'>
205 #adminmenu { margin-bottom: 7em; }
206 #analytics-warning { position: absolute; top: 7em; }
207 </style>";
208 }
209
210 function warning() {
211 echo "
212 <div id='analytics-warning' class='updated fade-ff0000'><p><strong>Matomo Web Analytics is not active.</strong> You must <a href='plugins.php?page=simple-matomo-tracking-code.php'>enter your Site ID</a> for it to work.</p></div>";
213 }
214 }
215 }
216
217
218 /**
219 * Code that actually inserts stuff into pages.
220 */
221 if ( ! class_exists( 'SMTC_Filter' ) ) {
222 class SMTC_Filter {
223
224 /*
225 * Insert the tracking code into the page
226 */
227 static function spool_analytics() {
228 ?><!-- Simple Matomo Tracking Code plugin active --><?php
229
230 $script_template = "<!-- Matomo -->
231 <script type=\"text/javascript\">
232 var _paq = window._paq = window._paq || [];
233 _paq.push(['trackPageView']);
234 {LINK_TRACKING}
235 (function() {
236 var u=\"{MATOMO_URL}\";
237 _paq.push(['setTrackerUrl', u+'matomo.php']);
238 _paq.push(['setSiteId', {IDSITE}]);
239 var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
240 g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
241 })();
242 </script>
243 <!-- End Matomo Code -->";
244
245 $options = get_option('MatomoAnalyticsPP');
246
247 if ($options["siteid"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) {
248
249 if ( $options['matomo_host'] ) {
250 $matomo_url = "//" . $options['matomo_host'];
251 $matomo_url = rtrim($matomo_url, '/') . '/';
252 $matomo_url = $matomo_url . ltrim($options['matomo_baseurl'], '/');
253 $matomo_url = rtrim($matomo_url, '/') . '/';
254 } else {
255 $matomo_url = $options['matomo_baseurl'];
256 $matomo_url = rtrim($matomo_url, '/') . '/';
257 }
258
259 if ( $options["dltracking"]) {
260 $link_tracking = "_paq.push(['enableLinkTracking']);";
261 }
262
263 $transitions = array(
264 "{MATOMO_URL}" => $matomo_url,
265 "{IDSITE}" => $options["siteid"],
266 "{LINK_TRACKING}" => $link_tracking);
267 echo strtr($script_template, $transitions);
268 }
269 }
270 }
271 }
272
273 // adds the menu item to the admin interface
274 add_action('admin_menu', array('SMTC_Admin','add_config_page'));
275
276 // adds the footer so the javascript is loaded
277 add_action('wp_footer', array('SMTC_Filter','spool_analytics'));
278
279 /**
280 * Register the "book" custom post type
281 */
282 function simple_matomo_tracking_code_setup_post_type() {
283 register_post_type( 'book', ['public' => true ] );
284 }
285 add_action( 'init', 'simple_matomo_tracking_code_setup_post_type' );
286
287
288 /**
289 * Activate the plugin.
290 */
291 function simple_matomo_tracking_code_activate() {
292 $admin = new SMTC_Admin();
293 $admin->init();
294 }
295
296 register_activation_hook( __FILE__, 'simple_matomo_tracking_code_activate' );
297 ?>
298