PluginProbe
Include Matomo Tracking, by Jonas Hellmann / 1.5
Include Matomo Tracking, by Jonas Hellmann v1.5
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2
include-matomo / include-matomo.php

include-matomo.php in Include Matomo Tracking, by Jonas Hellmann 1.5, at include-matomo.php

130 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Include Matomo Tracking, by Jonas Hellmann
4 Plugin URI: https://github.com/jonashellmann/include-matomo/
5 Description: This plugin includes Matomo into your Wordpress site
6 Version: 1.5
7 Author: Jonas Hellmann
8 Author URI: https://jonas-hellmann.de/en/
9 License: GPL3
10 */
11
12 if( get_option('matomo_url') !== '' && get_option('matomo_site_id') !== '0' ) {
13 add_action( 'wp_footer', 'include_matomo_script' );
14 }
15
16 function include_matomo_script() {
17 echo "<!-- Include Matomo -->\n";
18 echo "<script type='text/javascript'>\n";
19 echo " var _paq = _paq || [];\n";
20 echo " _paq.push(['trackPageView']);\n";
21 echo " _paq.push(['enableLinkTracking']);\n";
22 echo " (function() {\n";
23 echo " var u='//" . get_matomo_url() . "/';\n";
24 echo " _paq.push(['setTrackerUrl', u+'piwik.php']);\n";
25 echo " _paq.push(['setSiteId', '" . get_option('matomo_site_id') . "']);\n";
26 if (get_option('matomo_disable_cookies', 'n') == 'y') {
27 echo " _paq.push([‘disableCookies’]);\n";
28 }
29 echo " var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n";
30 echo " g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);\n";
31 echo " })();\n";
32 echo "</script>\n";
33 echo "<!-- End Matomo Code-->\n";
34 }
35
36 function get_matomo_url() {
37 $matomo_url = get_option('matomo_url');
38 if( substr_compare( $matomo_url, '/', -1, 1 ) == 0 ) {
39 $matomo_url = substr( $matomo_url, 0, -1 );
40 }
41 if( substr_compare( $matomo_url, 'https://', 0, 8 ) == 0 ) {
42 $matomo_url = substr( $matomo_url, 8 );
43 }
44 if( substr_compare( $matomo_url, 'http://', 0, 7 ) == 0 ) {
45 $matomo_url = substr( $matomo_url, 7 );
46 }
47 return $matomo_url;
48 }
49
50 // TODO: Only include if this should be used
51 add_filter( 'the_permalink_rss', 'add_matomo_campaign_to_rss' );
52
53 function add_matomo_campaign_to_rss($guid) {
54 global $post;
55 $get_vars = array(
56 'pk_campaign=' . urlencode( get_option('matomo_rss_campaign') ),
57 'pk_source=' . urlencode( get_option('matomo_rss_source') )
58 );
59 return $guid . '?' . implode( '&', $get_vars );
60 }
61
62
63 add_action('admin_menu', 'include_matomo_menu');
64
65 function include_matomo_menu() {
66 add_submenu_page('options-general.php', 'Include Matomo - Settings', 'Include Matomo', 'administrator', 'include-matomo-settings', 'include_matomo_settings_page');
67 }
68
69 add_action( 'admin_init', 'my_plugin_settings' );
70
71 function my_plugin_settings() {
72 register_setting( 'include-matomo-settings-group', 'matomo_url' );
73 register_setting( 'include-matomo-settings-group', 'matomo_site_id' );
74 register_setting( 'include-matomo-settings-group', 'matomo_rss_campaign' );
75 register_setting( 'include-matomo-settings-group', 'matomo_rss_source' );
76 register_setting( 'include-matomo-settings-group', 'matomo_disable_cookies' );
77 }
78
79 function include_matomo_settings_page() { ?>
80 <div class="wrap">
81 <h2>Matomo Settings</h2>
82
83 <form method="post" action="options.php">
84 <?php settings_fields( 'include-matomo-settings-group' ); ?>
85 <?php do_settings_sections( 'include-matomo-settings-group' ); ?>
86 <table class="form-table">
87 <tr valign="top">
88 <th scope="row">Matomo URL</th>
89 <td>
90 <input type="text" name="matomo_url" value="<?php echo esc_attr( get_option('matomo_url') ); ?>" />
91 </td>
92 </tr>
93
94 <tr valign="top">
95 <th scope="row">Matomo Page ID</th>
96 <td>
97 <input type="number" name="matomo_site_id" value="<?php echo esc_attr( get_option('matomo_site_id') ); ?>" />
98 </td>
99 </tr>
100
101 <tr valign="top">
102 <th scope="row">Matomo RSS Campaign</th>
103 <td>
104 <input type="text" name="matomo_rss_campaign" value="<?php echo esc_attr( get_option('matomo_rss_campaign') ); ?>" />
105 </td>
106 </tr>
107
108 <tr valign="top">
109 <th scope="row">Matomo RSS Source</th>
110 <td>
111 <input type="text" name="matomo_rss_source" value="<?php echo esc_attr( get_option('matomo_rss_source') ); ?>" />
112 </td>
113 </tr>
114
115 <tr valign="top">
116 <th scope="row">Matomo Disable Cookies? (y/n)</th>
117 <td>
118 <input type="text" name="matomo_disable_cookies" value="<?php echo esc_attr( get_option('matomo_disable_cookies', 'n') ); ?>" />
119 </td>
120 </tr>
121 </table>
122
123 <?php submit_button(); ?>
124
125 </form>
126 </div> <?php
127 }
128
129 ?>
130