Fix piwik tracking. Closes #2845.

This commit is contained in:
Mike Jolley 2013-04-08 20:33:42 +01:00
parent b183fb2fb7
commit 6ef24149ac
2 changed files with 34 additions and 25 deletions

View File

@ -183,6 +183,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Fix - Fix IE Download via SSL bug and fix http file over SSL. * Fix - Fix IE Download via SSL bug and fix http file over SSL.
* Fix - Show non-existing product line items. * Fix - Show non-existing product line items.
* Fix - Conflicts with W3 Total Cache DB Cache * Fix - Conflicts with W3 Total Cache DB Cache
* Fix - piwik tracking.
* Tweak - Added a check to parent theme for comments_template before loading plugin template. * Tweak - Added a check to parent theme for comments_template before loading plugin template.
* Tweak - Remove hard coded max from random products widget. * Tweak - Remove hard coded max from random products widget.
* Tweak - Add filter hook to the place order button for easy 3rd party manipulation. * Tweak - Add filter hook to the place order button for easy 3rd party manipulation.
@ -194,7 +195,9 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Tweak - woocommerce_show_page_title filter * Tweak - woocommerce_show_page_title filter
* Tweak - wrapper / css tweaks for TwentyThirteen compatibility * Tweak - wrapper / css tweaks for TwentyThirteen compatibility
* Tweak - Added filters for controlling cross-sell display * Tweak - Added filters for controlling cross-sell display
* Tweak - Made hierarchy code in breadcrumbs more reliable.
* Localisation - NZ States * Localisation - NZ States
* Other minor fixes and localisation updates.
= 2.0.5 - 26/03/2013 = = 2.0.5 - 26/03/2013 =
* Tweak - Made no shipping available messages filterable via woocommerce_cart_no_shipping_available_html and woocommerce_no_shipping_available_html. * Tweak - Made no shipping available messages filterable via woocommerce_cart_no_shipping_available_html and woocommerce_no_shipping_available_html.

View File

@ -1208,47 +1208,53 @@ function woocommerce_readfile_chunked( $file, $retbytes = true ) {
function woocommerce_ecommerce_tracking_piwik( $order_id ) { function woocommerce_ecommerce_tracking_piwik( $order_id ) {
global $woocommerce; global $woocommerce;
if (is_admin()) return; // Don't track admin // Don't track admin
if ( current_user_can('manage_options') )
return;
// Call the Piwik ecommerce function if WP-Piwik is configured to add tracking codes to the page // Call the Piwik ecommerce function if WP-Piwik is configured to add tracking codes to the page
$wp_piwik_global_settings = get_option( 'wp-piwik_global-settings' ); $wp_piwik_global_settings = get_option( 'wp-piwik_global-settings' );
// Return if Piwik settings are not here, or if global is not set // Return if Piwik settings are not here, or if global is not set
if ( ! isset( $wp_piwik_global_settings['add_tracking_code'] ) || ! $wp_piwik_global_settings['add_tracking_code'] ) return; if ( ! isset( $wp_piwik_global_settings['add_tracking_code'] ) || ! $wp_piwik_global_settings['add_tracking_code'] )
if ( ! isset( $GLOBALS['wp_piwik'] ) ) return; return;
if ( ! isset( $GLOBALS['wp_piwik'] ) )
return;
// Remove WP-Piwik from wp_footer and run it here instead, to get Piwik // Get the order and get tracking code
// loaded *before* we do our ecommerce tracking calls
remove_action('wp_footer', array($GLOBALS['wp_piwik'],'footer'));
$GLOBALS['wp_piwik']->footer();
// Get the order and output tracking code
$order = new WC_Order( $order_id ); $order = new WC_Order( $order_id );
ob_start();
?> ?>
<script type="text/javascript">
try { try {
// Add order items // Add order items
<?php if ( $order->get_items() ) foreach( $order->get_items() as $item ) : $_product = $order->get_product_from_item( $item ); ?> <?php if ( $order->get_items() ) foreach( $order->get_items() as $item ) : $_product = $order->get_product_from_item( $item ); ?>
piwikTracker.addEcommerceItem( piwikTracker.addEcommerceItem(
"<?php echo esc_js( $_product->sku ); ?>", // (required) SKU: Product unique identifier "<?php echo esc_js( $_product->get_sku() ); ?>", // (required) SKU: Product unique identifier
"<?php echo esc_js( $item['name'] ); ?>", // (optional) Product name "<?php echo esc_js( $item['name'] ); ?>", // (optional) Product name
"<?php if (isset($_product->variation_data)) echo esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ); ?>", // (optional) Product category. You can also specify an array of up to 5 categories eg. ["Books", "New releases", "Biography"] "<?php
if ( isset( $_product->variation_data ) )
echo esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) );
?>", // (optional) Product category. You can also specify an array of up to 5 categories eg. ["Books", "New releases", "Biography"]
<?php echo esc_js( $order->get_item_total( $item ) ); ?>, // (recommended) Product price <?php echo esc_js( $order->get_item_total( $item ) ); ?>, // (recommended) Product price
<?php echo esc_js( $item['qty'] ); ?> // (optional, default to 1) Product quantity <?php echo esc_js( $item['qty'] ); ?> // (optional, default to 1) Product quantity
); );
<?php endforeach; ?> <?php endforeach; ?>
// Track order // Track order
piwikTracker.trackEcommerceOrder( piwikTracker.trackEcommerceOrder(
"<?php echo esc_js( $order_id ); ?>", // (required) Unique Order ID "<?php echo esc_js( $order->get_order_number() ); ?>", // (required) Unique Order ID
<?php echo esc_js( $order->order_total ); ?>, // (required) Order Revenue grand total (includes tax, shipping, and subtracted discount) <?php echo esc_js( $order->get_total() ); ?>, // (required) Order Revenue grand total (includes tax, shipping, and subtracted discount)
false, // (optional) Order sub total (excludes shipping) false, // (optional) Order sub total (excludes shipping)
<?php echo esc_js( $order->get_total_tax() ); ?>, // (optional) Tax amount <?php echo esc_js( $order->get_total_tax() ); ?>, // (optional) Tax amount
<?php echo esc_js( $order->get_shipping() ); ?>, // (optional) Shipping amount <?php echo esc_js( $order->get_shipping() ); ?>, // (optional) Shipping amount
false // (optional) Discount offered (set to false for unspecified parameter) false // (optional) Discount offered (set to false for unspecified parameter)
); );
} catch( err ) {} } catch( err ) {}
</script>
<?php <?php
$code = ob_get_clean();
$woocommerce->add_inline_js( $code );
} }