-
-
Save logichub/7d76c55ccdbb2f2123370c6c9ba1d1dc to your computer and use it in GitHub Desktop.
Easy Digital Downloads - Google Conversion Tracking Code
This file contains hidden or 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
<?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