If you were following part one when we built a donations form using Gravity Forms and Paypal, then you’re hopefully up and running and have tested it out on your site. The next step is to hook it up to your Google Analytics account so you can start tracking where all these donors are coming from. As the saying goes, ‘you can’t manage what you don’t measure’.
There’s a few different techniques we could use, but here we’ll be making use of the query strings that Gravity Forms can append to the url of the confirmation page. The advantage of this technique is that it’s fairly agnostic, so it can easily be applied to other form plugins and other payment processors.
How it works:
We will tell Gravity Forms to append a query string to the ‘confirmation page’ that the donor is redirected to upon successful completion of their transaction. The query string contains some data from the donation. In the case of my test form, you can see that it is including the donation amount, a unique transaction ID, and a binary field that indicates whether it’s a single or regular donation:
There’s some code on the site that is looking for those query strings, and assigning them to a PHP variable. Then there’s some code on the confirmation page that is checking to see if the donation_amount variable is present. If it is, then it fires off the standard Google Analytics eCommerce code which ‘pushes’ the transaction variables through to your Google Analytics account.
Attribution: Much of the code we’ll be using was created by Jonathan Goldford on Github. I’ve updated it for the newer Google Universal Analytics code and fleshed out the instructions for getting it to work.
Step by step:
1. Make sure we’re sending a ‘clean’ donation amount to Google Analytics
The Google Analytics tracking script needs to see just a number for the transaction value – e.g. 50 or 24.99 as the currency is already fixed in your Google Analytics. But the Gravity forms Paypal ‘Form Total’ (that we’re obliged to use) will only send the amount and currency together – e.g. £50 or $24.99. When that gets added into the query string it will have the strange unicode encoding, or it will include the £ sign – which tends to break things pretty quickly!
You can try and use some clever regex code to strip out the currency symbol, but I found it easier to create a ‘clean’ hidden number field in Gravity Forms (mine is called ‘Analytics Friendly donation amount’). It has a calculation inbuilt so it is set to automatically be the same as the ‘Amount’ value. The default Amount value (e.g £24.99) is sent to Paypal and we’re using the ‘clean’ value (i.e. 24.99) to send in the query string to Google Analytics.
2. Add the query string to the Gravity Forms confirmation page.
In the previous tutorial we said we’d ignore the query string bit for now. Let’s have a closer look at it here. In the confirmation section where you’re designating which page on your site you want the form to redirect to, you need to click on the ‘Redirect Query String’ checkbox and tick the little box on the right hand side to select which field values you want to include in the query string.
If you’re following along with the code in this tutorial then you should make sure that you are sending at least these three query string variables: donation_amount, donation_id and donation_recurring.
3. Make sure you have Google Analytics tracking set up for your site.
You probably already have a plugin that does this. Just make sure it’s using the modern ‘Universal Analytics’ tracking code.
4. Enable eCommerce tracking in your Analytics reports.
Here’s how. Whilst you’re there, make sure that your account is set to be displaying the right currency.
5. Add some code to your functions.php file which will look for those query strings and set them as variables
(see the embedded Gist below)
6. Add some code in the template for your confirmation page which will look for the query string variables, and send them to Google Analytics when the page loads.
There’s some eCommerce variables that Google Analytics requires, and some which are optional. You can find out more on the Google developers site.
(see the embedded Gist below)
Hopefully this is all you’ll need to get you on your way. The comments in the code should be fairly self-explanatory but give me a shout if not.
Note: the page template code is specifically for people using the WordPress Genesis framework. If that’s not you, then you’ll need to modify it as the ‘genesis_after’ hook is unique to Genesis.
7. Log in to your Analytics Dashboard and go to Conversions>Ecommerce to check everything is working.
There’s generally a delay of up to 1 hour until the data shows up in ‘today’s’ reports, so be patient before assuming that it’s not working properly.
8. Get into the habit of using campaign builder url strings when you’re promoting your site.
They’re the easiest way of identifying exactly where your visitors are coming from. With this new e-commerce tracking you’ve just set up, you’ll be able to connect that up to the data to see which campaigns/channels are driving the most and the highest value average donations.
The data that shows in your Analytics reports is not likely to match perfectly with your own finance reports. Occasionally some transactions don’t get picked up, and if the user refreshes the confirmation page then the Analytics might count it twice. However, now that you’re measuring your donor journeys much better, you’re in a much better position to start managing your online fundraising much better too.