Нет описания

wp-google-analytics-legacy.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_head', array( $this, 'insert_code' ), 999 );
  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_head 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. if ( Jetpack_AMP_Support::is_amp_request() ) {
  64. // For Reader mode — legacy.
  65. add_filter( 'amp_post_template_analytics', 'Jetpack_Google_Analytics::amp_analytics_entries', 1000 );
  66. // For Standard and Transitional modes.
  67. add_filter( 'amp_analytics_entries', 'Jetpack_Google_Analytics::amp_analytics_entries', 1000 );
  68. return;
  69. }
  70. if ( 'G-' === substr( $tracking_id, 0, 2 ) ) {
  71. $this->render_gtag_code( $tracking_id );
  72. } else {
  73. $this->render_ga_code( $tracking_id );
  74. }
  75. }
  76. /**
  77. * Renders legacy ga.js code.
  78. *
  79. * @param string $tracking_id Google Analytics measurement ID.
  80. */
  81. private function render_ga_code( $tracking_id ) {
  82. $custom_vars = array(
  83. "_gaq.push(['_setAccount', '{$tracking_id}']);",
  84. );
  85. $track = array();
  86. if ( is_404() ) {
  87. // This is a 404 and we are supposed to track them.
  88. $custom_vars[] = "_gaq.push(['_trackEvent', '404', document.location.href, document.referrer]);";
  89. } elseif (
  90. is_search()
  91. && isset( $_REQUEST['s'] )
  92. ) {
  93. // Set track for searches, if it's a search, and we are supposed to.
  94. $track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // Input var okay.
  95. $track['code'] = 'search';
  96. }
  97. if ( ! empty( $track ) ) {
  98. $track['url'] = $this->_get_url( $track );
  99. // adjust the code that we output, account for both types of tracking.
  100. $track['url'] = esc_js( str_replace( '&', '&amp;', $track['url'] ) );
  101. $custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);";
  102. } else {
  103. $custom_vars[] = "_gaq.push(['_trackPageview']);";
  104. }
  105. /**
  106. * Allow for additional elements to be added to the classic Google Analytics queue (_gaq) array
  107. *
  108. * @since 5.4.0
  109. *
  110. * @param array $custom_vars Array of classic Google Analytics queue elements
  111. */
  112. $custom_vars = apply_filters( 'jetpack_wga_classic_custom_vars', $custom_vars );
  113. // Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
  114. printf(
  115. "<!-- Jetpack Google Analytics -->
  116. <script type='text/javascript'>
  117. var _gaq = _gaq || [];
  118. %s
  119. (function() {
  120. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  121. ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  122. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  123. })();
  124. </script>
  125. <!-- End Jetpack Google Analytics -->\r\n",
  126. implode( "\r\n", $custom_vars )
  127. );
  128. }
  129. /**
  130. * Renders new gtag code.
  131. *
  132. * @param string $tracking_id Google Analytics measurement ID.
  133. */
  134. private function render_gtag_code( $tracking_id ) {
  135. /**
  136. * Allow for additional elements to be added to the Global Site Tags array.
  137. *
  138. * @since 9.2.0
  139. *
  140. * @param array $universal_commands Array of gtag function calls.
  141. */
  142. $universal_commands = apply_filters( 'jetpack_gtag_universal_commands', array() );
  143. $custom_vars = array();
  144. if ( is_404() ) {
  145. $custom_vars[] = array(
  146. 'event',
  147. 'exception',
  148. array(
  149. 'description' => '404',
  150. 'fatal' => false,
  151. ),
  152. );
  153. }
  154. // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
  155. ?>
  156. <!-- Jetpack Google Analytics -->
  157. <script async src='https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $tracking_id ); ?>'></script>
  158. <script>
  159. window.dataLayer = window.dataLayer || [];
  160. function gtag() { dataLayer.push( arguments ); }
  161. gtag( 'js', new Date() );
  162. gtag( 'config', <?php echo wp_json_encode( $tracking_id ); ?> );
  163. <?php
  164. foreach ( $universal_commands as $command ) {
  165. echo 'gtag( ' . implode( ', ', array_map( 'wp_json_encode', $command ) ) . " );\n";
  166. }
  167. foreach ( $custom_vars as $var ) {
  168. echo 'gtag( ' . implode( ', ', array_map( 'wp_json_encode', $var ) ) . " );\n";
  169. }
  170. ?>
  171. </script>
  172. <!-- End Jetpack Google Analytics -->
  173. <?php
  174. // phpcs:enable
  175. }
  176. /**
  177. * Used to filter in the anonymize IP snippet to the custom vars array for classic analytics
  178. * Ref https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_gat._anonymizelp
  179. *
  180. * @param array custom vars to be filtered
  181. * @return array possibly updated custom vars
  182. */
  183. public function jetpack_wga_classic_anonymize_ip( $custom_vars ) {
  184. if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) {
  185. array_push( $custom_vars, "_gaq.push(['_gat._anonymizeIp']);" );
  186. }
  187. return $custom_vars;
  188. }
  189. /**
  190. * Used to filter in the order details to the custom vars array for classic analytics
  191. *
  192. * @param array custom vars to be filtered
  193. * @return array possibly updated custom vars
  194. */
  195. public function jetpack_wga_classic_track_purchases( $custom_vars ) {
  196. global $wp;
  197. if ( ! class_exists( 'WooCommerce' ) ) {
  198. return $custom_vars;
  199. }
  200. if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
  201. return;
  202. }
  203. // Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
  204. if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) {
  205. return $custom_vars;
  206. }
  207. $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
  208. if ( $minimum_woocommerce_active && is_order_received_page() ) {
  209. $order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
  210. if ( 0 < $order_id && 1 != get_post_meta( $order_id, '_ga_tracked', true ) ) {
  211. $order = new WC_Order( $order_id );
  212. // [ '_add_Trans', '123', 'Site Title', '21.00', '1.00', '5.00', 'Snohomish', 'WA', 'USA' ]
  213. array_push(
  214. $custom_vars,
  215. sprintf(
  216. '_gaq.push( %s );',
  217. json_encode(
  218. array(
  219. '_addTrans',
  220. (string) $order->get_order_number(),
  221. get_bloginfo( 'name' ),
  222. (string) $order->get_total(),
  223. (string) $order->get_total_tax(),
  224. (string) $order->get_total_shipping(),
  225. (string) $order->get_billing_city(),
  226. (string) $order->get_billing_state(),
  227. (string) $order->get_billing_country(),
  228. )
  229. )
  230. )
  231. );
  232. // Order items
  233. if ( $order->get_items() ) {
  234. foreach ( $order->get_items() as $item ) {
  235. $product = $order->get_product_from_item( $item );
  236. $product_sku_or_id = $product->get_sku() ? $product->get_sku() : $product->get_id();
  237. array_push(
  238. $custom_vars,
  239. sprintf(
  240. '_gaq.push( %s );',
  241. json_encode(
  242. array(
  243. '_addItem',
  244. (string) $order->get_order_number(),
  245. (string) $product_sku_or_id,
  246. $item['name'],
  247. Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
  248. (string) $order->get_item_total( $item ),
  249. (string) $item['qty'],
  250. )
  251. )
  252. )
  253. );
  254. }
  255. } // get_items
  256. // Mark the order as tracked
  257. update_post_meta( $order_id, '_ga_tracked', 1 );
  258. array_push( $custom_vars, "_gaq.push(['_trackTrans']);" );
  259. } // order not yet tracked
  260. } // is order received page
  261. return $custom_vars;
  262. }
  263. /**
  264. * Used to add footer javascript to track user clicking on add-to-cart buttons
  265. * on single views (.single_add_to_cart_button) and list views (.add_to_cart_button)
  266. */
  267. public function jetpack_wga_classic_track_add_to_cart() {
  268. if ( ! class_exists( 'WooCommerce' ) ) {
  269. return;
  270. }
  271. if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
  272. return;
  273. }
  274. if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
  275. return;
  276. }
  277. if ( is_product() ) { // product page
  278. global $product;
  279. $product_sku_or_id = $product->get_sku() ? $product->get_sku() : '#' + $product->get_id();
  280. wc_enqueue_js(
  281. "$( '.single_add_to_cart_button' ).click( function() {
  282. _gaq.push(['_trackEvent', 'Products', 'Add to Cart', '#" . esc_js( $product_sku_or_id ) . "']);
  283. } );"
  284. );
  285. } elseif ( is_woocommerce() ) { // any other page that uses templates (like product lists, archives, etc)
  286. wc_enqueue_js(
  287. "$( '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' ).click( function() {
  288. var label = $( this ).data( 'product_sku' ) ? $( this ).data( 'product_sku' ) : '#' + $( this ).data( 'product_id' );
  289. _gaq.push(['_trackEvent', 'Products', 'Add to Cart', label]);
  290. } );"
  291. );
  292. }
  293. }
  294. }