PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/frontend/class-universal.php

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