Last active
August 29, 2015 14:24
-
-
Save badlydrawnben/e60b1fba8764677980a7 to your computer and use it in GitHub Desktop.
Gravity Forms - pass donation data to Google Analytics via query string (Genesis version)
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 | |
/* This is built upon @jg314's really useful gist here https://gist.github.com/jg314/7591169 I have updated it for Universal Analytics tracking | |
rather than the older ga.js version*/ | |
// Add this bit to your functions.php file | |
/* | |
* 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' ); | |
// Create a Genesis page template like page-123.php for whatever your confirmation page is, and add this code to it | |
// SINGLE DONATION THANKS PAGE | |
//Place the following directly into the page template used for your donation thank you page. | |
//Track donation through Google Analytics Ecommerce Tracking | |
add_action( 'genesis_after', 'biscuits_donation_ga' ); | |
function biscuits_donation_ga( $query ) { | |
$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"> | |
// console.log('amount <?php echo $donation_amount; ?>'); // Uncomment for testing purposes | |
ga('require', 'ecommerce'); | |
ga('ecommerce:addTransaction', { | |
'id': '<?php echo $donation_id; ?>', // Transaction ID. Required. | |
//'affiliation': 'Acme Clothing', // Affiliation or store name. | |
'revenue': '<?php echo $donation_amount; ?>', // Grand Total. | |
//'shipping': '5', // Shipping. | |
//'tax': '1.29' // Tax. | |
}); | |
ga('ecommerce:addItem', { | |
'id': '<?php echo $donation_id; ?>', // Transaction ID. Required. | |
'name': '<?php echo $donation_recurring; ?>', // Product name. Required. | |
'sku': '<?php echo $donation_recurring_sku; ?>',// SKU/code. | |
//'category': 'Party Toys', // Category or variation. | |
'price': '<?php echo $donation_amount; ?>', // Unit price. | |
'quantity': '1' // Quantity. | |
}); | |
ga('ecommerce:send'); | |
</script> | |
<?php endif; //End Google Analytics Ecommerce Tracking | |
} | |
genesis(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment