summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/google-analytics/classes')
-rw-r--r--plugins/jetpack/modules/google-analytics/classes/class-jetpack-google-amp-analytics.php2
-rw-r--r--plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-legacy.php42
-rw-r--r--plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-options.php102
-rw-r--r--plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-universal.php159
-rw-r--r--plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-utils.php30
5 files changed, 228 insertions, 107 deletions
diff --git a/plugins/jetpack/modules/google-analytics/classes/class-jetpack-google-amp-analytics.php b/plugins/jetpack/modules/google-analytics/classes/class-jetpack-google-amp-analytics.php
index daa369b1..896f194b 100644
--- a/plugins/jetpack/modules/google-analytics/classes/class-jetpack-google-amp-analytics.php
+++ b/plugins/jetpack/modules/google-analytics/classes/class-jetpack-google-amp-analytics.php
@@ -118,7 +118,7 @@ class Jetpack_Google_AMP_Analytics {
* Send the stored events to GA.
*/
public function amp_send_ga_events() {
- if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
+ if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Simple comparison
return;
}
diff --git a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-legacy.php b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-legacy.php
index f78040b1..e86a3bc8 100644
--- a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-legacy.php
+++ b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-legacy.php
@@ -1,4 +1,4 @@
-<?php
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Jetpack_Google_Analytics_Legacy hooks and enqueues support for ga.js
@@ -15,7 +15,13 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
+/**
+ * Jetpack_Google_Analytics_Legacy hooks and enqueues support for ga.js
+ */
class Jetpack_Google_Analytics_Legacy {
+ /**
+ * Jetpack_Google_Analytics_Legacy constructor.
+ */
public function __construct() {
add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_anonymize_ip' ) );
add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_track_purchases' ) );
@@ -30,8 +36,8 @@ class Jetpack_Google_Analytics_Legacy {
* @param array $track - Must have ['data'] and ['code'].
* @return string - Tracking URL
*/
- private function _get_url( $track ) {
- $site_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); // Input var okay.
+ private function get_url( $track ) {
+ $site_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' ) );
foreach ( $track as $k => $value ) {
if ( strpos( strtolower( $value ), strtolower( $site_url ) ) === 0 ) {
$track[ $k ] = substr( $track[ $k ], strlen( $site_url ) );
@@ -50,7 +56,7 @@ class Jetpack_Google_Analytics_Legacy {
$track[ $k ] = trim( $track[ $k ], '_' );
}
$char = ( strpos( $track['data'], '?' ) === false ) ? '?' : '&amp;';
- return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); // Input var okay.
+ return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '' ) );
}
/**
@@ -100,15 +106,15 @@ class Jetpack_Google_Analytics_Legacy {
$custom_vars[] = "_gaq.push(['_trackEvent', '404', document.location.href, document.referrer]);";
} elseif (
is_search()
- && isset( $_REQUEST['s'] )
+ && isset( $_REQUEST['s'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Function renders client-side JS, no site actions.
) {
// Set track for searches, if it's a search, and we are supposed to.
- $track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // Input var okay.
+ $track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Function renders client-side JS, no site actions.
$track['code'] = 'search';
}
if ( ! empty( $track ) ) {
- $track['url'] = $this->_get_url( $track );
+ $track['url'] = $this->get_url( $track );
// adjust the code that we output, account for both types of tracking.
$track['url'] = esc_js( str_replace( '&', '&amp;', $track['url'] ) );
$custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);";
@@ -138,7 +144,7 @@ class Jetpack_Google_Analytics_Legacy {
})();
</script>
<!-- End Jetpack Google Analytics -->\r\n",
- implode( "\r\n", $custom_vars )
+ implode( "\r\n", $custom_vars ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Additional elements added to the classic Google Analytics script.
);
}
@@ -194,8 +200,8 @@ class Jetpack_Google_Analytics_Legacy {
* Used to filter in the anonymize IP snippet to the custom vars array for classic analytics
* Ref https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_gat._anonymizelp
*
- * @param array custom vars to be filtered
- * @return array possibly updated custom vars
+ * @param array $custom_vars Custom vars to be filtered.
+ * @return array Possibly updated custom vars.
*/
public function jetpack_wga_classic_anonymize_ip( $custom_vars ) {
if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) {
@@ -208,8 +214,8 @@ class Jetpack_Google_Analytics_Legacy {
/**
* Used to filter in the order details to the custom vars array for classic analytics
*
- * @param array custom vars to be filtered
- * @return array possibly updated custom vars
+ * @param array $custom_vars Custom vars to be filtered.
+ * @return array Possibly updated custom vars.
*/
public function jetpack_wga_classic_track_purchases( $custom_vars ) {
global $wp;
@@ -230,15 +236,17 @@ class Jetpack_Google_Analytics_Legacy {
$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
if ( $minimum_woocommerce_active && is_order_received_page() ) {
$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
- if ( 0 < $order_id && 1 != get_post_meta( $order_id, '_ga_tracked', true ) ) {
+ if ( 0 < $order_id && 1 !== (int) get_post_meta( $order_id, '_ga_tracked', true ) ) {
$order = new WC_Order( $order_id );
- // [ '_add_Trans', '123', 'Site Title', '21.00', '1.00', '5.00', 'Snohomish', 'WA', 'USA' ]
+ /**
+ * [ '_add_Trans', '123', 'Site Title', '21.00', '1.00', '5.00', 'Snohomish', 'WA', 'USA' ]
+ */
array_push(
$custom_vars,
sprintf(
'_gaq.push( %s );',
- json_encode(
+ wp_json_encode(
array(
'_addTrans',
(string) $order->get_order_number(),
@@ -264,7 +272,7 @@ class Jetpack_Google_Analytics_Legacy {
$custom_vars,
sprintf(
'_gaq.push( %s );',
- json_encode(
+ wp_json_encode(
array(
'_addItem',
(string) $order->get_order_number(),
@@ -308,7 +316,7 @@ class Jetpack_Google_Analytics_Legacy {
if ( is_product() ) { // product page
global $product;
- $product_sku_or_id = $product->get_sku() ? $product->get_sku() : '#' + $product->get_id();
+ $product_sku_or_id = $product->get_sku() ? $product->get_sku() : '#' . $product->get_id();
wc_enqueue_js(
"$( '.single_add_to_cart_button' ).click( function() {
_gaq.push(['_trackEvent', 'Products', 'Add to Cart', '#" . esc_js( $product_sku_or_id ) . "']);
diff --git a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-options.php b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-options.php
index b6e208b7..e84ab748 100644
--- a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-options.php
+++ b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-options.php
@@ -1,70 +1,130 @@
-<?php
-
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileNames
/**
-* Jetpack_Google_Analytics_Options provides a single interface to module options
-*
-* @author allendav
-*/
+ * Jetpack_Google_Analytics_Options provides a single interface to module options
+ *
+ * @author allendav
+ */
/**
-* Bail if accessed directly
-*/
+ * Bail if accessed directly
+ */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
+/**
+ * Jetpack_Google_Analytics_Options main class.
+ */
class Jetpack_Google_Analytics_Options {
+ /**
+ * Get the Google Analytics tracking ID.
+ *
+ * @param string $option_name Nested 'jetpack_wga' option value to retrieve.
+ * @param mixed $default Default value if $option is not set.
+ * @return mixed Option value or `$default`.
+ */
public static function get_option( $option_name, $default = false ) {
$o = get_option( 'jetpack_wga' );
return isset( $o[ $option_name ] ) ? $o[ $option_name ] : $default;
}
+ /**
+ * Get the analytics tracking code.
+ *
+ * @return string
+ */
public static function get_tracking_code() {
return self::get_option( 'code', '' );
}
+ /**
+ * Check if the tracking code is set.
+ *
+ * @return bool
+ */
public static function has_tracking_code() {
$code = self::get_tracking_code();
return ! empty( $code );
}
- // Options used by both legacy and universal analytics
+ /**
+ * Get the 'anonymize_ip' option used by both legacy and universal analytics.
+ *
+ * @return bool
+ */
public static function anonymize_ip_is_enabled() {
- return self::get_option( 'anonymize_ip' );
+ return (bool) self::get_option( 'anonymize_ip' );
}
- // eCommerce options used by both legacy and universal analytics
+ /**
+ * Get the 'ec_track_purchases' eCommerce option used by both legacy and universal analytics
+ *
+ * @return bool
+ */
public static function track_purchases_is_enabled() {
- return self::get_option( 'ec_track_purchases' );
+ return (bool) self::get_option( 'ec_track_purchases' );
}
+ /**
+ * Get the 'ec_track_add_to_cart' analytics option.
+ *
+ * @return bool
+ */
public static function track_add_to_cart_is_enabled() {
- return self::get_option( 'ec_track_add_to_cart' );
+ return (bool) self::get_option( 'ec_track_add_to_cart' );
}
- // Enhanced eCommerce options
+ /**
+ * Get the 'enh_ec_tracking' analytics option.
+ *
+ * @return bool
+ */
public static function enhanced_ecommerce_tracking_is_enabled() {
- return self::get_option( 'enh_ec_tracking' );
+ return (bool) self::get_option( 'enh_ec_tracking' );
}
+ /**
+ * Get the 'enh_ec_track_remove_from_cart' analytics option.
+ *
+ * @return bool
+ */
public static function track_remove_from_cart_is_enabled() {
- return self::get_option( 'enh_ec_track_remove_from_cart' );
+ return (bool) self::get_option( 'enh_ec_track_remove_from_cart' );
}
+ /**
+ * Get the 'enh_ec_track_prod_impression' analytics option.
+ *
+ * @return bool
+ */
public static function track_product_impressions_is_enabled() {
- return self::get_option( 'enh_ec_track_prod_impression' );
+ return (bool) self::get_option( 'enh_ec_track_prod_impression' );
}
+ /**
+ * Get the 'enh_ec_track_prod_click' analytics option.
+ *
+ * @return bool
+ */
public static function track_product_clicks_is_enabled() {
- return self::get_option( 'enh_ec_track_prod_click' );
+ return (bool) self::get_option( 'enh_ec_track_prod_click' );
}
+ /**
+ * Get the 'enh_ec_track_prod_detail_view' analytics option.
+ *
+ * @return bool
+ */
public static function track_product_detail_view_is_enabled() {
- return self::get_option( 'enh_ec_track_prod_detail_view' );
+ return (bool) self::get_option( 'enh_ec_track_prod_detail_view' );
}
+ /**
+ * Get the 'enh_ec_track_checkout_started' analytics option.
+ *
+ * @return bool
+ */
public static function track_checkout_started_is_enabled() {
- return self::get_option( 'enh_ec_track_checkout_started' );
+ return (bool) self::get_option( 'enh_ec_track_checkout_started' );
}
}
-
diff --git a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-universal.php b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-universal.php
index 268f72ef..ea80b904 100644
--- a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-universal.php
+++ b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-universal.php
@@ -1,21 +1,26 @@
-<?php
-
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
-* Jetpack_Google_Analytics_Universal hooks and and enqueues support for analytics.js
-* https://developers.google.com/analytics/devguides/collection/analyticsjs/
-* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce
-*
-* @author allendav
-*/
+ * Jetpack_Google_Analytics_Universal hooks and and enqueues support for analytics.js
+ * https://developers.google.com/analytics/devguides/collection/analyticsjs/
+ * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce
+ *
+ * @author allendav
+ */
/**
-* Bail if accessed directly
-*/
+ * Bail if accessed directly
+ */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
+/**
+ * Jetpack_Google_Analytics_Universal main class.
+ */
class Jetpack_Google_Analytics_Universal {
+ /**
+ * Jetpack_Google_Analytics_Universal constructor.
+ */
public function __construct() {
add_filter( 'jetpack_wga_universal_commands', array( $this, 'maybe_anonymize_ip' ) );
add_filter( 'jetpack_wga_universal_commands', array( $this, 'maybe_track_purchases' ) );
@@ -37,6 +42,9 @@ class Jetpack_Google_Analytics_Universal {
add_action( 'wp_footer', array( $this, 'send_pageview_in_footer' ), 24 );
}
+ /**
+ * Hook for the `wp_head` action to output the analytics code.
+ */
public function wp_head() {
$tracking_code = Jetpack_Google_Analytics_Options::get_tracking_code();
if ( empty( $tracking_code ) ) {
@@ -66,6 +74,7 @@ class Jetpack_Google_Analytics_Universal {
*/
$universal_commands = apply_filters( 'jetpack_wga_universal_commands', array() );
+ // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript -- Script is added to wp_head.
$async_code = "
<!-- Jetpack Google Analytics -->
<script>
@@ -76,15 +85,21 @@ class Jetpack_Google_Analytics_Universal {
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Jetpack Google Analytics -->
- ";
+ "; // phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedScript
$async_code = str_replace( '%tracking_id%', $tracking_code, $async_code );
$universal_commands_string = implode( "\r\n", $universal_commands );
- $async_code = str_replace( '%universal_commands%', $universal_commands_string, $async_code );
+ $async_code = str_replace( '%universal_commands%', $universal_commands_string, $async_code );
- echo "$async_code\r\n";
+ echo "$async_code\r\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
+ /**
+ * Check if the 'anonymize_ip' option should be added to the universal Google Analytics queue (ga) commands.
+ *
+ * @param array $command_array Array of commands.
+ * @return array `$command_array` with the additional command conditionally added.
+ */
public function maybe_anonymize_ip( $command_array ) {
if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) {
array_push( $command_array, "ga( 'set', 'anonymizeIp', true );" );
@@ -93,6 +108,14 @@ class Jetpack_Google_Analytics_Universal {
return $command_array;
}
+ /**
+ * Process purchase tracking options for the universal Google Analytics queue (ga) commands.
+ *
+ * May also update post meta to indicate the order has been tracked.
+ *
+ * @param array $command_array Array of commands.
+ * @return array `$command_array` with additional commands conditionally added.
+ */
public function maybe_track_purchases( $command_array ) {
global $wp;
@@ -114,47 +137,47 @@ class Jetpack_Google_Analytics_Universal {
}
$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
- if ( 0 == $order_id ) {
+ if ( 0 === (int) $order_id ) {
return $command_array;
}
// A 1 indicates we've already tracked this order - don't do it again
- if ( 1 == get_post_meta( $order_id, '_ga_tracked', true ) ) {
+ if ( 1 === (int) get_post_meta( $order_id, '_ga_tracked', true ) ) {
return $command_array;
}
- $order = new WC_Order( $order_id );
+ $order = new WC_Order( $order_id );
$order_currency = $order->get_currency();
- $command = "ga( 'set', '&cu', '" . esc_js( $order_currency ) . "' );";
+ $command = "ga( 'set', '&cu', '" . esc_js( $order_currency ) . "' );";
array_push( $command_array, $command );
// Order items
if ( $order->get_items() ) {
foreach ( $order->get_items() as $item ) {
- $product = $order->get_product_from_item( $item );
+ $product = $order->get_product_from_item( $item );
$product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
$item_details = array(
- 'id' => $product_sku_or_id,
- 'name' => $item['name'],
+ 'id' => $product_sku_or_id,
+ 'name' => $item['name'],
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
- 'price' => $order->get_item_total( $item ),
+ 'price' => $order->get_item_total( $item ),
'quantity' => $item['qty'],
);
- $command = "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );";
+ $command = "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . ' );';
array_push( $command_array, $command );
}
}
// Order summary
$summary = array(
- 'id' => $order->get_order_number(),
+ 'id' => $order->get_order_number(),
'affiliation' => get_bloginfo( 'name' ),
- 'revenue' => $order->get_total(),
- 'tax' => $order->get_total_tax(),
- 'shipping' => $order->get_total_shipping()
+ 'revenue' => $order->get_total(),
+ 'tax' => $order->get_total_tax(),
+ 'shipping' => $order->get_total_shipping(),
);
- $command = "ga( 'ec:setAction', 'purchase', " . wp_json_encode( $summary ) . " );";
+ $command = "ga( 'ec:setAction', 'purchase', " . wp_json_encode( $summary ) . ' );';
array_push( $command_array, $command );
update_post_meta( $order_id, '_ga_tracked', 1 );
@@ -162,6 +185,9 @@ class Jetpack_Google_Analytics_Universal {
return $command_array;
}
+ /**
+ * Enqueue add-to-cart click tracking script, if enabled.
+ */
public function add_to_cart() {
if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
return;
@@ -174,7 +200,7 @@ class Jetpack_Google_Analytics_Universal {
global $product;
$product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
- $selector = ".single_add_to_cart_button";
+ $selector = '.single_add_to_cart_button';
wc_enqueue_js(
"$( '" . esc_js( $selector ) . "' ).click( function() {
@@ -190,6 +216,9 @@ class Jetpack_Google_Analytics_Universal {
);
}
+ /**
+ * Enqueue add-to-cart click tracking script for looped product views, if enabled.
+ */
public function loop_add_to_cart() {
if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
return;
@@ -204,7 +233,7 @@ class Jetpack_Google_Analytics_Universal {
return;
}
- $selector = ".add_to_cart_button:not(.product_type_variable, .product_type_grouped)";
+ $selector = '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)';
wc_enqueue_js(
"$( '" . esc_js( $selector ) . "' ).click( function() {
@@ -221,6 +250,9 @@ class Jetpack_Google_Analytics_Universal {
);
}
+ /**
+ * Enqueue remove-from-cart click tracking script, if enabled.
+ */
public function remove_from_cart() {
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
return;
@@ -273,6 +305,9 @@ class Jetpack_Google_Analytics_Universal {
return $url;
}
+ /**
+ * Enqueue listing impression tracking script, if enabled.
+ */
public function listing_impression() {
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
return;
@@ -282,25 +317,28 @@ class Jetpack_Google_Analytics_Universal {
return;
}
- if ( isset( $_GET['s'] ) ) {
- $list = "Search Results";
+ if ( isset( $_GET['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No site actions, just GA options being set.
+ $list = 'Search Results';
} else {
- $list = "Product List";
+ $list = 'Product List';
}
global $product, $woocommerce_loop;
$product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
$item_details = array(
- 'id' => $product_sku_or_id,
- 'name' => $product->get_title(),
+ 'id' => $product_sku_or_id,
+ 'name' => $product->get_title(),
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
- 'list' => $list,
- 'position' => $woocommerce_loop['loop']
+ 'list' => $list,
+ 'position' => $woocommerce_loop['loop'],
);
- wc_enqueue_js( "ga( 'ec:addImpression', " . wp_json_encode( $item_details ) . " );" );
+ wc_enqueue_js( "ga( 'ec:addImpression', " . wp_json_encode( $item_details ) . ' );' );
}
+ /**
+ * Enqueue listing click tracking script, if enabled.
+ */
public function listing_click() {
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
return;
@@ -310,22 +348,22 @@ class Jetpack_Google_Analytics_Universal {
return;
}
- if ( isset( $_GET['s'] ) ) {
- $list = "Search Results";
+ if ( isset( $_GET['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No site actions, just GA options being set.
+ $list = 'Search Results';
} else {
- $list = "Product List";
+ $list = 'Product List';
}
global $product, $woocommerce_loop;
$product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
- $selector = ".products .post-" . esc_js( $product->get_id() ) . " a";
+ $selector = '.products .post-' . esc_js( $product->get_id() ) . ' a';
$item_details = array(
- 'id' => $product_sku_or_id,
- 'name' => $product->get_title(),
+ 'id' => $product_sku_or_id,
+ 'name' => $product->get_title(),
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
- 'position' => $woocommerce_loop['loop']
+ 'position' => $woocommerce_loop['loop'],
);
wc_enqueue_js(
@@ -341,6 +379,9 @@ class Jetpack_Google_Analytics_Universal {
);
}
+ /**
+ * Enqueue product detail view tracking script, if enabled.
+ */
public function product_detail() {
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
return;
@@ -354,17 +395,20 @@ class Jetpack_Google_Analytics_Universal {
$product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
$item_details = array(
- 'id' => $product_sku_or_id,
- 'name' => $product->get_title(),
+ 'id' => $product_sku_or_id,
+ 'name' => $product->get_title(),
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
- 'price' => $product->get_price()
+ 'price' => $product->get_price(),
);
wc_enqueue_js(
- "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );" .
+ "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . ' );' .
"ga( 'ec:setAction', 'detail' );"
);
}
+ /**
+ * Enqueue post-checkout tracking script, if enabled.
+ */
public function checkout_process() {
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
return;
@@ -375,24 +419,24 @@ class Jetpack_Google_Analytics_Universal {
}
$universal_commands = array();
- $cart = WC()->cart->get_cart();
+ $cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ) {
/**
* This filter is already documented in woocommerce/templates/cart/cart.php
*/
- $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
+ $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
$item_details = array(
- 'id' => $product_sku_or_id,
- 'name' => $product->get_title(),
+ 'id' => $product_sku_or_id,
+ 'name' => $product->get_title(),
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
- 'price' => $product->get_price(),
- 'quantity' => $cart_item[ 'quantity' ]
+ 'price' => $product->get_price(),
+ 'quantity' => $cart_item['quantity'],
);
- array_push( $universal_commands, "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );" );
+ array_push( $universal_commands, "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . ' );' );
}
array_push( $universal_commands, "ga( 'ec:setAction','checkout' );" );
@@ -400,6 +444,11 @@ class Jetpack_Google_Analytics_Universal {
wc_enqueue_js( implode( "\r\n", $universal_commands ) );
}
+ /**
+ * Enqueue pageview event in footer of all pages.
+ *
+ * Action hook added with later priority to come after all of the above tracking.
+ */
public function send_pageview_in_footer() {
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
return;
diff --git a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-utils.php b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-utils.php
index 807461de..ceabc8ff 100644
--- a/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-utils.php
+++ b/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-utils.php
@@ -1,22 +1,25 @@
-<?php
-
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
-* Jetpack_Google_Analytics_Options provides a single interface to module options
-*
-* @author allendav
-*/
+ * Jetpack_Google_Analytics_Options provides a single interface to module options
+ *
+ * @author allendav
+ */
/**
-* Bail if accessed directly
-*/
+ * Bail if accessed directly
+ */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
+/**
+ * Jetpack_Google_Analytics_Utils main class.
+ */
class Jetpack_Google_Analytics_Utils {
/**
* Gets product categories or varation attributes as a formatted concatenated string
- * @param WC_Product
+ *
+ * @param WC_Product $product Product to get categories/variations for.
* @return string
*/
public static function get_product_categories_concatenated( $product ) {
@@ -32,21 +35,22 @@ class Jetpack_Google_Analytics_Utils {
if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
$line = wc_get_formatted_variation( $variation_data, true );
} else {
- $out = array();
+ $out = array();
$categories = get_the_terms( $product->get_id(), 'product_cat' );
if ( $categories ) {
foreach ( $categories as $category ) {
$out[] = $category->name;
}
}
- $line = join( "/", $out );
+ $line = join( '/', $out );
}
return $line;
}
/**
* Gets a product's SKU with fallback to just ID. IDs are prepended with a hash symbol.
- * @param WC_Product
+ *
+ * @param WC_Product $product Product to get SKU/ID for.
* @return string
*/
public static function get_product_sku_or_id( $product ) {
@@ -60,4 +64,4 @@ class Jetpack_Google_Analytics_Utils {
return $product->get_sku() ? $product->get_sku() : '#' . $product->get_id();
}
-} \ No newline at end of file
+}