ecommerce track Closes #1361.

This commit is contained in:
Mike Jolley 2012-08-10 16:56:13 +01:00
parent d351240e6a
commit 10a39244bd
4 changed files with 84 additions and 84 deletions

View File

@ -260,21 +260,21 @@ class WC_Order {
/** Gets shipping and product tax */ /** Gets shipping and product tax */
function get_total_tax() { function get_total_tax() {
return apply_filters( 'woocommerce_order_amount_total_tax', $this->order_tax + $this->order_shipping_tax ); return apply_filters( 'woocommerce_order_amount_total_tax', number_format( $this->order_tax + $this->order_shipping_tax, 2, '.', '' ) );
} }
/** /**
* gets the total (product) discount amount - these are applied before tax * gets the total (product) discount amount - these are applied before tax
*/ */
function get_cart_discount() { function get_cart_discount() {
return apply_filters( 'woocommerce_order_amount_cart_discount', $this->cart_discount ); return apply_filters( 'woocommerce_order_amount_cart_discount', number_format( $this->cart_discount, 2, '.', '' ) );
} }
/** /**
* gets the total (product) discount amount - these are applied before tax * gets the total (product) discount amount - these are applied before tax
*/ */
function get_order_discount() { function get_order_discount() {
return apply_filters( 'woocommerce_order_amount_order_discount', $this->order_discount ); return apply_filters( 'woocommerce_order_amount_order_discount', number_format( $this->order_discount, 2, '.', '' ) );
} }
/** /**
@ -282,18 +282,18 @@ class WC_Order {
*/ */
function get_total_discount() { function get_total_discount() {
if ($this->order_discount || $this->cart_discount) : if ($this->order_discount || $this->cart_discount) :
return apply_filters( 'woocommerce_order_amount_total_discount', $this->order_discount + $this->cart_discount ); return apply_filters( 'woocommerce_order_amount_total_discount', number_format( $this->order_discount + $this->cart_discount, 2, '.', '' ) );
endif; endif;
} }
/** Gets shipping */ /** Gets shipping */
function get_shipping() { function get_shipping() {
return apply_filters( 'woocommerce_order_amount_shipping', $this->order_shipping ); return apply_filters( 'woocommerce_order_amount_shipping', number_format( $this->order_shipping, 2, '.', '' ) );
} }
/** Gets shipping tax amount */ /** Gets shipping tax amount */
function get_shipping_tax() { function get_shipping_tax() {
return apply_filters( 'woocommerce_order_amount_shipping_tax', $this->order_shipping_tax ); return apply_filters( 'woocommerce_order_amount_shipping_tax', number_format( $this->order_shipping_tax, 2, '.', '' ) );
} }
/** Gets shipping method title */ /** Gets shipping method title */
@ -303,7 +303,7 @@ class WC_Order {
/** Gets order total */ /** Gets order total */
function get_total() { function get_total() {
return apply_filters( 'woocommerce_order_amount_total', $this->order_total ); return apply_filters( 'woocommerce_order_amount_total', number_format( $this->order_total, 2, '.', '' ) );
} }
/** Get item subtotal - this is the cost before discount */ /** Get item subtotal - this is the cost before discount */
@ -359,7 +359,7 @@ class WC_Order {
/** Deprecated functions */ /** Deprecated functions */
function get_order_total() { function get_order_total() {
return apply_filters( 'woocommerce_order_amount_total', $this->order_total ); return $this->get_total();
} }
function get_item_cost( $item, $inc_tax = false ) { function get_item_cost( $item, $inc_tax = false ) {

View File

@ -1121,7 +1121,7 @@ class WC_Product {
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' ); $attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
$image_link = $attachment ? current( $attachment ) : ''; $image_link = $attachment ? current( $attachment ) : '';
$image_title = get_the_title( $attachment_id ); $image_title = get_the_title( $attachment_id );
} else { } else {
$image = $image_link = $image_title = ''; $image = $image_link = $image_title = '';
@ -1239,8 +1239,6 @@ class WC_Product {
function variable_product_sync() { function variable_product_sync() {
global $woocommerce; global $woocommerce;
if (!$this->is_type('variable')) return;
$children = get_posts( array( $children = get_posts( array(
'post_parent' => $this->id, 'post_parent' => $this->id,
'posts_per_page'=> -1, 'posts_per_page'=> -1,
@ -1280,8 +1278,7 @@ class WC_Product {
$this->price = $this->min_variation_price; $this->price = $this->min_variation_price;
if ( $this->min_variation_price !== '' ) $woocommerce->clear_product_transients( $this->id );
$woocommerce->clear_product_transients( $this->id );
} }
} }

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Google Analytics Integration * Google Analytics Integration
* *
* Allows tracking code to be inserted into store pages. * Allows tracking code to be inserted into store pages.
* *
* @class WC_Google_Analytics * @class WC_Google_Analytics
@ -10,15 +10,15 @@
* @author WooThemes * @author WooThemes
*/ */
class WC_Google_Analytics extends WC_Integration { class WC_Google_Analytics extends WC_Integration {
public function __construct() { public function __construct() {
$this->id = 'google_analytics'; $this->id = 'google_analytics';
$this->method_title = __( 'Google Analytics', 'woocommerce' ); $this->method_title = __( 'Google Analytics', 'woocommerce' );
$this->method_description = __( 'Google Analytics is a free service offered by Google that generates detailed statistics about the visitors to a website.', 'woocommerce' ); $this->method_description = __( 'Google Analytics is a free service offered by Google that generates detailed statistics about the visitors to a website.', 'woocommerce' );
// Load the form fields. // Load the form fields.
$this->init_form_fields(); $this->init_form_fields();
// Load the settings. // Load the settings.
$this->init_settings(); $this->init_settings();
@ -27,32 +27,32 @@ class WC_Google_Analytics extends WC_Integration {
$this->ga_standard_tracking_enabled = $this->settings['ga_standard_tracking_enabled']; $this->ga_standard_tracking_enabled = $this->settings['ga_standard_tracking_enabled'];
$this->ga_ecommerce_tracking_enabled = $this->settings['ga_ecommerce_tracking_enabled']; $this->ga_ecommerce_tracking_enabled = $this->settings['ga_ecommerce_tracking_enabled'];
$this->ga_event_tracking_enabled = $this->settings['ga_event_tracking_enabled']; $this->ga_event_tracking_enabled = $this->settings['ga_event_tracking_enabled'];
// Actions // Actions
add_action( 'woocommerce_update_options_integration_google_analytics', array( &$this, 'process_admin_options') ); add_action( 'woocommerce_update_options_integration_google_analytics', array( &$this, 'process_admin_options') );
// Tracking code // Tracking code
add_action( 'wp_footer', array( &$this, 'google_tracking_code' ) ); add_action( 'wp_footer', array( &$this, 'google_tracking_code' ) );
add_action( 'woocommerce_thankyou', array( &$this, 'ecommerce_tracking_code' ) ); add_action( 'woocommerce_thankyou', array( &$this, 'ecommerce_tracking_code' ) );
// Event tracking code // Event tracking code
add_action( 'woocommerce_after_add_to_cart_button', array( &$this, 'add_to_cart' ) ); add_action( 'woocommerce_after_add_to_cart_button', array( &$this, 'add_to_cart' ) );
add_action( 'woocommerce_after_shop_loop', array( &$this, 'loop_add_to_cart' ) ); add_action( 'woocommerce_after_shop_loop', array( &$this, 'loop_add_to_cart' ) );
} }
/** /**
* Initialise Settings Form Fields * Initialise Settings Form Fields
*/ */
function init_form_fields() { function init_form_fields() {
$this->form_fields = array( $this->form_fields = array(
'ga_id' => array( 'ga_id' => array(
'title' => __('Google Analytics ID', 'woocommerce'), 'title' => __('Google Analytics ID', 'woocommerce'),
'description' => __('Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce'), 'description' => __('Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce'),
'type' => 'text', 'type' => 'text',
'default' => get_option('woocommerce_ga_id') // Backwards compat 'default' => get_option('woocommerce_ga_id') // Backwards compat
), ),
'ga_standard_tracking_enabled' => array( 'ga_standard_tracking_enabled' => array(
'title' => __('Tracking code', 'woocommerce'), 'title' => __('Tracking code', 'woocommerce'),
'label' => __('Add tracking code to your site\'s footer. You don\'t need to enable this if using a 3rd party analytics plugin.', 'woocommerce'), 'label' => __('Add tracking code to your site\'s footer. You don\'t need to enable this if using a 3rd party analytics plugin.', 'woocommerce'),
'type' => 'checkbox', 'type' => 'checkbox',
@ -72,21 +72,21 @@ class WC_Google_Analytics extends WC_Integration {
'default' => 'no' 'default' => 'no'
) )
); );
} // End init_form_fields() } // End init_form_fields()
/** /**
* Google Analytics standard tracking * Google Analytics standard tracking
**/ **/
function google_tracking_code() { function google_tracking_code() {
global $woocommerce; global $woocommerce;
if ( is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no" ) return; if ( is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no" ) return;
$tracking_id = $this->ga_id; $tracking_id = $this->ga_id;
if ( ! $tracking_id ) return; if ( ! $tracking_id ) return;
$loggedin = ( is_user_logged_in() ) ? 'yes' : 'no'; $loggedin = ( is_user_logged_in() ) ? 'yes' : 'no';
if ( is_user_logged_in() ) { if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $user_id = get_current_user_id();
@ -96,9 +96,9 @@ class WC_Google_Analytics extends WC_Integration {
$user_id = ''; $user_id = '';
$username = __('Guest', 'woocommerce'); $username = __('Guest', 'woocommerce');
} }
$woocommerce->add_inline_js(" echo "<script type='text/javascript'>
var _gaq = _gaq || []; var _gaq = _gaq || [];
_gaq.push( _gaq.push(
['_setAccount', '" . $tracking_id . "'], ['_setAccount', '" . $tracking_id . "'],
@ -107,36 +107,38 @@ class WC_Google_Analytics extends WC_Integration {
['_setCustomVar', 3, 'username', '" . $username . "', 1], ['_setCustomVar', 3, 'username', '" . $username . "', 1],
['_trackPageview'] ['_trackPageview']
); );
(function() { (function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 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'; 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); var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})(); })();
"); </script>";
} }
/** /**
* Google Analytics eCommerce tracking * Google Analytics eCommerce tracking
**/ **/
function ecommerce_tracking_code( $order_id ) { function ecommerce_tracking_code( $order_id ) {
global $woocommerce; global $woocommerce;
if ( is_admin() || current_user_can('manage_options') || $this->ga_ecommerce_tracking_enabled == "no" ) return; if ( $this->ga_ecommerce_tracking_enabled == "no" || current_user_can('manage_options') )
return;
$tracking_id = $this->ga_id; $tracking_id = $this->ga_id;
if ( ! $tracking_id ) return; if ( ! $tracking_id ) return;
// Doing eCommerce tracking so unhook standard tracking from the footer // Doing eCommerce tracking so unhook standard tracking from the footer
remove_action('wp_footer', array( &$this, 'google_tracking_code' ) ); remove_action( 'wp_footer', array( &$this, 'google_tracking_code' ) );
// Get the order and output tracking code // Get the order and output tracking code
$order = new WC_Order($order_id); $order = new WC_Order( $order_id );
$loggedin = (is_user_logged_in()) ? 'yes' : 'no'; $loggedin = is_user_logged_in() ? 'yes' : 'no';
if (is_user_logged_in()) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $user_id = get_current_user_id();
$current_user = get_user_by('id', $user_id); $current_user = get_user_by('id', $user_id);
$username = $current_user->user_login; $username = $current_user->user_login;
@ -144,10 +146,10 @@ class WC_Google_Analytics extends WC_Integration {
$user_id = ''; $user_id = '';
$username = __('Guest', 'woocommerce'); $username = __('Guest', 'woocommerce');
} }
$code = " $code = "
var _gaq = _gaq || []; var _gaq = _gaq || [];
_gaq.push( _gaq.push(
['_setAccount', '" . $tracking_id . "'], ['_setAccount', '" . $tracking_id . "'],
['_setCustomVar', 1, 'logged-in', '" . $loggedin . "', 1], ['_setCustomVar', 1, 'logged-in', '" . $loggedin . "', 1],
@ -155,11 +157,11 @@ class WC_Google_Analytics extends WC_Integration {
['_setCustomVar', 3, 'username', '" . $username . "', 1], ['_setCustomVar', 3, 'username', '" . $username . "', 1],
['_trackPageview'] ['_trackPageview']
); );
_gaq.push(['_addTrans', _gaq.push(['_addTrans',
'" . $order_id . "', // order ID - required '" . $order_id . "', // order ID - required
'" . get_bloginfo( 'name' ) . "', // affiliation or store name '" . get_bloginfo( 'name' ) . "', // affiliation or store name
'" . $order->order_total . "', // total - required '" . $order->get_total() . "', // total - required
'" . $order->get_total_tax() . "', // tax '" . $order->get_total_tax() . "', // tax
'" . $order->get_shipping() . "', // shipping '" . $order->get_shipping() . "', // shipping
'" . $order->billing_city . "', // city '" . $order->billing_city . "', // city
@ -167,21 +169,21 @@ class WC_Google_Analytics extends WC_Integration {
'" . $order->billing_country . "' // country '" . $order->billing_country . "' // country
]); ]);
"; ";
// Order items // Order items
if ( $order->get_items() ) { if ( $order->get_items() ) {
foreach ( $order->get_items() as $item ) { foreach ( $order->get_items() as $item ) {
$_product = $order->get_product_from_item( $item ); $_product = $order->get_product_from_item( $item );
$code .= "_gaq.push(['_addItem',"; $code .= "_gaq.push(['_addItem',";
$code .= "'" . $order_id . "',"; $code .= "'" . $order_id . "',";
$code .= "'" . ( ( ! empty( $_product->sku ) ) ? __('SKU:', 'woocommerce') . ' ' . $_product->sku : $_product->id ) . "',"; $code .= "'" . ( $_product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $_product->get_sku() : $_product->id ) . "',";
$code .= "'" . $item['name'] . "',"; $code .= "'" . $item['name'] . "',";
if ( isset( $_product->variation_data ) ) { if ( isset( $_product->variation_data ) ) {
$code .= "'" . woocommerce_get_formatted_variation( $_product->variation_data, true ) . "',"; $code .= "'" . woocommerce_get_formatted_variation( $_product->variation_data, true ) . "',";
} else { } else {
$out = array(); $out = array();
$categories = get_the_terms($_product->id, 'product_cat'); $categories = get_the_terms($_product->id, 'product_cat');
@ -193,86 +195,86 @@ class WC_Google_Analytics extends WC_Integration {
$code .= "'" . join( "/", $out) . "',"; $code .= "'" . join( "/", $out) . "',";
} }
$code .= "'" . ( $item['line_total'] / $item['qty'] ) . "',"; $code .= "'" . $order->get_item_total( $item ) . "',";
$code .= "'" . $item['qty'] . "'"; $code .= "'" . $item['qty'] . "'";
$code .= "]);"; $code .= "]);";
} }
} }
$code .= " $code .= "
_gaq.push(['_trackTrans']); // submits transaction to the Analytics servers _gaq.push(['_trackTrans']); // submits transaction to the Analytics servers
(function() { (function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 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'; 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); var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})(); })();
"; ";
$woocommerce->add_inline_js( $code ); echo '<script type="text/javascript">' . $code . '</script>';
} }
/** /**
* Google Analytics event tracking for single product add to cart * Google Analytics event tracking for single product add to cart
* *
**/ **/
function add_to_cart() { function add_to_cart() {
if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return; if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;
if ( ! is_single() ) return; if ( ! is_single() ) return;
global $product; global $product;
$parameters = array(); $parameters = array();
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
$parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'"; $parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
$parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'"; $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'";
$parameters['label'] = "'#" . esc_js( $product->id ) . "'"; $parameters['label'] = "'#" . esc_js( $product->id ) . "'";
$this->event_tracking_code( $parameters, '.single_add_to_cart_button' ); $this->event_tracking_code( $parameters, '.single_add_to_cart_button' );
} }
/** /**
* Google Analytics event tracking for loop add to cart * Google Analytics event tracking for loop add to cart
* *
**/ **/
function loop_add_to_cart() { function loop_add_to_cart() {
if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return; if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;
$parameters = array(); $parameters = array();
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
$parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'"; $parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
$parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'"; $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'";
$parameters['label'] = "'#' + $(this).attr('data-product_id')"; // Product ID $parameters['label'] = "'#' + $(this).attr('data-product_id')"; // Product ID
$this->event_tracking_code( $parameters, '.add_to_cart_button' ); $this->event_tracking_code( $parameters, '.add_to_cart_button' );
} }
/** /**
* Google Analytics event tracking for loop add to cart * Google Analytics event tracking for loop add to cart
* *
* @param array $parameters - associative array of _trackEvent parameters * @param array $parameters - associative array of _trackEvent parameters
* @param string $selector - jQuery selector for binding click event * @param string $selector - jQuery selector for binding click event
**/ **/
private function event_tracking_code( $parameters, $selector ) { private function event_tracking_code( $parameters, $selector ) {
global $woocommerce; global $woocommerce;
$parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters ); $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
$woocommerce->add_inline_js(" $woocommerce->add_inline_js("
$('" . $selector . "').click(function() { $('" . $selector . "').click(function() {
" . sprintf( "_gaq.push(['_trackEvent', %s, %s, %s]);", $parameters['category'], $parameters['action'], $parameters['label'] ) . " " . sprintf( "_gaq.push(['_trackEvent', %s, %s, %s]);", $parameters['category'], $parameters['action'], $parameters['label'] ) . "
}); });
"); ");
} }
private function disable_tracking( $type ) { private function disable_tracking( $type ) {
if( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) return true; if( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) return true;
} }
} }
/** /**

View File

@ -160,7 +160,8 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Tweak - Order page styling * Tweak - Order page styling
* Fix - has_file() handling for variations * Fix - has_file() handling for variations
* Fix - Hide if cart is empty option * Fix - Hide if cart is empty option
* FIx - Hide individual variations from frontend * Fix - Hide individual variations from frontend
* Fix - Google Analytics ecommerce tracking
= 1.6.2 - 09/08/2012 = = 1.6.2 - 09/08/2012 =
* Feature - Added google analytics event tracking for add to cart buttons (thanks to Max Rice) * Feature - Added google analytics event tracking for add to cart buttons (thanks to Max Rice)