wp-google-analytics-legacy.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Jetpack_Google_Analytics_Legacy hooks and enqueues support for ga.js
  4. * https://developers.google.com/analytics/devguides/collection/gajs/
  5. *
  6. * @author Aaron D. Campbell (original)
  7. * @author allendav
  8. */
  9. /**
  10. * Bail if accessed directly
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. class Jetpack_Google_Analytics_Legacy {
  16. public function __construct() {
  17. add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_anonymize_ip' ) );
  18. add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_track_purchases' ) );
  19. add_action( 'wp_footer', array( $this, 'insert_code' ) );
  20. add_action( 'wp_footer', array( $this, 'jetpack_wga_classic_track_add_to_cart' ) );
  21. }
  22. /**
  23. * Used to generate a tracking URL
  24. * Called exclusively by insert_code
  25. *
  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. }
  35. if ( 'data' === $k ) {
  36. $track[ $k ] = preg_replace( '/^https?:\/\/|^\/+/i', '', $track[ $k ] );
  37. }
  38. // This way we don't lose search data.
  39. if ( 'data' === $k && 'search' === $track['code'] ) {
  40. $track[ $k ] = rawurlencode( $track[ $k ] );
  41. } else {
  42. $track[ $k ] = preg_replace( '/[^a-z0-9\.\/\+\?=-]+/i', '_', $track[ $k ] );
  43. }
  44. $track[ $k ] = trim( $track[ $k ], '_' );
  45. }
  46. $char = ( strpos( $track['data'], '?' ) === false ) ? '?' : '&amp;';
  47. return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); // Input var okay.
  48. }
  49. /**
  50. * This injects the Google Analytics code into the footer of the page.
  51. * Called exclusively by wp_footer action
  52. */
  53. public function insert_code() {
  54. $tracking_id = Jetpack_Google_Analytics_Options::get_tracking_code();
  55. if ( empty( $tracking_id ) ) {
  56. echo "<!-- Your Google Analytics Plugin is missing the tracking ID -->\r\n";
  57. return;
  58. }
  59. // If we're in the admin_area, return without inserting code.
  60. if ( is_admin() ) {
  61. return;
  62. }
  63. $custom_vars = array(
  64. "_gaq.push(['_setAccount', '{$tracking_id}']);",
  65. );
  66. $track = array();
  67. if ( is_404() ) {
  68. // This is a 404 and we are supposed to track them.
  69. $custom_vars[] = "_gaq.push(['_trackEvent', '404', document.location.href, document.referrer]);";
  70. } elseif (
  71. is_search()
  72. && isset( $_REQUEST['s'] )
  73. ) {
  74. // Set track for searches, if it's a search, and we are supposed to.
  75. $track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // Input var okay.
  76. $track['code'] = 'search';
  77. }
  78. if ( ! empty( $track ) ) {
  79. $track['url'] = $this->_get_url( $track );
  80. // adjust the code that we output, account for both types of tracking.
  81. $track['url'] = esc_js( str_replace( '&', '&amp;', $track['url'] ) );
  82. $custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);";
  83. } else {
  84. $custom_vars[] = "_gaq.push(['_trackPageview']);";
  85. }
  86. /**
  87. * Allow for additional elements to be added to the classic Google Analytics queue (_gaq) array
  88. *
  89. * @since 5.4.0
  90. *
  91. * @param array $custom_vars Array of classic Google Analytics queue elements
  92. */
  93. $custom_vars = apply_filters( 'jetpack_wga_classic_custom_vars', $custom_vars );
  94. // Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
  95. printf(
  96. "<!-- Jetpack Google Analytics -->
  97. <script type='text/javascript'>
  98. var _gaq = _gaq || [];
  99. %s
  100. (function() {
  101. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  102. ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  103. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  104. })();
  105. </script>\r\n",
  106. implode( "\r\n", $custom_vars )
  107. );
  108. }
  109. /**
  110. * Used to filter in the anonymize IP snippet to the custom vars array for classic analytics
  111. * Ref https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_gat._anonymizelp
  112. * @param array custom vars to be filtered
  113. * @return array possibly updated custom vars
  114. */
  115. public function jetpack_wga_classic_anonymize_ip( $custom_vars ) {
  116. if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) {
  117. array_push( $custom_vars, "_gaq.push(['_gat._anonymizeIp']);" );
  118. }
  119. return $custom_vars;
  120. }
  121. /**
  122. * Used to filter in the order details to the custom vars array for classic analytics
  123. * @param array custom vars to be filtered
  124. * @return array possibly updated custom vars
  125. */
  126. public function jetpack_wga_classic_track_purchases( $custom_vars ) {
  127. global $wp;
  128. if ( ! class_exists( 'WooCommerce' ) ) {
  129. return $custom_vars;
  130. }
  131. if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
  132. return;
  133. }
  134. // Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
  135. if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) {
  136. return $custom_vars;
  137. }
  138. $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
  139. if ( $minimum_woocommerce_active && is_order_received_page() ) {
  140. $order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
  141. if ( 0 < $order_id && 1 != get_post_meta( $order_id, '_ga_tracked', true ) ) {
  142. $order = new WC_Order( $order_id );
  143. // [ '_add_Trans', '123', 'Site Title', '21.00', '1.00', '5.00', 'Snohomish', 'WA', 'USA' ]
  144. array_push(
  145. $custom_vars,
  146. sprintf(
  147. '_gaq.push( %s );', json_encode(
  148. array(
  149. '_addTrans',
  150. (string) $order->get_order_number(),
  151. get_bloginfo( 'name' ),
  152. (string) $order->get_total(),
  153. (string) $order->get_total_tax(),
  154. (string) $order->get_total_shipping(),
  155. (string) $order->get_billing_city(),
  156. (string) $order->get_billing_state(),
  157. (string) $order->get_billing_country()
  158. )
  159. )
  160. )
  161. );
  162. // Order items
  163. if ( $order->get_items() ) {
  164. foreach ( $order->get_items() as $item ) {
  165. $product = $order->get_product_from_item( $item );
  166. $product_sku_or_id = $product->get_sku() ? $product->get_sku() : $product->get_id();
  167. array_push(
  168. $custom_vars,
  169. sprintf(
  170. '_gaq.push( %s );', json_encode(
  171. array(
  172. '_addItem',
  173. (string) $order->get_order_number(),
  174. (string) $product_sku_or_id,
  175. $item['name'],
  176. Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
  177. (string) $order->get_item_total( $item ),
  178. (string) $item['qty']
  179. )
  180. )
  181. )
  182. );
  183. }
  184. } // get_items
  185. // Mark the order as tracked
  186. update_post_meta( $order_id, '_ga_tracked', 1 );
  187. array_push( $custom_vars, "_gaq.push(['_trackTrans']);" );
  188. } // order not yet tracked
  189. } // is order received page
  190. return $custom_vars;
  191. }
  192. /**
  193. * Used to add footer javascript to track user clicking on add-to-cart buttons
  194. * on single views (.single_add_to_cart_button) and list views (.add_to_cart_button)
  195. */
  196. public function jetpack_wga_classic_track_add_to_cart() {
  197. if ( ! class_exists( 'WooCommerce' ) ) {
  198. return;
  199. }
  200. if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
  201. return;
  202. }
  203. if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
  204. return;
  205. }
  206. if ( is_product() ) { // product page
  207. global $product;
  208. $product_sku_or_id = $product->get_sku() ? $product->get_sku() : "#" + $product->get_id();
  209. wc_enqueue_js(
  210. "jQuery( function( $ ) {
  211. $( '.single_add_to_cart_button' ).click( function() {
  212. _gaq.push(['_trackEvent', 'Products', 'Add to Cart', '#" . esc_js( $product_sku_or_id ) . "']);
  213. } );
  214. } );"
  215. );
  216. } else if ( is_woocommerce() ) { // any other page that uses templates (like product lists, archives, etc)
  217. wc_enqueue_js(
  218. "jQuery( function( $ ) {
  219. $( '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' ).click( function() {
  220. var label = $( this ).data( 'product_sku' ) ? $( this ).data( 'product_sku' ) : '#' + $( this ).data( 'product_id' );
  221. _gaq.push(['_trackEvent', 'Products', 'Add to Cart', label]);
  222. } );
  223. } );"
  224. );
  225. }
  226. }
  227. }