Last active
June 28, 2017 06:00
-
-
Save mahdi-alavi/6bcb6a24805107f2d127fea083050dea to your computer and use it in GitHub Desktop.
Piwik & WooCommerce - tracking code and woocommerce order tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in line 61 insert your piwik url | |
// source code: wordpress.org/plugins/woocommerce-piwik-integration/ | |
/* Get encoded categories by product. | |
=========================================================================== */ | |
function getEncodedCategoriesByProduct( $product ) { | |
$categories = get_the_terms( $product->get_id(), 'product_cat' ); | |
if ( ! $categories ) { | |
$categories = array(); | |
} | |
$categories = array_map( function ( $element ) { | |
return sprintf( "'%s'", esc_attr( $element->name ) ); | |
}, $categories ); | |
return sprintf( "[%s]", implode( ", ", $categories ) ); | |
} | |
/* Piwik tracking code. | |
=========================================================================== */ | |
function itl_piwik_tracking_code() { | |
global $wp_query, $product; | |
$code = ""; | |
$code .= " | |
var _paq = _paq || [];"; | |
if ( is_product() ) { | |
$code .= sprintf(" | |
_paq.push(['setEcommerceView', | |
'%s', | |
'%s', | |
%s, | |
%f | |
]);", $product->get_sku(), esc_attr( $product->get_title() ), getEncodedCategoriesByProduct( $product ), $product->get_price() . '0' ); | |
} | |
elseif ( is_product_category() && isset( $wp_query->query_vars['product_cat'] ) && ! empty( $wp_query->query_vars['product_cat'] ) ) { | |
$code .= sprintf(" | |
_paq.push(['setEcommerceView', | |
false, | |
false, | |
'%s' | |
]);", esc_attr( $wp_query->queried_object->name ) ); | |
} | |
if ( is_user_logged_in() ) { | |
$code .= sprintf(" | |
_paq.push(['setUserId', '%s']);", get_current_user_id() ); | |
} | |
$code .= " | |
_paq.push(['trackPageView']);"; | |
$code .= " | |
_paq.push(['enableLinkTracking']);"; | |
$code .= " | |
_paq.push(['enableHeartBeatTimer', 30]);"; | |
$code .= " | |
(function() { | |
var u='//piwik.test.com/'; | |
_paq.push(['setTrackerUrl', u+'piwik.php']); | |
_paq.push(['setSiteId', '1']); | |
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; | |
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); | |
})(); | |
"; | |
echo '<script type="text/javascript">' . $code . '</script>'; | |
} | |
add_action( 'wp_footer', 'itl_piwik_tracking_code' ); | |
/* Piwik woocommerce order tracking. | |
=========================================================================== */ | |
function itl_ecommerce_tracking_code( $order_id ) { | |
if ( get_post_meta( $order_id, '_piwik_tracked', true ) == 1 ) { | |
return; | |
} | |
$order = new WC_Order( $order_id ); | |
$code = ' | |
var _paq = _paq || []; | |
'; | |
if ( $order->get_items() ) { | |
foreach ( $order->get_items() as $item ) { | |
$_product = $order->get_product_from_item( $item ); | |
$code .= ' | |
_paq.push(["addEcommerceItem", | |
"' . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->id ) . '", | |
"' . esc_js( $item['name'] ) . '",'; | |
$out = array(); | |
$categories = get_the_terms( $_product->id, 'product_cat' ); | |
if ( $categories ) { | |
foreach ( $categories as $category ) { | |
$out[] = $category->name; | |
} | |
} | |
if ( count( $out ) > 0 ) { | |
$code .= '["' . join( "\", \"", $out ) . '"],'; | |
} else { | |
$code .= '[],'; | |
} | |
$code .= '"' . esc_js( $order->get_item_total( $item ) ) . '",'; | |
$code .= '"' . esc_js( $item['qty'] ) . '"'; | |
$code .= "]);"; | |
} | |
} | |
$code .= ' | |
_paq.push(["trackEcommerceOrder", | |
"' . esc_js( $order->get_order_number() ) . '", | |
"' . esc_js( $order->get_total() ) . '", | |
"' . esc_js( $order->get_total() - $order->get_total_shipping() ) . '", | |
"' . esc_js( $order->get_total_tax() ) . '", | |
"' . esc_js( $order->get_total_shipping() ) . '" | |
]); | |
'; | |
echo '<script type="text/javascript">' . $code . '</script>'; | |
update_post_meta( $order_id, '_piwik_tracked', 1 ); | |
} | |
add_action( 'woocommerce_thankyou', 'itl_ecommerce_tracking_code' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment