Jetpack_Google_Analytics

Jetpack_Google_Analytics is the class that handles ALL of the plugin functionality.

Defined (1)

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

/modules/google-analytics/wp-google-analytics.php  
  1. class Jetpack_Google_Analytics { 
  2.  
  3. /** 
  4. * @var Jetpack_Google_Analytics - Static property to hold our singleton instance 
  5. */ 
  6. static $instance = false; 
  7.  
  8. /** 
  9. * This is our constructor, which is private to force the use of get_instance() 
  10. * @return void 
  11. */ 
  12. private function __construct() { 
  13. add_action( 'wp_footer', array( $this, 'insert_code' ) ); 
  14.  
  15. /** 
  16. * Function to instantiate our class and make it a singleton 
  17. */ 
  18. public static function get_instance() { 
  19. if ( ! self::$instance ) { 
  20. self::$instance = new self; 
  21.  
  22. return self::$instance; 
  23.  
  24. /** 
  25. * Used to generate a tracking URL 
  26. * @param array $track - Must have ['data'] and ['code']. 
  27. * @return string - Tracking URL 
  28. */ 
  29. private function _get_url( $track ) { 
  30. $site_url = ( is_ssl() ? 'https://':'http://' ) . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); // Input var okay. 
  31. foreach ( $track as $k => $value ) { 
  32. if ( strpos( strtolower( $value ), strtolower( $site_url ) ) === 0 ) { 
  33. $track[ $k ] = substr( $track[ $k ], strlen( $site_url ) ); 
  34. if ( 'data' === $k ) { 
  35. $track[ $k ] = preg_replace( '/^https?:\/\/|^\/+/i', '', $track[ $k ] ); 
  36.  
  37. // This way we don't lose search data. 
  38. if ( 'data' === $k && 'search' === $track['code'] ) { 
  39. $track[ $k ] = rawurlencode( $track[ $k ] ); 
  40. } else { 
  41. $track[ $k ] = preg_replace( '/[^a-z0-9\.\/\+\?=-]+/i', '_', $track[ $k ] ); 
  42.  
  43. $track[ $k ] = trim( $track[ $k ], '_' ); 
  44. $char = ( strpos( $track['data'], '?' ) === false ) ? '?' : '&'; 
  45. return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); // Input var okay. 
  46.  
  47. /** 
  48. * Maybe output or return, depending on the context 
  49. */ 
  50. private function _output_or_return( $val, $maybe ) { 
  51. if ( $maybe ) { 
  52. echo $val . "\r\n"; 
  53. } else { 
  54. return $val; 
  55.  
  56. /** 
  57. * This injects the Google Analytics code into the footer of the page. 
  58. * @param bool[optional] $output - defaults to true, false returns but does NOT echo the code. 
  59. */ 
  60. public function insert_code( $output = true ) { 
  61. // If $output is not a boolean false, set it to true (default). 
  62. $output = ( false !== $output); 
  63.  
  64. $tracking_id = $this->_get_tracking_code(); 
  65. if ( empty( $tracking_id ) ) { 
  66. return $this->_output_or_return( '<!-- Your Google Analytics Plugin is missing the tracking ID -->', $output ); 
  67.  
  68. // If we're in the admin_area, return without inserting code. 
  69. if ( is_admin() ) { 
  70. return $this->_output_or_return( '<!-- Your Google Analytics Plugin is set to ignore Admin area -->', $output ); 
  71.  
  72. $custom_vars = array( 
  73. "_gaq.push(['_setAccount', '{$tracking_id}']);",  
  74. ); 
  75.  
  76. $track = array(); 
  77. if ( is_404() ) { 
  78. // This is a 404 and we are supposed to track them. 
  79. $custom_vars[] = "_gaq.push( [ '_trackEvent', '404', document.location.href, document.referrer ] );"; 
  80. } elseif ( is_search() ) { 
  81. // Set track for searches, if it's a search, and we are supposed to. 
  82. $track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // Input var okay. 
  83. $track['code'] = 'search'; 
  84.  
  85. if ( ! empty( $track ) ) { 
  86. $track['url'] = $this->_get_url( $track ); 
  87. // adjust the code that we output, account for both types of tracking. 
  88. $track['url'] = esc_js( str_replace( '&', '&', $track['url'] ) ); 
  89. $custom_vars[] = "_gaq.push(['_trackPageview', '{$track['url']}']);"; 
  90. } else { 
  91. $custom_vars[] = "_gaq.push(['_trackPageview']);"; 
  92.  
  93. $async_code = "<!-- Jetpack Google Analytics --> 
  94. <script type='text/javascript'> 
  95. var _gaq = _gaq || []; 
  96. %custom_vars% 
  97.  
  98. (function() { 
  99. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
  100. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
  101. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
  102. })(); 
  103. </script>"; 
  104.  
  105. $custom_vars_string = implode( "\r\n", $custom_vars ); 
  106. $async_code = str_replace( '%custom_vars%', $custom_vars_string, $async_code ); 
  107.  
  108. return $this->_output_or_return( $async_code, $output ); 
  109.  
  110. /** 
  111. * Used to get the tracking code option 
  112. * @return tracking code option value. 
  113. */ 
  114. private function _get_tracking_code() { 
  115. $o = get_option( 'jetpack_wga' ); 
  116.  
  117. if ( isset( $o['code'] ) && preg_match( '#UA-[\d-]+#', $o['code'], $matches ) ) { 
  118. return $o['code']; 
  119.  
  120. return '';