/includes/frontend/tracking/class-tracking-analytics.php

  1. <?php 
  2. /** 
  3. * Tracking analytics.js class. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @author Chris Christoff 
  9. */ 
  10.  
  11. // Exit if accessed directly 
  12. if ( ! defined( 'ABSPATH' ) ) { 
  13. exit; 
  14.  
  15. class MonsterInsights_Tracking_Analytics extends MonsterInsights_Tracking_Abstract { 
  16.  
  17. /** 
  18. * Holds the base class object. 
  19. * 
  20. * @since 6.0.0 
  21. * @access public 
  22. * 
  23. * @var object $base Base class object. 
  24. */ 
  25. public $base; 
  26.  
  27. /** 
  28. * Holds the name of the tracking type. 
  29. * 
  30. * @since 6.0.0 
  31. * @access public 
  32. * 
  33. * @var string $name Name of the tracking type. 
  34. */ 
  35. public $name = 'analytics'; 
  36.  
  37. /** 
  38. * Version of the tracking class. 
  39. * 
  40. * @since 6.0.0 
  41. * @access public 
  42. * 
  43. * @var string $version Version of the tracking class. 
  44. */ 
  45. public $version = '1.0.0'; 
  46.  
  47. /** 
  48. * Primary class constructor. 
  49. * 
  50. * @since 6.0.0 
  51. * @access public 
  52. */ 
  53. public function __construct() { 
  54. $this->base = MonsterInsights(); 
  55.  
  56. /** 
  57. * Get frontend tracking options. 
  58. * 
  59. * This function is used to return an array of parameters 
  60. * for the frontend_output() function to output. These are  
  61. * generally dimensions and turned on GA features. 
  62. * 
  63. * @since 6.0.0 
  64. * @access public 
  65. * 
  66. * @return array Array of the options to use. 
  67. */ 
  68. public function frontend_tracking_options( ) { 
  69. global $wp_query; 
  70. $options = array(); 
  71.  
  72. if ( monsterinsights_get_ua_to_output() ) { 
  73. $ua_code = monsterinsights_get_ua_to_output(); 
  74. } else { 
  75. return $options; 
  76.  
  77. $domain = 'auto'; // Default domain value 
  78. if ( monsterinsights_get_option( 'subdomain_tracking', false ) ) { 
  79. $domain = esc_attr( monsterinsights_get_option( 'subdomain_tracking', '' ) ); 
  80.  
  81. $allow_linker = monsterinsights_get_option( 'add_allow_linker', false ); 
  82. $allow_anchor = monsterinsights_get_option( 'allow_anchor', false ); 
  83.  
  84.  
  85. $create = array(); 
  86. if ( $allow_anchor ) { 
  87. $create['allowAnchor'] = true; 
  88.  
  89. if ( $allow_linker ) { 
  90. $create['allowLinker'] = true; 
  91.  
  92. $create = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_create', $create ); 
  93.  
  94. if ( $create && ! empty( $create ) && is_array( $create ) ) { 
  95. $create = json_encode( $create ); 
  96. $create = str_replace( '"', "'", $create ); 
  97. $options['create'] = "'create', '" . esc_js( $ua_code ). "', '" . esc_js( $domain ) . "', " . $create; 
  98. } else { 
  99. $options['create'] = "'create', '" . esc_js( $ua_code ) . "', '" . esc_js( $domain ) . "'"; 
  100.  
  101. $options['forceSSL'] = "'set', 'forceSSL', true"; 
  102.  
  103. if ( monsterinsights_get_option( 'custom_code', false ) ) { 
  104. // Add custom code to the view 
  105. $options['custom_code'] = array( 
  106. 'type' => 'custom_code',  
  107. 'value' => stripslashes( monsterinsights_get_option( 'custom_code', '' ) ),  
  108. ); 
  109.  
  110. // Anonymous data 
  111. if ( monsterinsights_get_option( 'anonymize_ips', false ) ) { 
  112. $options['anonymize_ips'] = "'set', 'anonymizeIp', true"; 
  113.  
  114. $options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_before_scripts', $options ); 
  115.  
  116. // add demographics 
  117. if ( monsterinsights_get_option( 'demographics', false ) ) { 
  118. $options['demographics'] = "'require', 'displayfeatures'"; 
  119.  
  120. // Check for Enhanced link attribution 
  121. if ( monsterinsights_get_option( 'enhanced_link_attribution', false ) ) { 
  122. $options['enhanced_link_attribution'] = "'require', 'linkid', 'linkid.js'"; 
  123.  
  124. $options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_before_pageview', $options ); 
  125. $options = apply_filters( 'monsterinsights_frontend_tracking_options_before_pageview', $options, $this->name, $this->version ); 
  126.  
  127. if ( is_404() ) { 
  128. if ( monsterinsights_get_option( 'hash_tracking', false ) ) { 
  129. $options['send'] = "'send', 'pageview', '/404.html?page=' + document.location.pathname + document.location.search + location.hash + '&from=' + document.referrer"; 
  130. } else { 
  131. $options['send'] = "'send', 'pageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer"; 
  132. } else if ( $wp_query->is_search ) { 
  133. $pushstr = "'send', 'pageview', '/?s="; 
  134. if ( (int) $wp_query->found_posts === 0 ) { 
  135. $options['send'] = $pushstr . 'no-results:' . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=no-results'"; 
  136. } else if ( (int) $wp_query->found_posts === 1 ) { 
  137. $options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=1-result'"; 
  138. } else if ( $wp_query->found_posts > 1 && $wp_query->found_posts < 6 ) { 
  139. $options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=2-5-results'"; 
  140. } else { 
  141. $options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=plus-5-results'"; 
  142. } else if ( monsterinsights_get_option( 'hash_tracking', false ) ) { 
  143. $options['send'] = "'send', 'pageview', location.pathname + location.search + location.hash"; 
  144. } else { 
  145. $options['send'] = "'send', 'pageview'"; 
  146.  
  147. $options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_end', $options ); 
  148. return $options; 
  149.  
  150. /** 
  151. * Get frontend output. 
  152. * 
  153. * This function is used to return the Javascript 
  154. * to output in the head of the page for the given 
  155. * tracking method. 
  156. * 
  157. * @since 6.0.0 
  158. * @access public 
  159. * 
  160. * @return string Javascript to output. 
  161. */ 
  162. public function frontend_output( ) { 
  163. $options = $this->frontend_tracking_options(); 
  164. $is_debug_mode = monsterinsights_is_debug_mode(); 
  165. $src = apply_filters( 'monsterinsights_frontend_output_analytics_src', '//www.google-analytics.com/analytics.js' ); 
  166. if ( current_user_can( 'manage_options' ) && $is_debug_mode ) { 
  167. $src = apply_filters( 'monsterinsights_frontend_output_analytics_src', '//www.google-analytics.com/analytics_debug.js' ); 
  168. $compat = monsterinsights_get_option( 'gatracker_compatibility_mode', false ); 
  169. $compat = $compat ? 'window.ga = __gaTracker;' : ''; 
  170. ob_start(); 
  171. ?> 
  172. <!-- This site uses the Google Analytics by MonsterInsights plugin v<?php echo MONSTERINSIGHTS_VERSION; ?> - Using Analytics tracking - https://www.monsterinsights.com/ --> 
  173. <?php if ( monsterinsights_get_ua() ) { ?> 
  174. <script type="text/javascript" data-cfasync="false"> 
  175. // Function to detect opted out users 
  176. function __gaTrackerIsOptedOut() { 
  177. return document.cookie.indexOf(disableStr + '=true') > -1; 
  178.  
  179. // Disable tracking if the opt-out cookie exists. 
  180. var disableStr = 'ga-disable-<?php echo monsterinsights_get_ua(); ?>'; 
  181. if ( __gaTrackerIsOptedOut() ) { 
  182. window[disableStr] = true; 
  183.  
  184. // Opt-out function 
  185. function __gaTrackerOptout() { 
  186. document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/'; 
  187. window[disableStr] = true; 
  188.  
  189. (function(i, s, o, g, r, a, m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function() { 
  190. (i[r].q=i[r].q||[]).push(arguments)}, i[r].l=1*new Date();a=s.createElement(o),  
  191. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a, m) 
  192. })(window, document, 'script', '<?php echo $src; ?>', '__gaTracker'); 
  193.  
  194. <?php 
  195. if ( current_user_can( 'manage_options' ) && $is_debug_mode ) { 
  196. echo 'window.ga_debug = {trace: true};'; 
  197. echo $compat; 
  198. if ( count( $options ) >= 1 ) { 
  199. foreach ( $options as $item ) { 
  200. if ( ! is_array( $item ) ) { 
  201. echo ' __gaTracker(' . $item . ");\n"; 
  202. } else if ( ! empty ( $item['value'] ) ) { 
  203. echo ' ' . $item['value'] . "\n"; 
  204. ?> 
  205. </script> 
  206. <?php } else { ?> 
  207. <!-- No UA code set --> 
  208. <?php } ?> 
  209. <!-- / Google Analytics by MonsterInsights --> 
  210. <?php 
  211. $output = ob_get_contents(); 
  212. ob_end_clean(); 
  213. return $output; 
.