2012-04-30 19:50:35 +00:00
< ? php
2013-02-20 17:14:46 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2012-04-30 19:50:35 +00:00
/**
* Google Analytics Integration
2012-08-10 15:56:13 +00:00
*
2012-04-30 19:50:35 +00:00
* Allows tracking code to be inserted into store pages .
*
* @ class WC_Google_Analytics
2012-08-15 18:15:06 +00:00
* @ extends WC_Integration
* @ version 1.6 . 4
* @ package WooCommerce / Classes / Integrations
* @ author WooThemes
2012-04-30 19:50:35 +00:00
*/
class WC_Google_Analytics extends WC_Integration {
2012-08-10 15:56:13 +00:00
2012-08-15 18:15:06 +00:00
/**
* Init and hook in the integration .
*
* @ access public
* @ return void
*/
2012-08-10 15:56:13 +00:00
public function __construct () {
2012-04-30 19:50:35 +00:00
$this -> id = 'google_analytics' ;
$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' );
2012-08-10 15:56:13 +00:00
2012-04-30 19:50:35 +00:00
// Load the settings.
2013-01-03 10:38:59 +00:00
$this -> init_form_fields ();
2012-04-30 19:50:35 +00:00
$this -> init_settings ();
// Define user set variables
2012-12-31 12:07:43 +00:00
$this -> ga_id = $this -> get_option ( 'ga_id' );
$this -> ga_standard_tracking_enabled = $this -> get_option ( 'ga_standard_tracking_enabled' );
$this -> ga_ecommerce_tracking_enabled = $this -> get_option ( 'ga_ecommerce_tracking_enabled' );
$this -> ga_event_tracking_enabled = $this -> get_option ( 'ga_event_tracking_enabled' );
2012-08-10 15:56:13 +00:00
2012-04-30 19:50:35 +00:00
// Actions
2012-12-15 11:53:32 +00:00
add_action ( 'woocommerce_update_options_integration_google_analytics' , array ( $this , 'process_admin_options' ) );
2012-08-10 15:56:13 +00:00
2012-04-30 19:50:35 +00:00
// Tracking code
2012-12-15 11:53:32 +00:00
add_action ( 'wp_footer' , array ( $this , 'google_tracking_code' ) );
add_action ( 'woocommerce_thankyou' , array ( $this , 'ecommerce_tracking_code' ) );
2012-08-10 15:56:13 +00:00
2012-07-29 20:39:28 +00:00
// Event tracking code
2012-12-15 11:53:32 +00:00
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' ) );
2012-08-10 15:56:13 +00:00
}
2012-08-15 18:15:06 +00:00
/**
2012-04-30 19:50:35 +00:00
* Initialise Settings Form Fields
2012-08-15 18:15:06 +00:00
*
* @ access public
* @ return void
2012-04-30 19:50:35 +00:00
*/
function init_form_fields () {
2012-08-10 15:56:13 +00:00
$this -> form_fields = array (
'ga_id' => array (
2012-10-16 09:45:33 +00:00
'title' => __ ( 'Google Analytics ID' , 'woocommerce' ),
'description' => __ ( 'Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>' , 'woocommerce' ),
2012-04-30 19:50:35 +00:00
'type' => 'text' ,
2012-05-11 20:41:18 +00:00
'default' => get_option ( 'woocommerce_ga_id' ) // Backwards compat
2012-04-30 19:50:35 +00:00
),
2012-08-10 15:56:13 +00:00
'ga_standard_tracking_enabled' => array (
2012-10-16 09:45:33 +00:00
'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' ),
2012-04-30 19:50:35 +00:00
'type' => 'checkbox' ,
'checkboxgroup' => 'start' ,
2012-05-11 20:41:18 +00:00
'default' => get_option ( 'woocommerce_ga_standard_tracking_enabled' ) ? get_option ( 'woocommerce_ga_standard_tracking_enabled' ) : 'no' // Backwards compat
2012-04-30 19:50:35 +00:00
),
'ga_ecommerce_tracking_enabled' => array (
2012-10-16 09:45:33 +00:00
'label' => __ ( 'Add eCommerce tracking code to the thankyou page' , 'woocommerce' ),
2012-04-30 19:50:35 +00:00
'type' => 'checkbox' ,
2012-07-29 20:39:28 +00:00
'checkboxgroup' => '' ,
2012-05-11 20:41:18 +00:00
'default' => get_option ( 'woocommerce_ga_ecommerce_tracking_enabled' ) ? get_option ( 'woocommerce_ga_ecommerce_tracking_enabled' ) : 'no' // Backwards compat
2012-07-29 20:39:28 +00:00
),
'ga_event_tracking_enabled' => array (
2012-10-16 09:45:33 +00:00
'label' => __ ( 'Add event tracking code for add to cart actions' , 'woocommerce' ),
2012-07-29 20:39:28 +00:00
'type' => 'checkbox' ,
'checkboxgroup' => 'end' ,
'default' => 'no'
2012-04-30 19:50:35 +00:00
)
);
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
} // End init_form_fields()
2012-08-10 15:56:13 +00:00
2012-08-15 18:15:06 +00:00
2012-05-03 15:56:08 +00:00
/**
* Google Analytics standard tracking
2012-08-15 18:15:06 +00:00
*
* @ access public
* @ return void
*/
2012-05-03 15:56:08 +00:00
function google_tracking_code () {
global $woocommerce ;
2012-08-10 15:56:13 +00:00
2012-05-11 20:41:18 +00:00
if ( is_admin () || current_user_can ( 'manage_options' ) || $this -> ga_standard_tracking_enabled == " no " ) return ;
2012-08-10 15:56:13 +00:00
2012-05-11 20:41:18 +00:00
$tracking_id = $this -> ga_id ;
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
if ( ! $tracking_id ) return ;
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
$loggedin = ( is_user_logged_in () ) ? 'yes' : 'no' ;
if ( is_user_logged_in () ) {
$user_id = get_current_user_id ();
$current_user = get_user_by ( 'id' , $user_id );
$username = $current_user -> user_login ;
} else {
$user_id = '' ;
2012-10-16 09:45:33 +00:00
$username = __ ( 'Guest' , 'woocommerce' );
2012-04-30 19:50:35 +00:00
}
2012-08-10 15:56:13 +00:00
echo " <script type='text/javascript'>
2012-05-03 15:56:08 +00:00
var _gaq = _gaq || [];
_gaq . push (
2012-10-17 12:46:35 +00:00
[ '_setAccount' , '" . esc_js( $tracking_id ) . "' ],
2012-07-30 15:01:33 +00:00
[ '_setCustomVar' , 1 , 'logged-in' , '" . $loggedin . "' , 1 ],
[ '_setCustomVar' , 2 , 'user-id' , '" . $user_id . "' , 1 ],
[ '_setCustomVar' , 3 , 'username' , '" . $username . "' , 1 ],
2012-05-03 15:56:08 +00:00
[ '_trackPageview' ]
);
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
( 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 );
})();
2012-08-10 15:56:13 +00:00
</ script > " ;
2012-05-03 15:56:08 +00:00
}
2012-08-10 15:56:13 +00:00
2012-08-15 18:15:06 +00:00
2012-05-03 15:56:08 +00:00
/**
* Google Analytics eCommerce tracking
2012-08-15 18:15:06 +00:00
*
* @ access public
* @ param mixed $order_id
* @ return void
*/
2012-05-03 15:56:08 +00:00
function ecommerce_tracking_code ( $order_id ) {
global $woocommerce ;
2012-08-10 15:56:13 +00:00
2013-01-17 10:25:52 +00:00
if ( $this -> ga_ecommerce_tracking_enabled == " no " || current_user_can ( 'manage_options' ) || get_post_meta ( $order_id , '_ga_tracked' , true ) == 1 )
2012-08-10 15:56:13 +00:00
return ;
2012-05-11 20:41:18 +00:00
$tracking_id = $this -> ga_id ;
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
if ( ! $tracking_id ) return ;
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
// Doing eCommerce tracking so unhook standard tracking from the footer
2012-12-15 11:53:32 +00:00
remove_action ( 'wp_footer' , array ( $this , 'google_tracking_code' ) );
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
// Get the order and output tracking code
2012-08-10 15:56:13 +00:00
$order = new WC_Order ( $order_id );
$loggedin = is_user_logged_in () ? 'yes' : 'no' ;
if ( is_user_logged_in () ) {
2012-05-03 15:56:08 +00:00
$user_id = get_current_user_id ();
$current_user = get_user_by ( 'id' , $user_id );
$username = $current_user -> user_login ;
} else {
$user_id = '' ;
2012-10-16 09:45:33 +00:00
$username = __ ( 'Guest' , 'woocommerce' );
2012-05-03 15:56:08 +00:00
}
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
$code = "
2012-05-03 15:56:08 +00:00
var _gaq = _gaq || [];
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
_gaq . push (
2012-10-17 12:46:35 +00:00
[ '_setAccount' , '" . esc_js( $tracking_id ) . "' ],
[ '_setCustomVar' , 1 , 'logged-in' , '" . esc_js( $loggedin ) . "' , 1 ],
[ '_setCustomVar' , 2 , 'user-id' , '" . esc_js( $user_id ) . "' , 1 ],
[ '_setCustomVar' , 3 , 'username' , '" . esc_js( $username ) . "' , 1 ],
2012-05-03 15:56:08 +00:00
[ '_trackPageview' ]
);
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
_gaq . push ([ '_addTrans' ,
2012-10-17 12:46:35 +00:00
'" . esc_js( $order_id ) . "' , // order ID - required
'" . esc_js( get_bloginfo( ' name ' ) ) . "' , // affiliation or store name
'" . esc_js( $order->get_total() ) . "' , // total - required
'" . esc_js( $order->get_total_tax() ) . "' , // tax
'" . esc_js( $order->get_shipping() ) . "' , // shipping
'" . esc_js( $order->billing_city ) . "' , // city
'" . esc_js( $order->billing_state ) . "' , // state or province
'" . esc_js( $order->billing_country ) . "' // country
2012-05-03 15:56:08 +00:00
]);
2012-07-30 15:01:33 +00:00
" ;
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
// Order items
if ( $order -> get_items () ) {
foreach ( $order -> get_items () as $item ) {
$_product = $order -> get_product_from_item ( $item );
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
$code .= " _gaq.push(['_addItem', " ;
2012-10-17 12:46:35 +00:00
$code .= " ' " . esc_js ( $order_id ) . " ', " ;
$code .= " ' " . esc_js ( $_product -> get_sku () ? __ ( 'SKU:' , 'woocommerce' ) . ' ' . $_product -> get_sku () : $_product -> id ) . " ', " ;
$code .= " ' " . esc_js ( $item [ 'name' ] ) . " ', " ;
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
if ( isset ( $_product -> variation_data ) ) {
2012-08-10 15:56:13 +00:00
2012-10-17 12:46:35 +00:00
$code .= " ' " . esc_js ( woocommerce_get_formatted_variation ( $_product -> variation_data , true ) ) . " ', " ;
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
} else {
$out = array ();
$categories = get_the_terms ( $_product -> id , 'product_cat' );
if ( $categories ) {
foreach ( $categories as $category ){
$out [] = $category -> name ;
}
}
2012-10-17 12:46:35 +00:00
$code .= " ' " . esc_js ( join ( " / " , $out ) ) . " ', " ;
2012-07-30 15:01:33 +00:00
}
2012-10-17 12:46:35 +00:00
$code .= " ' " . esc_js ( $order -> get_item_total ( $item ) ) . " ', " ;
$code .= " ' " . esc_js ( $item [ 'qty' ] ) . " ' " ;
2012-07-30 15:01:33 +00:00
$code .= " ]); " ;
}
}
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
$code .= "
2012-05-03 15:56:08 +00:00
_gaq . push ([ '_trackTrans' ]); // submits transaction to the Analytics servers
2012-08-10 15:56:13 +00:00
2012-05-03 15:56:08 +00:00
( 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 );
})();
2012-07-30 15:01:33 +00:00
" ;
2012-08-10 15:56:13 +00:00
echo '<script type="text/javascript">' . $code . '</script>' ;
2013-01-17 10:25:52 +00:00
update_post_meta ( $order_id , '_ga_tracked' , 1 );
2012-05-03 15:56:08 +00:00
}
2012-08-10 15:56:13 +00:00
2012-08-15 18:15:06 +00:00
2012-07-29 20:39:28 +00:00
/**
* Google Analytics event tracking for single product add to cart
*
2012-08-15 18:15:06 +00:00
* @ access public
* @ return void
*/
2012-07-29 20:39:28 +00:00
function add_to_cart () {
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
if ( $this -> disable_tracking ( $this -> ga_event_tracking_enabled ) ) return ;
if ( ! is_single () ) return ;
2012-08-10 15:56:13 +00:00
2012-07-29 20:39:28 +00:00
global $product ;
2012-08-10 15:56:13 +00:00
2012-07-29 20:39:28 +00:00
$parameters = array ();
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
$parameters [ 'category' ] = " ' " . __ ( 'Products' , 'woocommerce' ) . " ' " ;
2012-12-30 13:48:27 +00:00
$parameters [ 'action' ] = " ' " . __ ( 'Add to cart' , 'woocommerce' ) . " ' " ;
2013-02-14 16:50:45 +00:00
$parameters [ 'label' ] = " ' " . esc_js ( $product -> get_sku () ? __ ( 'SKU:' , 'woocommerce' ) . ' ' . $product -> get_sku () : " # " . $product -> id ) . " ' " ;
2012-08-10 15:56:13 +00:00
2012-07-30 15:01:33 +00:00
$this -> event_tracking_code ( $parameters , '.single_add_to_cart_button' );
2012-07-29 20:39:28 +00:00
}
2012-08-10 15:56:13 +00:00
2012-08-15 18:15:06 +00:00
2012-07-29 20:39:28 +00:00
/**
* Google Analytics event tracking for loop add to cart
*
2012-08-15 18:15:06 +00:00
* @ access public
* @ return void
*/
2012-07-29 20:39:28 +00:00
function loop_add_to_cart () {
2012-11-27 16:22:47 +00:00
2012-07-30 15:01:33 +00:00
if ( $this -> disable_tracking ( $this -> ga_event_tracking_enabled ) ) return ;
2012-08-10 15:56:13 +00:00
2012-07-29 20:39:28 +00:00
$parameters = array ();
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
$parameters [ 'category' ] = " ' " . __ ( 'Products' , 'woocommerce' ) . " ' " ;
2012-07-30 15:01:33 +00:00
$parameters [ 'action' ] = " ' " . __ ( 'Add to Cart' , 'woocommerce' ) . " ' " ;
2013-02-14 16:50:45 +00:00
$parameters [ 'label' ] = " ( $ (this).data('product_sku')) ? ('SKU: ' + $ (this).data('product_sku')) : ('#' + $ (this).data('product_id')) " ; // Product SKU or ID
2012-08-10 15:56:13 +00:00
2012-11-08 12:11:46 +00:00
$this -> event_tracking_code ( $parameters , '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' );
2012-07-29 20:39:28 +00:00
}
2012-08-10 15:56:13 +00:00
2012-08-15 18:15:06 +00:00
2012-07-29 20:39:28 +00:00
/**
* Google Analytics event tracking for loop add to cart
2012-08-10 15:56:13 +00:00
*
2012-08-15 18:15:06 +00:00
* @ access private
* @ param mixed $parameters associative array of _trackEvent parameters
* @ param mixed $selector jQuery selector for binding click event
* @ return void
*/
2012-07-29 20:39:28 +00:00
private function event_tracking_code ( $parameters , $selector ) {
2012-07-30 15:01:33 +00:00
global $woocommerce ;
2012-08-10 15:56:13 +00:00
2012-07-29 20:39:28 +00:00
$parameters = apply_filters ( 'woocommerce_ga_event_tracking_parameters' , $parameters );
2012-11-27 16:22:47 +00:00
2012-07-30 15:01:33 +00:00
$woocommerce -> add_inline_js ( "
$ ( '" . $selector . "' ) . click ( function () {
" . sprintf( " _gaq . push ([ '_trackEvent' , % s , % s , % s ]); " , $parameters['category'] , $parameters['action'] , $parameters['label'] ) . "
2012-07-29 20:39:28 +00:00
});
2012-07-30 15:01:33 +00:00
" );
2012-07-29 20:39:28 +00:00
}
2012-08-10 15:56:13 +00:00
2012-08-15 18:15:06 +00:00
/**
* Check if tracking is disabled
*
* @ access private
* @ param mixed $type
* @ return bool
*/
2012-07-29 20:39:28 +00:00
private function disable_tracking ( $type ) {
2012-08-10 15:56:13 +00:00
2012-11-08 12:11:46 +00:00
if ( is_admin () || current_user_can ( 'manage_options' ) || ( ! $this -> ga_id ) || 'no' == $type ) return true ;
2012-08-10 15:56:13 +00:00
2012-07-29 20:39:28 +00:00
}
2012-08-10 15:56:13 +00:00
2012-04-30 19:50:35 +00:00
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
/**
2012-08-15 18:15:06 +00:00
* Add the integration to WooCommerce .
*
2012-08-15 18:30:35 +00:00
* @ package WooCommerce / Classes / Integrations
2012-08-15 18:15:06 +00:00
* @ access public
* @ param array $integrations
* @ return array
*/
2012-04-30 19:50:35 +00:00
function add_google_analytics_integration ( $integrations ) {
2012-08-15 18:15:06 +00:00
$integrations [] = 'WC_Google_Analytics' ;
return $integrations ;
2012-04-30 19:50:35 +00:00
}
2012-08-15 18:15:06 +00:00
2013-02-13 18:35:10 +00:00
add_filter ( 'woocommerce_integrations' , 'add_google_analytics_integration' );