Skip to content

Commit

Permalink
add google analytics event tracking for add to cart
Browse files Browse the repository at this point in the history
Revised implementation of GA event tracking after feedback on #1315.
Adds jQuery .click bind on add to cart buttons on both single product
pages and loop pages.

new filter:
woocommerce_ga_event_tracking_parameters - allows change of category,
action, or label parameters included in _trackEvent

closes #1256
  • Loading branch information
maxrice committed Jul 29, 2012
1 parent 90f1339 commit c7eed14
Showing 1 changed file with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ public function __construct() {
$this->ga_id = $this->settings['ga_id'];
$this->ga_standard_tracking_enabled = $this->settings['ga_standard_tracking_enabled'];
$this->ga_ecommerce_tracking_enabled = $this->settings['ga_ecommerce_tracking_enabled'];
$this->ga_event_tracking_enabled = $this->settings['ga_event_tracking_enabled'];

// Actions
add_action( 'woocommerce_update_options_integration_google_analytics', array( &$this, 'process_admin_options') );

// Tracking code
add_action( 'wp_footer', array( &$this, 'google_tracking_code' ) );
add_action( 'woocommerce_thankyou', array( &$this, 'ecommerce_tracking_code' ) );

// Event tracking code
add_action( 'woocommerce_after_add_to_cart_button', array( &$this, 'add_to_cart' ) );
add_action( 'woocommerce_pagination', array( &$this, 'loop_add_to_cart' ) );
}

/**
Expand All @@ -57,8 +62,14 @@ function init_form_fields() {
'ga_ecommerce_tracking_enabled' => array(
'label' => __('Add eCommerce tracking code to the thankyou page', 'woocommerce'),
'type' => 'checkbox',
'checkboxgroup' => 'end',
'checkboxgroup' => '',
'default' => get_option('woocommerce_ga_ecommerce_tracking_enabled') ? get_option('woocommerce_ga_ecommerce_tracking_enabled') : 'no' // Backwards compat
),
'ga_event_tracking_enabled' => array(
'label' => __('Add event tracking code for add to cart actions', 'woocommerce'),
'type' => 'checkbox',
'checkboxgroup' => 'end',
'default' => 'no'
)
);

Expand Down Expand Up @@ -195,6 +206,72 @@ function ecommerce_tracking_code( $order_id ) {
</script>
<?php
}
/**
* Google Analytics event tracking for single product add to cart
*
**/
function add_to_cart() {

if( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;

global $product;

$parameters = array();
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
$parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
$parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'";
$parameters['label'] = "'" . esc_js( $product->get_title() ) . "'";

$this->event_tracking_code( $parameters, '.button.alt' );

}

/**
* Google Analytics event tracking for loop add to cart
*
**/
function loop_add_to_cart() {

if( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;

$parameters = array();
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
$parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
$parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'";
$parameters['label'] = "$(this).parent().find('h3').text()";

$this->event_tracking_code( $parameters, '.button.add_to_cart_button' );

}


/**
* Google Analytics event tracking for loop add to cart
*
* @param array $parameters - associative array of _trackEvent parameters
* @param string $selector - jQuery selector for binding click event
**/
private function event_tracking_code( $parameters, $selector ) {

$parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );

?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('<?php echo $selector; ?>').click(function() {
<?php printf( "_gaq.push(['_trackEvent', %s, %s, %s]);", $parameters['category'], $parameters['action'], $parameters['label'] ); ?>
});
});
</script>
<?php
}

private function disable_tracking( $type ) {

if( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) return true;

}

}

/**
Expand Down

0 comments on commit c7eed14

Please sign in to comment.