Yoast_GA_Universal

This is the frontend class for the GA Universal code.

Defined (1)

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

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