MonsterInsights_Tracking_GA

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

Defined (1)

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

/includes/frontend/tracking/class-tracking-ga.php  
  1. class MonsterInsights_Tracking_GA 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 = 'ga'; 
  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. $options['_setAccount'] = "'_setAccount', '" . monsterinsights_get_ua_to_output() . "'"; 
  50. } else { 
  51. return $options; 
  52.  
  53. if ( monsterinsights_get_option( 'subdomain_tracking', false ) ) { 
  54. $options['_setDomainName'] = "'_setDomainName', '" . esc_js( monsterinsights_get_option( 'subdomain_tracking', '' ) ) . "'"; 
  55.  
  56. if ( monsterinsights_get_option( 'allow_anchor', false ) ) { 
  57. $options['_setAllowAnchor'] = "'_setAllowAnchor', true"; 
  58.  
  59. if ( monsterinsights_get_option( 'add_allow_linker', false ) ) { 
  60. $options['_setAllowLinker'] = "'_setAllowLinker', true"; 
  61.  
  62. // SSL data 
  63. $options['_forceSSL'] = "'_gat._forceSSL'"; 
  64.  
  65. if ( monsterinsights_get_option( 'custom_code', false ) ) { 
  66. // Add custom code to the view 
  67. $options['custom_code'] = array( 
  68. 'type' => 'custom_code',  
  69. 'value' => esc_js( stripslashes( monsterinsights_get_option( 'custom_code', '' ) ) ),  
  70. ); 
  71.  
  72. // Anonymous data 
  73. if ( monsterinsights_get_option( 'anonymize_ips', false ) && ! monsterinsights_get_option( 'allowhash', false ) ) { 
  74. $options['anonymize_ips'] = "'_gat._anonymizeIp'"; 
  75.  
  76. if ( monsterinsights_get_option( 'allowhash', false ) ) { 
  77. $options['allowhash'] = "'_gat._anonymizeIp', true"; 
  78.  
  79. $options = apply_filters( 'monsterinsights_frontend_tracking_options_ga_before_pageview', $options ); 
  80. $options = apply_filters( 'monsterinsights_frontend_tracking_options_before_pageview', $options, $this->name, $this->version ); 
  81.  
  82. if ( is_404() ) { 
  83. $options['_trackPageview'] = "'_trackPageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer"; 
  84. } else if ( $wp_query->is_search ) { 
  85. $pushstr = "'_trackPageview', '/?s="; 
  86. if ( (int) $wp_query->found_posts === 0 ) { 
  87. $options['_trackPageview'] = $pushstr . 'no-results:' . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=no-results'"; 
  88. } else if ( (int) $wp_query->found_posts === 1 ) { 
  89. $options['_trackPageview'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=1-result'"; 
  90. } else if ( $wp_query->found_posts > 1 && $wp_query->found_posts < 6 ) { 
  91. $options['_trackPageview'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=2-5-results'"; 
  92. } else { 
  93. $options['_trackPageview'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=plus-5-results'"; 
  94. } else { 
  95. $options['_trackPageview'] = "'_trackPageview'"; 
  96.  
  97. $options = apply_filters( 'monsterinsights_frontend_tracking_options_ga_end', $options ); 
  98. return $options; 
  99.  
  100. /** 
  101. * Get frontend output. 
  102. * This function is used to return the Javascript 
  103. * to output in the head of the page for the given 
  104. * tracking method. 
  105. * @since 6.0.0 
  106. * @access public 
  107. * @return string Javascript to output. 
  108. */ 
  109. public function frontend_output( ) { 
  110. $options = $this->frontend_tracking_options(); 
  111. $src = apply_filters( 'monsterinsights_frontend_output_ga_src', "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n" ); 
  112. ob_start(); 
  113. ?> 
  114. <!-- This site uses the Google Analytics by MonsterInsights plugin v<?php echo MONSTERINSIGHTS_VERSION; ?> - Using ga.js tracking - https://www.monsterinsights.com/ --> 
  115. <?php if ( monsterinsights_get_ua() ) { ?> 
  116. <script type="text/javascript" data-cfasync="false"> 
  117.  
  118. var _gaq = _gaq || []; 
  119. <?php 
  120. if ( count( $options ) >= 1 ) { 
  121. foreach ( $options as $item ) { 
  122. if ( ! is_array( $item ) ) { 
  123. echo ' _gaq.push([' . $item . "]);\n"; 
  124. } else if ( isset( $item['value'] ) ) { 
  125. echo ' '.$item['value'] . "\n"; 
  126. ?> 
  127.  
  128. (function () { 
  129. var ga = document.createElement('script'); 
  130. ga.type = 'text/javascript'; 
  131. ga.async = true; 
  132. ga.src = <?php echo $src;?> 
  133. var s = document.getElementsByTagName('script')[0]; 
  134. s.parentNode.insertBefore(ga, s); 
  135. })(); 
  136. </script> 
  137. <?php } else { ?> 
  138. <!-- No UA code set --> 
  139. <?php } ?> 
  140. <!-- / Google Analytics by MonsterInsights --> 
  141. <?php 
  142. $output = ob_get_contents(); 
  143. ob_end_clean(); 
  144. return $output;