您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

225 行
8.0 KiB

  1. <?php
  2. /**
  3. * Plugin name: sandbox adaptions
  4. * Plugin URI: https://www.netzgestaltung.at
  5. * Author: Thomas Fellinger
  6. * Author URI: https://www.netzgestaltung.at
  7. * Version: 0.1
  8. * Description: Custom website functions
  9. * License: GPL v2
  10. * Copyright 2020 Thomas Fellinger (email : office@netzgestaltung.at)
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or
  14. * (at your option) any later version.
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. define('SANDBOX_STATS_PATH', plugin_dir_path(__FILE__));
  24. define('SANDBOX_STATS_URL', plugin_dir_url(__FILE__));
  25. /**
  26. * Plugin setup hook
  27. * ================
  28. */
  29. add_action('plugins_loaded', 'sandbox_setup_stats');
  30. function sandbox_setup_stats() {
  31. // track pageviews and url params
  32. add_action('wp_footer', 'sandbox_matomo_tracker');
  33. // track download actions
  34. // add_action('ddownload_save_success_before', 'sandbox_action_tracker');
  35. // track form submits
  36. // cf7 hooks: https://github.com/netzgestaltung/contact-form-7-hooks/
  37. add_action('wpcf7_submit', 'sandbox_form_submit_tracker', 10, 2);
  38. }
  39. /**
  40. * Matomo Tracker implementing class MatomoTracker
  41. * =============================================
  42. * https://github.com/netzgestaltung/wordpress-snippets/blob/master/matomo-tracker.php
  43. * Tracks anonymous pageViews on every visit by HTTP Tracking API
  44. *
  45. * Usage:
  46. * Tracks campaigns with the scheme: https://www.domain.tld/?c=<pk_campaign>(-<pk_source>)(-<pk_medum>)(-<pk_keyword>)(-<pk_content>)
  47. *
  48. * Installation:
  49. * Download: https://github.com/matomo-org/matomo-php-tracker
  50. * save MatomoTracker.php in yourThemes <folderRoot>/includes/matomo-php-tracker/MatomoTracker.php
  51. * Integrate this file into yourThemes functions.php and rename "sandbox" to your themes name
  52. *
  53. * Configuration:
  54. * Specify $tracker_url, $matomo_site_id and $matomo_user_token
  55. *
  56. *
  57. * License: GNU General Public License v2.0
  58. */
  59. function sandbox_get_matomo_tracker(){
  60. include_once(SANDBOX_STATS_PATH . '/config.php');
  61. include_once(SANDBOX_STATS_PATH . '/MatomoTracker.php');
  62. MatomoTracker::$URL = SANDBOX_STATS_TRACKER_URL;
  63. $matomoTracker = new MatomoTracker(SANDBOX_STATS_SITE_ID);
  64. // Specify an API token with at least Write permission, so the Visitor IP address can be recorded
  65. // Learn more about token_auth: https://matomo.org/faq/general/faq_114/
  66. $matomoTracker->setTokenAuth(SANDBOX_STATS_AUTH_TOKEN);
  67. // You can manually set the visitor details (resolution, time, plugins, etc.)
  68. // See all other ->set* functions available in the MatomoTracker.php file
  69. // $matomoTracker->setResolution(1600, 1400);
  70. // only track anonymous data!
  71. $matomoTracker->disableCookieSupport();
  72. $matomoTracker->setIp('0.0.0.0');
  73. return $matomoTracker;
  74. }
  75. function sandbox_action_tracker($download_id){
  76. $matomoTracker = sandbox_get_matomo_tracker();
  77. // requires plugin "alpha-downloads"
  78. $download_url = get_post_meta($download_id, '_alpha_file_url', true);
  79. $matomoTracker->doTrackAction($download_url, 'download');
  80. }
  81. // track form submits
  82. // cf7 hooks: https://github.com/netzgestaltung/contact-form-7-hooks/
  83. function sandbox_form_submit_tracker($cf7, $result){
  84. $matomoTracker = sandbox_get_matomo_tracker();
  85. // a form has been viewed
  86. $matomoTracker->doTrackContentImpression('form-' . $cf7->name(), 'Status: ' . $result['status'] . ', Message: ' . $result['message']);
  87. if ( $result['status'] === 'mail_sent' ) {
  88. // event for goal tracking
  89. $matomoTracker->doTrackEvent('form', 'Form submit successful', 'form-' . $cf7->name(), true);
  90. } else {
  91. // collect errors as custom variable
  92. $matomoTracker->setCustomVariable(2, 'Form submit not successful', 'Form-ID: ' . $result['contact_form_id'] . ', Status: ' . $result['status'] . ', Message: ' . $result['message'], 'event');
  93. }
  94. // a form has been submitted
  95. $matomoTracker->doTrackContentInteraction('Form submit', 'form-' . $cf7->name(), 'Status: ' . $result['status'] . ', Message: ' . $result['message']);
  96. }
  97. function sandbox_matomo_tracker(){
  98. // exclude json calls
  99. if ( isset($_POST['json']) || isset($_GET['json']) ) {
  100. return;
  101. }
  102. // page url
  103. $site_url = '';
  104. $schema = 'https://';
  105. if ( empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] === 'off' ) {
  106. $schema = 'http://';
  107. }
  108. $uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
  109. $site_url .= $schema . $_SERVER['SERVER_NAME'] . $uri_parts[0];
  110. $is_referer = isset($_SERVER['HTTP_REFERER']) ? ( $site_url === $_SERVER['HTTP_REFERER'] ) : false;
  111. // exclude:
  112. // - site_url ist referer
  113. // - admin pages
  114. // - is outside main query
  115. // - is wp doing ajax
  116. // - bot user agents by regex
  117. if ( !$is_referer && !is_admin() && is_main_query() && !wp_doing_ajax() && !preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']) ) {
  118. $matomoTracker = sandbox_get_matomo_tracker();
  119. // page title
  120. $page_title = wp_get_document_title();
  121. if ( is_404() ) {
  122. $page_title = '404 not found, Look for 404 Data at Custom Variables';
  123. $matomoTracker->setCustomVariable(1, '404', $site_url, 'page');
  124. $site_url = $schema . $_SERVER['SERVER_NAME'] . '/404';
  125. }
  126. // Campaign Tracking
  127. if ( isset($_GET['c']) ) {
  128. // Matomo related campain query params
  129. $campaign_parts = array(
  130. 'pk_campaign',
  131. 'pk_source',
  132. 'pk_medium',
  133. 'pk_kwd',
  134. 'pk_content',
  135. );
  136. // get array from query string, delimited by "-"
  137. $campaign_params = explode('-', $_GET['c'], 5);
  138. // how much params are given?
  139. $campaign_param_length = count($campaign_params);
  140. // reduce the parts set to the number of given params
  141. $campaign_parts = array_slice($campaign_parts, 0, $campaign_param_length, true);
  142. // put params and parts together
  143. $campaign = array_combine($campaign_parts, $campaign_params);
  144. // create new querystring
  145. if ( count($campaign) > 0 ) { // if we have campain params
  146. $site_url .= '?' . http_build_query($campaign);
  147. }
  148. }
  149. // Event tracking
  150. // ?mtb={category}-{action}-{name}
  151. // we map short URL IDs to readable Values
  152. // asking for spezific values is very secure but hard to maintain
  153. if ( isset($_GET['mtb']) ) {
  154. $mtb_params = explode('-', $_GET['mtb'], 3);
  155. $mtb_value = false;
  156. if ( is_404() ) {
  157. $mtb_value = true;
  158. }
  159. if ( $mtb_params[0] === 'h' ) {
  160. // Homepage mapping
  161. $mtb_params[0] = 'Home';
  162. } else if ( $mtb_params[0] === 'sb' ) {
  163. // Sidebar Banner mapping
  164. $mtb_params[0] = 'Sidebar Banner';
  165. } else if ( $mtb_params[0] === 'hb' ) {
  166. // Header Button mapping
  167. $mtb_params[0] = 'Header Button';
  168. }
  169. // Type mapping
  170. if ( $mtb_params[1] === 'b' ) {
  171. $mtb_params[1] = 'Button';
  172. } else if ( $mtb_params[1] === 'l' ) {
  173. $mtb_params[1] = 'Link';
  174. } else if ( $mtb_params[1] === 't' ) {
  175. $mtb_params[1] = 'Thumbnail';
  176. }
  177. // Content Identifier mapping
  178. if ( $mtb_params[2] === 'bb' ) {
  179. $mtb_params[2] = 'Banner button';
  180. } else if ( $mtb_params[2] === 'bt' ) {
  181. $mtb_params[2] = 'Banner thumbnail';
  182. } else if ( $mtb_params[2] === 'db' ) {
  183. $mtb_params[2] = 'Demo Button';
  184. }
  185. // track event
  186. $matomoTracker->doTrackEvent($mtb_params[0], $mtb_params[1], $mtb_params[2], $mtb_value);
  187. }
  188. // Set the url of visited page that we send to matomo
  189. $matomoTracker->setUrl($site_url);
  190. // Sends Tracker request via http
  191. $matomoTracker->doTrackPageView($page_title);
  192. // You can also track Goal conversions
  193. // $matomoTracker->doTrackGoal($idGoal = 1, $revenue = 42);
  194. }
  195. }
  196. ?>