Add upstream
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Jetpack_Google_Analytics_Legacy hooks and enqueues support for ga.js
|
||||
* https://developers.google.com/analytics/devguides/collection/gajs/
|
||||
*
|
||||
* @author Aaron D. Campbell (original)
|
||||
* @author allendav
|
||||
*/
|
||||
|
||||
/**
|
||||
* Bail if accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Jetpack_Google_Analytics_Legacy {
|
||||
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' ) );
|
||||
add_action( 'wp_footer', array( $this, 'insert_code' ) );
|
||||
add_action( 'wp_footer', array( $this, 'jetpack_wga_classic_track_add_to_cart' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to generate a tracking URL
|
||||
* Called exclusively by insert_code
|
||||
*
|
||||
* @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.
|
||||
foreach ( $track as $k => $value ) {
|
||||
if ( strpos( strtolower( $value ), strtolower( $site_url ) ) === 0 ) {
|
||||
$track[ $k ] = substr( $track[ $k ], strlen( $site_url ) );
|
||||
}
|
||||
if ( 'data' === $k ) {
|
||||
$track[ $k ] = preg_replace( '/^https?:\/\/|^\/+/i', '', $track[ $k ] );
|
||||
}
|
||||
|
||||
// This way we don't lose search data.
|
||||
if ( 'data' === $k && 'search' === $track['code'] ) {
|
||||
$track[ $k ] = rawurlencode( $track[ $k ] );
|
||||
} else {
|
||||
$track[ $k ] = preg_replace( '/[^a-z0-9\.\/\+\?=-]+/i', '_', $track[ $k ] );
|
||||
}
|
||||
|
||||
$track[ $k ] = trim( $track[ $k ], '_' );
|
||||
}
|
||||
$char = ( strpos( $track['data'], '?' ) === false ) ? '?' : '&';
|
||||
return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); // Input var okay.
|
||||
}
|
||||
|
||||
/**
|
||||
* This injects the Google Analytics code into the footer of the page.
|
||||
* Called exclusively by wp_footer action
|
||||
*/
|
||||
public function insert_code() {
|
||||
$tracking_id = Jetpack_Google_Analytics_Options::get_tracking_code();
|
||||
if ( empty( $tracking_id ) ) {
|
||||
echo "<!-- Your Google Analytics Plugin is missing the tracking ID -->\r\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// If we're in the admin_area, return without inserting code.
|
||||
if ( is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$custom_vars = array(
|
||||
"_gaq.push(['_setAccount', '{$tracking_id}']);",
|
||||
);
|
||||
|
||||
$track = array();
|
||||
if ( is_404() ) {
|
||||
// This is a 404 and we are supposed to track them.
|
||||
$custom_vars[] = "_gaq.push(['_trackEvent', '404', document.location.href, document.referrer]);";
|
||||
} elseif (
|
||||
is_search()
|
||||
&& isset( $_REQUEST['s'] )
|
||||
) {
|
||||
// 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['code'] = 'search';
|
||||
}
|
||||
|
||||
if ( ! empty( $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( '&', '&', $track['url'] ) );
|
||||
$custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);";
|
||||
} else {
|
||||
$custom_vars[] = "_gaq.push(['_trackPageview']);";
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow for additional elements to be added to the classic Google Analytics queue (_gaq) array
|
||||
*
|
||||
* @since 5.4.0
|
||||
*
|
||||
* @param array $custom_vars Array of classic Google Analytics queue elements
|
||||
*/
|
||||
$custom_vars = apply_filters( 'jetpack_wga_classic_custom_vars', $custom_vars );
|
||||
|
||||
// Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
|
||||
printf(
|
||||
"<!-- Jetpack Google Analytics -->
|
||||
<script type='text/javascript'>
|
||||
var _gaq = _gaq || [];
|
||||
%s
|
||||
(function() {
|
||||
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
||||
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
||||
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
||||
})();
|
||||
</script>\r\n",
|
||||
implode( "\r\n", $custom_vars )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function jetpack_wga_classic_anonymize_ip( $custom_vars ) {
|
||||
if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) {
|
||||
array_push( $custom_vars, "_gaq.push(['_gat._anonymizeIp']);" );
|
||||
}
|
||||
|
||||
return $custom_vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function jetpack_wga_classic_track_purchases( $custom_vars ) {
|
||||
global $wp;
|
||||
|
||||
if ( ! class_exists( 'WooCommerce' ) ) {
|
||||
return $custom_vars;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) {
|
||||
return $custom_vars;
|
||||
}
|
||||
|
||||
$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 ) ) {
|
||||
$order = new WC_Order( $order_id );
|
||||
|
||||
// [ '_add_Trans', '123', 'Site Title', '21.00', '1.00', '5.00', 'Snohomish', 'WA', 'USA' ]
|
||||
array_push(
|
||||
$custom_vars,
|
||||
sprintf(
|
||||
'_gaq.push( %s );', json_encode(
|
||||
array(
|
||||
'_addTrans',
|
||||
(string) $order->get_order_number(),
|
||||
get_bloginfo( 'name' ),
|
||||
(string) $order->get_total(),
|
||||
(string) $order->get_total_tax(),
|
||||
(string) $order->get_total_shipping(),
|
||||
(string) $order->get_billing_city(),
|
||||
(string) $order->get_billing_state(),
|
||||
(string) $order->get_billing_country()
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Order items
|
||||
if ( $order->get_items() ) {
|
||||
foreach ( $order->get_items() as $item ) {
|
||||
$product = $order->get_product_from_item( $item );
|
||||
$product_sku_or_id = $product->get_sku() ? $product->get_sku() : $product->get_id();
|
||||
|
||||
array_push(
|
||||
$custom_vars,
|
||||
sprintf(
|
||||
'_gaq.push( %s );', json_encode(
|
||||
array(
|
||||
'_addItem',
|
||||
(string) $order->get_order_number(),
|
||||
(string) $product_sku_or_id,
|
||||
$item['name'],
|
||||
Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
|
||||
(string) $order->get_item_total( $item ),
|
||||
(string) $item['qty']
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
} // get_items
|
||||
|
||||
// Mark the order as tracked
|
||||
update_post_meta( $order_id, '_ga_tracked', 1 );
|
||||
array_push( $custom_vars, "_gaq.push(['_trackTrans']);" );
|
||||
} // order not yet tracked
|
||||
} // is order received page
|
||||
|
||||
return $custom_vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to add footer javascript to track user clicking on add-to-cart buttons
|
||||
* on single views (.single_add_to_cart_button) and list views (.add_to_cart_button)
|
||||
*/
|
||||
public function jetpack_wga_classic_track_add_to_cart() {
|
||||
if ( ! class_exists( 'WooCommerce' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_product() ) { // product page
|
||||
global $product;
|
||||
$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 ) . "']);
|
||||
} );"
|
||||
);
|
||||
} else if ( is_woocommerce() ) { // any other page that uses templates (like product lists, archives, etc)
|
||||
wc_enqueue_js(
|
||||
"$( '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' ).click( function() {
|
||||
var label = $( this ).data( 'product_sku' ) ? $( this ).data( 'product_sku' ) : '#' + $( this ).data( 'product_id' );
|
||||
_gaq.push(['_trackEvent', 'Products', 'Add to Cart', label]);
|
||||
} );"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Jetpack_Google_Analytics_Options provides a single interface to module options
|
||||
*
|
||||
* @author allendav
|
||||
*/
|
||||
|
||||
/**
|
||||
* Bail if accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Jetpack_Google_Analytics_Options {
|
||||
public static function get_option( $option_name, $default = false ) {
|
||||
$o = get_option( 'jetpack_wga' );
|
||||
return isset( $o[ $option_name ] ) ? $o[ $option_name ] : $default;
|
||||
}
|
||||
|
||||
public static function get_tracking_code() {
|
||||
return self::get_option( 'code', '' );
|
||||
}
|
||||
|
||||
public static function has_tracking_code() {
|
||||
$code = self::get_tracking_code();
|
||||
return ! empty( $code );
|
||||
}
|
||||
|
||||
// Options used by both legacy and universal analytics
|
||||
public static function anonymize_ip_is_enabled() {
|
||||
return self::get_option( 'anonymize_ip' );
|
||||
}
|
||||
|
||||
// eCommerce options used by both legacy and universal analytics
|
||||
public static function track_purchases_is_enabled() {
|
||||
return self::get_option( 'ec_track_purchases' );
|
||||
}
|
||||
|
||||
public static function track_add_to_cart_is_enabled() {
|
||||
return self::get_option( 'ec_track_add_to_cart' );
|
||||
}
|
||||
|
||||
// Enhanced eCommerce options
|
||||
public static function enhanced_ecommerce_tracking_is_enabled() {
|
||||
return self::get_option( 'enh_ec_tracking' );
|
||||
}
|
||||
|
||||
public static function track_remove_from_cart_is_enabled() {
|
||||
return self::get_option( 'enh_ec_track_remove_from_cart' );
|
||||
}
|
||||
|
||||
public static function track_product_impressions_is_enabled() {
|
||||
return self::get_option( 'enh_ec_track_prod_impression' );
|
||||
}
|
||||
|
||||
public static function track_product_clicks_is_enabled() {
|
||||
return self::get_option( 'enh_ec_track_prod_click' );
|
||||
}
|
||||
|
||||
public static function track_product_detail_view_is_enabled() {
|
||||
return self::get_option( 'enh_ec_track_prod_detail_view' );
|
||||
}
|
||||
|
||||
public static function track_checkout_started_is_enabled() {
|
||||
return self::get_option( 'enh_ec_track_checkout_started' );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,410 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Jetpack_Google_Analytics_Universal {
|
||||
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' ) );
|
||||
|
||||
add_action( 'wp_head', array( $this, 'wp_head' ), 999999 );
|
||||
|
||||
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
|
||||
add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
|
||||
add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
|
||||
add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
|
||||
add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
|
||||
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_impression' ) );
|
||||
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_click' ) );
|
||||
add_action( 'woocommerce_after_single_product', array( $this, 'product_detail' ) );
|
||||
add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
|
||||
|
||||
// we need to send a pageview command last - so we use priority 24 to add
|
||||
// this command's JavaScript just before wc_print_js is called (pri 25)
|
||||
add_action( 'wp_footer', array( $this, 'send_pageview_in_footer' ), 24 );
|
||||
}
|
||||
|
||||
public function wp_head() {
|
||||
$tracking_code = Jetpack_Google_Analytics_Options::get_tracking_code();
|
||||
if ( empty( $tracking_code ) ) {
|
||||
echo "<!-- No tracking ID configured for Jetpack Google Analytics -->\r\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// If we're in the admin_area, return without inserting code.
|
||||
if ( is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow for additional elements to be added to the universal Google Analytics queue (ga) array
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param array $custom_vars Array of universal Google Analytics queue elements
|
||||
*/
|
||||
$universal_commands = apply_filters( 'jetpack_wga_universal_commands', array() );
|
||||
|
||||
$async_code = "
|
||||
<!-- Jetpack Google Analytics -->
|
||||
<script>
|
||||
window.ga = window.ga || function(){ ( ga.q = ga.q || [] ).push( arguments ) }; ga.l=+new Date;
|
||||
ga( 'create', '%tracking_id%', 'auto' );
|
||||
ga( 'require', 'ec' );
|
||||
%universal_commands%
|
||||
</script>
|
||||
<script async src='https://www.google-analytics.com/analytics.js'></script>
|
||||
<!-- End Jetpack Google Analytics -->
|
||||
";
|
||||
$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 );
|
||||
|
||||
echo "$async_code\r\n";
|
||||
}
|
||||
|
||||
public function maybe_anonymize_ip( $command_array ) {
|
||||
if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) {
|
||||
array_push( $command_array, "ga( 'set', 'anonymizeIp', true );" );
|
||||
}
|
||||
|
||||
return $command_array;
|
||||
}
|
||||
|
||||
public function maybe_track_purchases( $command_array ) {
|
||||
global $wp;
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) {
|
||||
return $command_array;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WooCommerce' ) ) {
|
||||
return $command_array;
|
||||
}
|
||||
|
||||
$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
|
||||
if ( ! $minimum_woocommerce_active ) {
|
||||
return $command_array;
|
||||
}
|
||||
|
||||
if ( ! is_order_received_page() ) {
|
||||
return $command_array;
|
||||
}
|
||||
|
||||
$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
|
||||
if ( 0 == $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 ) ) {
|
||||
return $command_array;
|
||||
}
|
||||
|
||||
$order = new WC_Order( $order_id );
|
||||
$order_currency = $order->get_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_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
|
||||
|
||||
$item_details = array(
|
||||
'id' => $product_sku_or_id,
|
||||
'name' => $item['name'],
|
||||
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
|
||||
'price' => $order->get_item_total( $item ),
|
||||
'quantity' => $item['qty'],
|
||||
);
|
||||
$command = "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );";
|
||||
array_push( $command_array, $command );
|
||||
}
|
||||
}
|
||||
|
||||
// Order summary
|
||||
$summary = array(
|
||||
'id' => $order->get_order_number(),
|
||||
'affiliation' => get_bloginfo( 'name' ),
|
||||
'revenue' => $order->get_total(),
|
||||
'tax' => $order->get_total_tax(),
|
||||
'shipping' => $order->get_total_shipping()
|
||||
);
|
||||
$command = "ga( 'ec:setAction', 'purchase', " . wp_json_encode( $summary ) . " );";
|
||||
array_push( $command_array, $command );
|
||||
|
||||
update_post_meta( $order_id, '_ga_tracked', 1 );
|
||||
|
||||
return $command_array;
|
||||
}
|
||||
|
||||
public function add_to_cart() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! is_single() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $product;
|
||||
|
||||
$product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
|
||||
$selector = ".single_add_to_cart_button";
|
||||
|
||||
wc_enqueue_js(
|
||||
"$( '" . esc_js( $selector ) . "' ).click( function() {
|
||||
var productDetails = {
|
||||
'id': '" . esc_js( $product_sku_or_id ) . "',
|
||||
'name' : '" . esc_js( $product->get_title() ) . "',
|
||||
'quantity': $( 'input.qty' ).val() ? $( 'input.qty' ).val() : '1',
|
||||
};
|
||||
ga( 'ec:addProduct', productDetails );
|
||||
ga( 'ec:setAction', 'add' );
|
||||
ga( 'send', 'event', 'UX', 'click', 'add to cart' );
|
||||
} );"
|
||||
);
|
||||
}
|
||||
|
||||
public function loop_add_to_cart() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WooCommerce' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
|
||||
if ( ! $minimum_woocommerce_active ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selector = ".add_to_cart_button:not(.product_type_variable, .product_type_grouped)";
|
||||
|
||||
wc_enqueue_js(
|
||||
"$( '" . esc_js( $selector ) . "' ).click( function() {
|
||||
var productSku = $( this ).data( 'product_sku' );
|
||||
var productID = $( this ).data( 'product_id' );
|
||||
var productDetails = {
|
||||
'id': productSku ? productSku : '#' + productID,
|
||||
'quantity': $( this ).data( 'quantity' ),
|
||||
};
|
||||
ga( 'ec:addProduct', productDetails );
|
||||
ga( 'ec:setAction', 'add' );
|
||||
ga( 'send', 'event', 'UX', 'click', 'add to cart' );
|
||||
} );"
|
||||
);
|
||||
}
|
||||
|
||||
public function remove_from_cart() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_remove_from_cart_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We listen at div.woocommerce because the cart 'form' contents get forcibly
|
||||
// updated and subsequent removals from cart would then not have this click
|
||||
// handler attached
|
||||
wc_enqueue_js(
|
||||
"$( 'div.woocommerce' ).on( 'click', 'a.remove', function() {
|
||||
var productSku = $( this ).data( 'product_sku' );
|
||||
var productID = $( this ).data( 'product_id' );
|
||||
var quantity = $( this ).parent().parent().find( '.qty' ).val()
|
||||
var productDetails = {
|
||||
'id': productSku ? productSku : '#' + productID,
|
||||
'quantity': quantity ? quantity : '1',
|
||||
};
|
||||
ga( 'ec:addProduct', productDetails );
|
||||
ga( 'ec:setAction', 'remove' );
|
||||
ga( 'send', 'event', 'UX', 'click', 'remove from cart' );
|
||||
} );"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the product ID and SKU to the remove product link (for use by remove_from_cart above) if not present
|
||||
*
|
||||
* @param string $url Full HTML a tag of the link to remove an item from the cart.
|
||||
* @param string $key Unique Key ID for a cart item.
|
||||
*/
|
||||
public function remove_from_cart_attributes( $url, $key ) {
|
||||
if ( false !== strpos( $url, 'data-product_id' ) ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$item = WC()->cart->get_cart_item( $key );
|
||||
$product = $item['data'];
|
||||
|
||||
$new_attributes = sprintf(
|
||||
'" data-product_id="%1$s" data-product_sku="%2$s">',
|
||||
esc_attr( $product->get_id() ),
|
||||
esc_attr( $product->get_sku() )
|
||||
);
|
||||
|
||||
$url = str_replace( '">', $new_attributes, $url );
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function listing_impression() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_product_impressions_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $_GET['s'] ) ) {
|
||||
$list = "Search Results";
|
||||
} else {
|
||||
$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(),
|
||||
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
|
||||
'list' => $list,
|
||||
'position' => $woocommerce_loop['loop']
|
||||
);
|
||||
wc_enqueue_js( "ga( 'ec:addImpression', " . wp_json_encode( $item_details ) . " );" );
|
||||
}
|
||||
|
||||
public function listing_click() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_product_clicks_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $_GET['s'] ) ) {
|
||||
$list = "Search Results";
|
||||
} else {
|
||||
$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";
|
||||
|
||||
$item_details = array(
|
||||
'id' => $product_sku_or_id,
|
||||
'name' => $product->get_title(),
|
||||
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
|
||||
'position' => $woocommerce_loop['loop']
|
||||
);
|
||||
|
||||
wc_enqueue_js(
|
||||
"$( '" . esc_js( $selector ) . "' ).click( function() {
|
||||
if ( true === $( this ).hasClass( 'add_to_cart_button' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );
|
||||
ga( 'ec:setAction', 'click', { list: '" . esc_js( $list ) . "' } );
|
||||
ga( 'send', 'event', 'UX', 'click', { list: '" . esc_js( $list ) . "' } );
|
||||
} );"
|
||||
);
|
||||
}
|
||||
|
||||
public function product_detail() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_product_detail_view_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $product;
|
||||
$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(),
|
||||
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
|
||||
'price' => $product->get_price()
|
||||
);
|
||||
wc_enqueue_js(
|
||||
"ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );" .
|
||||
"ga( 'ec:setAction', 'detail' );"
|
||||
);
|
||||
}
|
||||
|
||||
public function checkout_process() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! Jetpack_Google_Analytics_Options::track_checkout_started_is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$universal_commands = array();
|
||||
$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_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(),
|
||||
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
|
||||
'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:setAction','checkout' );" );
|
||||
|
||||
wc_enqueue_js( implode( "\r\n", $universal_commands ) );
|
||||
}
|
||||
|
||||
public function send_pageview_in_footer() {
|
||||
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WooCommerce' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wc_enqueue_js( "ga( 'send', 'pageview' );" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Jetpack_Google_Analytics_Options provides a single interface to module options
|
||||
*
|
||||
* @author allendav
|
||||
*/
|
||||
|
||||
/**
|
||||
* Bail if accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Jetpack_Google_Analytics_Utils {
|
||||
/**
|
||||
* Gets product categories or varation attributes as a formatted concatenated string
|
||||
* @param WC_Product
|
||||
* @return string
|
||||
*/
|
||||
public static function get_product_categories_concatenated( $product ) {
|
||||
if ( ! class_exists( 'WooCommerce' ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( ! $product ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
|
||||
if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
|
||||
$line = wc_get_formatted_variation( $variation_data, true );
|
||||
} else {
|
||||
$out = array();
|
||||
$categories = get_the_terms( $product->get_id(), 'product_cat' );
|
||||
if ( $categories ) {
|
||||
foreach ( $categories as $category ) {
|
||||
$out[] = $category->name;
|
||||
}
|
||||
}
|
||||
$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
|
||||
* @return string
|
||||
*/
|
||||
public static function get_product_sku_or_id( $product ) {
|
||||
if ( ! class_exists( 'WooCommerce' ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( ! $product ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $product->get_sku() ? $product->get_sku() : '#' . $product->get_id();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user