Yoast_GA_JS

The frontend JS class.

Defined (1)

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

/frontend/class-ga-js.php  
  1. class Yoast_GA_JS extends Yoast_GA_Tracking { 
  2.  
  3. /** 
  4. * Function to output the GA Tracking code in the wp_head() 
  5. * @param bool $return_array 
  6. * @return null|array 
  7. */ 
  8. public function tracking( $return_array = false ) { 
  9. global $wp_query; 
  10.  
  11. if ( $this->do_tracking() && ! is_preview() ) { 
  12. $gaq_push = array(); 
  13.  
  14. // Running action for adding possible code 
  15. do_action( 'yst_tracking' ); 
  16.  
  17. if ( isset( $this->options['subdomain_tracking'] ) && $this->options['subdomain_tracking'] != '' ) { 
  18. $domain = esc_attr( $this->options['subdomain_tracking'] ); 
  19. else { 
  20. $domain = null; // Default domain value 
  21.  
  22. if ( ! isset( $this->options['allowanchor'] ) ) { 
  23. $this->options['allowanchor'] = false; 
  24.  
  25. $ua_code = $this->get_tracking_code(); 
  26. if ( is_null( $ua_code ) && $return_array == false ) { 
  27. return null; 
  28.  
  29. $gaq_push[] = "'_setAccount', '" . $ua_code . "'"; 
  30.  
  31. if ( ! is_null( $domain ) ) { 
  32. $gaq_push[] = "'_setDomainName', '" . $domain . "'"; 
  33.  
  34. if ( isset( $this->options['allowanchor'] ) && $this->options['allowanchor'] ) { 
  35. $gaq_push[] = "'_setAllowAnchor', true"; 
  36.  
  37. if ( $this->options['add_allow_linker'] ) { 
  38. $gaq_push[] = "'_setAllowLinker', true"; 
  39.  
  40. // @todo, check for AllowLinker in GA.js? Universal only? 
  41.  
  42. // SSL data 
  43. $gaq_push[] = "'_gat._forceSSL'"; 
  44.  
  45. if ( ! empty( $this->options['custom_code'] ) ) { 
  46. // Add custom code to the view 
  47. $gaq_push[] = array( 
  48. 'type' => 'custom_code',  
  49. 'value' => stripslashes( $this->options['custom_code'] ),  
  50. ); 
  51.  
  52. // Anonymous data 
  53. if ( $this->options['anonymize_ips'] == 1 ) { 
  54. $gaq_push[] = "'_gat._anonymizeIp'"; 
  55.  
  56. if ( isset( $this->options['allowhash'] ) && $this->options['allowhash'] ) { 
  57. $gaq_push[] = "'_gat._anonymizeIp', true"; 
  58.  
  59. if ( is_404() ) { 
  60. $gaq_push[] = "'_trackPageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer"; 
  61. else { 
  62. if ( $wp_query->is_search ) { 
  63. $pushstr = "'_trackPageview', '/?s="; 
  64. if ( $wp_query->found_posts == 0 ) { 
  65. $gaq_push[] = $pushstr . 'no-results:' . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=no-results'"; 
  66. else { 
  67. if ( $wp_query->found_posts == 1 ) { 
  68. $gaq_push[] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=1-result'"; 
  69. else { 
  70. if ( $wp_query->found_posts > 1 && $wp_query->found_posts < 6 ) { 
  71. $gaq_push[] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=2-5-results'"; 
  72. else { 
  73. $gaq_push[] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=plus-5-results'"; 
  74. else { 
  75. $gaq_push[] = "'_trackPageview'"; 
  76.  
  77. /** 
  78. * Filter: 'yoast-ga-push-array-ga-js' - Allows filtering of the commands to push 
  79. * @api array $gaq_push 
  80. */ 
  81. if ( true == $return_array ) { 
  82. return $gaq_push; 
  83.  
  84. $gaq_push = apply_filters( 'yoast-ga-push-array-ga-js', $gaq_push ); 
  85.  
  86. $ga_settings = $this->options; // Assign the settings to the javascript include view 
  87.  
  88.  
  89. // Include the tracking view 
  90. if ( ! $this->debug_mode() ) { 
  91. require( 'views/tracking-ga-js.php' ); 
  92. else { 
  93. $this->disabled_usergroup(); 
  94.  
  95. /** 
  96. * Get tracking prefix 
  97. * @return string 
  98. */ 
  99. public function get_tracking_prefix() { 
  100. return ( empty( $this->options['trackprefix'] ) ) ? '/yoast-ga/' : $this->options['trackprefix']; 
  101.  
  102. /** 
  103. * Ouput tracking link 
  104. * @param string $label 
  105. * @param array $matches 
  106. * @return mixed 
  107. */ 
  108. protected function output_parse_link( $label, $matches ) { 
  109. $link = $this->get_target( $label, $matches ); 
  110.  
  111. // bail early for links that we can't handle 
  112. if ( is_null( $link['type'] ) || 'internal' === $link['type'] ) { 
  113. return $matches[0]; 
  114.  
  115. $onclick = null; 
  116. $full_url = $this->make_full_url( $link ); 
  117.  
  118. switch ( $link['type'] ) { 
  119. case 'download': 
  120. if ( $this->options['track_download_as'] == 'pageview' ) { 
  121. $onclick = "_gaq.push(['_trackPageview', 'download/" . esc_js( $full_url ) . "']);"; 
  122. else { 
  123. $onclick = "_gaq.push(['_trackEvent', 'download', '" . esc_js( $full_url ) . "']);"; 
  124.  
  125. break; 
  126. case 'email': 
  127. $onclick = "_gaq.push(['_trackEvent', 'mailto', '" . esc_js( $link['original_url'] ) . "']);"; 
  128.  
  129. break; 
  130. case 'internal-as-outbound': 
  131. $label = $this->sanitize_internal_label(); 
  132.  
  133. $onclick = "_gaq.push(['_trackEvent', '" . esc_js( $link['category'] ) . '-' . esc_js( $label ) . "', '" . esc_js( $full_url ) . "', '" . esc_js( strip_tags( $link['link_text'] ) ) . "']);"; 
  134.  
  135. break; 
  136. case 'outbound': 
  137. $onclick = "_gaq.push(['_trackEvent', '" . esc_js( $link['category'] ) . "', '" . esc_js( $full_url ) . "', '" . esc_js( strip_tags( $link['link_text'] ) ) . "']);"; 
  138.  
  139. break; 
  140.  
  141. $link['link_attributes'] = $this->output_add_onclick( $link['link_attributes'], $onclick ); 
  142.  
  143. return '<a href="' . $full_url . '" ' . $link['link_attributes'] . '>' . $link['link_text'] . '</a>'; 
  144.