Tracking PayPal with Google Analytics and Universal Analytics

Many of us use PayPal to accept online payments – it’s quick and easy to setup and lots of people have PayPal accounts, so it’s a great way to collect funds. But the biggest problem for people using Google Analytics is that you can’t place your own Google Analytics Tracking Code (or any code or that matter) on PayPal’s pages. This can make measuring conversions and values tricky if you want to see accurate data inside your Google Analytics reports … until now! The issue that happens with PayPal is that people start the process on your website, but then complete the payment on PayPal. Some people will return to your website to see your thank you page, but others will close their browser before they return. This has meant there are two choices in how to measure conversions: we can measure conversions before sending people to PayPal (which results in inflated conversion figures); or we can measure them when they return, (which means underestimated conversion figures).

paypal-google-analytics1.gif

Accurately Measuring PayPal Transactions

The best way to measure PayPal transactions into Google Analytics is to make use of PayPal’s “Instant Payment Notification” (or IPN) service which automatically sends you details about each transaction. The problem is that for most of us, it has been way too complicated to get this setup, as a custom system was required to accept the transaction details.

The other problem is how do we get the transaction details into Google Analytics? Now with Universal Analytics and the Measurement Protocol, it is really easy to get data into Google Analytics. So using the IPN service and the Measurement Protocol we can get PayPal talking to Google Analytics which allows us to get accurate transaction details.

Using Zapier

Zapier is a neat service that allows you to connect different products together. In this post we are going to use it to connect PayPal and Google Analytics, as it makes it really quick to start getting accurate data.  If you’re more technically inclined, or you don’t want to create a Zapier account, you could create your own mechanism for accepting the IPN details and submitting them to Google Analytics.

Before you start, you should create a new Google Analytics testing property, as you don’t want to be sending data into your primary property and views if things aren’t already working 100% correctly. You should also use PayPal’s sandbox to test the transactions before going live. This will save you having to spend your own money when testing.

We will now step through creating the Zap to send data to Google Analytics, you can follow along and also get the preconfigured Zap at http://zpr.io/Gc4q

1. Create your Zap

Start by creating a new Zap for PayPal when there is a successful sale and send this to a Web Hook. You will need to select ‘Custom Request’ for the Web Hook (you will find this option under ‘Show all actions’).

zapier-paypal-setup1

2. Add the Web Hook to PayPal

Now Zapier will give you a URL that will accept the details coming from the PayPal IPN. You can either add this to your PayPal account so that all transactions are sent to Zapier, or you can just add this to a specific buy now button you have created.

<script async="async" src="https://www.paypalobjects.com/js/external/paypal-button.min.js?merchant=you@company.com" data-button="buynow" data-name="My product" data-amount="1.00" data-callback="https://zapier.com/hooks/paypal/?cid=123456"> </script>

3. Add a Custom Filter

We’re going to add a custom filter inside our Zap to ensure that there is a value set for PayPal’s custom value, before we sending anything to Google Analytics. (We will discuss why we need this at the end of the post under ‘how this works’.)

zapier-paypal-filter1.gif

4. Construct the Google Analytics Hit

Now we need to create the hit that we send to Google Analytics using the Measurement Protocol. There are two important things to be aware of. Firstly, you will need to change UA-123456-1 to your own Google Analytics Property ID as this is where the data will be sent. Secondly, you will notice that there is an incorrectly formatted query parameter of cidCustom, this isn’t actually a mistake, so make sure it looks exactly like that in your Zap configuration (even though logic would tell you to use cid=Custom, this will not work).

zapier-paypal-analytics-hit11.gif

The reason that we don’t include the equals sign in the Zap is because there is a bug that causes the custom value to be truncated if it contains a period. Since we will be passing a value with a period, this is a bit of a hack to work around the bug in Zappier.

5. Configure Google Analytics

You will need to switch to Enhanced Ecommerce inside your Google Analytics account. To do this head to the ‘Admin’ section and click on ‘Ecommerce Settings’ under your view.

Screen Shot 2014-11-06 at 4.17.17 pm

6. Check and Modify your Code

You will need to ensure you are using the latest version of the Google Analytics Tracking Code, which should look something like:

<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-123456-1', 'company.com'); ga('send', 'pageview'); </script>

You will then need to add the following code to all the pages where you include PayPal buttons.

<script type="text/javascript"> function _uGC(l,n,s) { if (!l || l=="" || !n || n=="" || !s || s=="") return "-"; var i,i2,i3,c="-"; i=l.indexOf(n); i3=n.indexOf("=")+1; if (i > -1) { i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; } c=l.substring((i+i3),i2); } return c; } var uaGaCkVal= _uGC(document.cookie, '_ga=', ';'); var uaGaCkValArray= uaGaCkVal.split('.'); var uaUIDVal=""; var gacid=""; if(uaGaCkValArray.length==4) { uaUIDVal=uaGaCkValArray[2] + "." + uaGaCkValArray[3]; gacid='=' + uaUIDVal.replace(/%2F/g,"-"); } else { uaUIDVal=""; gacid=""; } var myNewValue = gacid; </script>

This code basically grabs details from the Google Analytics cookie on a person’s browser and then adds this to the PayPal button. If you are using Google Tag Manger, you can put this into a custom HTML container.

Finally, you need to modify your PayPal buy now buttons so they include the custom data input. In our example below we are also adding the IPN details directly into the button code and we are also using the sandbox mode so we can test things out before adding them to our live website:

<script async="async" src="https://www.paypalobjects.com/js/external/paypal-button.min.js?merchant=you@company.com" data-button="buynow" data-name="My product" data-amount="1.00" data-custom=unknown data-env="sandbox" data-callback="https://zapier.com/hooks/paypal/?cid=123456"> </script>

You will need to change the URL for ‘data-callback’ to the URL provided in Zapier, you will find this under the second step when you create the Zap. Remember to remove data-env=”sandbox” when you add this to your live website.

7. Test and Save

From here you will want to test your Zap by completing a PayPal transaction and then sending it to Google Analytics. To make 100% sure it works you should load your webpage that includes your PayPal button using Google Analytics Campaign Tags (UTM tags). If the test works correctly, then your custom campaign tags (e.g. source and medium) will display inside Google Analytics when you load the page and they should also be displayed when your transaction details are sent from Zapier.

PayPal Data within Google Analytics

Now if everything has worked correctly you will see data inside the sales report:

paypal-in-google-analytics

How it Works

The custom code we added to our page takes the Client ID (CID) value from the Google Analytics cookie and sends this to PayPal. Then we grab the Client ID from within Zapier and send this along with the transaction details to Google Analytics. What this means is that Google Analytics sees the transaction, as if it had occurred on our website without the person ever leaving.

Bonus Tips

Tip 1: Make sure you create a thank you page on your website and direct people to this page after they have successfully completed a transaction. This allows you to promote other items or content after people have completed their transaction.

Tip 2: Zapier will create a pageview for /PayPal/Completed for each successful transaction. You should definitely create a goal that includes the page (or pages) where your buttons are (e.g. /BuyNow) as the first step in a goal funnel and then /PayPal/Completed as your goal URL. This will allow you to easily see the percentage of people making it through those two steps in the process.

Questions? I bet you have a question, so hit us up in the comments!