<?php
//Google Analytics E-commerce Tracking code
function your_prefix_google_ecommerce_tracking_code() {
$success_page = edd_get_option( 'success_page' ) ? is_page( edd_get_option( 'success_page' ) ) : false;
if ( ! $success_page || !edd_is_success_page() ){
return;
}
$session = edd_get_purchase_session();
if ( isset( $_GET[ 'payment_key' ] ) ) {
$payment_key = urldecode( $_GET[ 'payment_key' ] );
} elseif ( $edd_receipt_args['payment_key'] ) {
$payment_key = $edd_receipt_args['payment_key'];
} else if ( $session ) {
$payment_key = $session[ 'purchase_key' ];
}
if ( ! isset( $payment_key ) ){
return;
}
$purchase_key = $session['purchase_key'];
$price = $session['price'];
$cart_items = $session['cart_details'];
?>
<script>
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': '<?php echo $purchase_key; ?>',
'affiliation': 'Web Purchase',
'revenue': '<?php echo $price; ?>',
});
<?php
foreach ($cart_items as $key => $product) {
echo "
ga('ecommerce:addItem', {
'id': '" . $product["id"] . "',
'name': '" . $product["name"] . "',
'sku': '" . edd_get_download_sku( $product["id"] ) . "',
'price': '" . $product["item_price"] . "',
'quantity': '" . $product["quantity"] . "',
'currency': 'USD' // local currency code.
});
";
}
?>
ga('ecommerce:send');
</script>
<?php
}
add_action('wp_footer','your_prefix_google_ecommerce_tracking_code');
0 comments