Skip to content

Instantly share code, notes, and snippets.

@miguelmich
Created November 30, 2014 00:14
Show Gist options
  • Save miguelmich/f8b58ac2818b50ceb749 to your computer and use it in GitHub Desktop.
Save miguelmich/f8b58ac2818b50ceb749 to your computer and use it in GitHub Desktop.
Easy Digital Downloads - Google Conversion Tracking Code
<?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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment