MonsterInsights_Tracking_Analytics

The Google Analytics for WordPress by MonsterInsights MonsterInsights Tracking Analytics class.

Defined (1)

The class is defined in the following location(s).

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