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

  1. <?php 
  2. /** 
  3. * Tracking ga.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_GA 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 = 'ga'; 
  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. $options['_setAccount'] = "'_setAccount', '" . monsterinsights_get_ua_to_output() . "'"; 
  74. } else { 
  75. return $options; 
  76.  
  77. if ( monsterinsights_get_option( 'subdomain_tracking', false ) ) { 
  78. $options['_setDomainName'] = "'_setDomainName', '" . esc_js( monsterinsights_get_option( 'subdomain_tracking', '' ) ) . "'"; 
  79.  
  80. if ( monsterinsights_get_option( 'allow_anchor', false ) ) { 
  81. $options['_setAllowAnchor'] = "'_setAllowAnchor', true"; 
  82.  
  83. if ( monsterinsights_get_option( 'add_allow_linker', false ) ) { 
  84. $options['_setAllowLinker'] = "'_setAllowLinker', true"; 
  85.  
  86. // SSL data 
  87. $options['_forceSSL'] = "'_gat._forceSSL'"; 
  88.  
  89. if ( monsterinsights_get_option( 'custom_code', false ) ) { 
  90. // Add custom code to the view 
  91. $options['custom_code'] = array( 
  92. 'type' => 'custom_code',  
  93. 'value' => esc_js( stripslashes( monsterinsights_get_option( 'custom_code', '' ) ) ),  
  94. ); 
  95.  
  96. // Anonymous data 
  97. if ( monsterinsights_get_option( 'anonymize_ips', false ) && ! monsterinsights_get_option( 'allowhash', false ) ) { 
  98. $options['anonymize_ips'] = "'_gat._anonymizeIp'"; 
  99.  
  100. if ( monsterinsights_get_option( 'allowhash', false ) ) { 
  101. $options['allowhash'] = "'_gat._anonymizeIp', true"; 
  102.  
  103. $options = apply_filters( 'monsterinsights_frontend_tracking_options_ga_before_pageview', $options ); 
  104. $options = apply_filters( 'monsterinsights_frontend_tracking_options_before_pageview', $options, $this->name, $this->version ); 
  105.  
  106. if ( is_404() ) { 
  107. $options['_trackPageview'] = "'_trackPageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer"; 
  108. } else if ( $wp_query->is_search ) { 
  109. $pushstr = "'_trackPageview', '/?s="; 
  110. if ( (int) $wp_query->found_posts === 0 ) { 
  111. $options['_trackPageview'] = $pushstr . 'no-results:' . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=no-results'"; 
  112. } else if ( (int) $wp_query->found_posts === 1 ) { 
  113. $options['_trackPageview'] = $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['_trackPageview'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=2-5-results'"; 
  116. } else { 
  117. $options['_trackPageview'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=plus-5-results'"; 
  118. } else { 
  119. $options['_trackPageview'] = "'_trackPageview'"; 
  120.  
  121. $options = apply_filters( 'monsterinsights_frontend_tracking_options_ga_end', $options ); 
  122. return $options; 
  123.  
  124. /** 
  125. * Get frontend output. 
  126. * 
  127. * This function is used to return the Javascript 
  128. * to output in the head of the page for the given 
  129. * tracking method. 
  130. * 
  131. * @since 6.0.0 
  132. * @access public 
  133. * 
  134. * @return string Javascript to output. 
  135. */ 
  136. public function frontend_output( ) { 
  137. $options = $this->frontend_tracking_options(); 
  138. $src = apply_filters( 'monsterinsights_frontend_output_ga_src', "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n" ); 
  139. ob_start(); 
  140. ?> 
  141. <!-- This site uses the Google Analytics by MonsterInsights plugin v<?php echo MONSTERINSIGHTS_VERSION; ?> - Using ga.js tracking - https://www.monsterinsights.com/ --> 
  142. <?php if ( monsterinsights_get_ua() ) { ?> 
  143. <script type="text/javascript" data-cfasync="false"> 
  144.  
  145. var _gaq = _gaq || []; 
  146. <?php 
  147. if ( count( $options ) >= 1 ) { 
  148. foreach ( $options as $item ) { 
  149. if ( ! is_array( $item ) ) { 
  150. echo ' _gaq.push([' . $item . "]);\n"; 
  151. } else if ( isset( $item['value'] ) ) { 
  152. echo ' '.$item['value'] . "\n"; 
  153. ?> 
  154.  
  155. (function () { 
  156. var ga = document.createElement('script'); 
  157. ga.type = 'text/javascript'; 
  158. ga.async = true; 
  159. ga.src = <?php echo $src;?> 
  160. var s = document.getElementsByTagName('script')[0]; 
  161. s.parentNode.insertBefore(ga, s); 
  162. })(); 
  163. </script> 
  164. <?php } else { ?> 
  165. <!-- No UA code set --> 
  166. <?php } ?> 
  167. <!-- / Google Analytics by MonsterInsights --> 
  168. <?php 
  169. $output = ob_get_contents(); 
  170. ob_end_clean(); 
  171. return $output; 
.