Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Google Analytics Ecommerce tracking to a donation form created through Gravity Forms.
<?php
/*
* Add custom query vars to handle Google Analytics Ecommerce tracking.
* This should be placed in a plugin or in functions.php of the theme.
*
* You'll want to use the redirect method for Gravity Forms confirmations and
* turn on passing field data via a query string. An example we used was:
* donation_amount={Donation Amount:13}&donation_id={entry_id}&donation_recurring=0
*/
function sm_add_query_vars( $vars ){
$vars[] = "donation_amount";
$vars[] = "donation_id";
$vars[] = "donation_recurring";
return $vars;
}
add_filter( 'query_vars', 'sm_add_query_vars' );
//Place the following directly into the page template used for your donation thank you page.
//Track donation through Google Analytics Ecommerce Tracking
$donation_amount = get_query_var( 'donation_amount' );
if( $donation_amount != '' ): //Only do Ecommerce if the donation amount is in query vars
//Donation Amount
$donation_amount = filter_var( $donation_amount, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
$donation_amount = number_format( $donation_amount, 2, '.', '' );
//Gravity Forms Transaction ID
$donation_id = get_query_var( 'donation_id' );
$donation_id = intval( $donation_id );
//Whether Donation is Recurring or One-Time
$donation_recurring = get_query_var( 'donation_recurring' );
$donation_recurring = intval( $donation_recurring );
$donation_recurring_sku = ( $donation_recurring == 1 ) ? 'RD' : 'OTD';
$donation_recurring = ( $donation_recurring == 1 ) ? 'Recurring Donation' : 'One-Time Donation';
?>
<script type="text/javascript">
_gaq.push(['_addTrans',
'<?php echo $donation_id; ?>', // transaction ID
'', // affiliation
'<?php echo $donation_amount; ?>', // total
'', // tax
'', // shipping
'', // city
'', // state or province
'' // country
]);
_gaq.push(['_addItem',
'<?php echo $donation_id; ?>', // transaction ID - necessary to associate item with transaction
'<?php echo $donation_recurring_sku; ?>', // SKU/code - required
'<?php echo $donation_recurring; ?>', // product name
'', // category or variation
'<?php echo $donation_amount; ?>', // unit price - required
'1' // quantity - required
]);
_gaq.push(['_trackTrans']);
</script>
<?php endif; //End Google Analytics Ecommerce Tracking ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.