/wp-content/plugins/google-analytics-for-wordpress/frontend/class-ga-js.php

https://gitlab.com/najomie/fit-hippie · PHP · 191 lines · 145 code · 28 blank · 18 comment · 32 complexity · 609a5f32385beb8fe92037b71fdba83a MD5 · raw file

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