How to track PayPal transactions with Google Analytics

How can I track PayPal in Google Analytics?

The best way to track PayPal transactions into Google Analytics is by sending details about the individual user (using their Client ID) to PayPal and then sending confirmed sales back into Google Analytics. This allows us to see transactions, along with their acquisition channels and other behavior inside our reports.

Let’s get started…

PayPal is a great way to accept payments online. It’s fast and easy, but if you’re using Google Analytics you’ll quickly discover that you can’t just copy and paste your tracking code into your PayPal account.

So what can we do?

We’ll you could move to a custom payment solution where you keep people on your website (and don’t send them to PayPal). But this can take time and more importantly, lots of people like the option of paying using their existing PayPal account. It’s familiar and it keeps their payment details safe and secure.

This means we need to find a way to get PayPal to talk to Google Analytics. We need PayPal to tell us when people purchase and the total value of the transaction, once we have these details we then need to tie this to the appropriate session and user inside our reports.

There are simpler solutions, for example you could track people who return to your confirmation page after completing their payment. However, this means we will be under-reporting the total number of conversions since some people won’t make it back to our website after a successful transaction. Alternatively, we could count conversions when people click through to PayPal, but then we will be over-reporting the number of conversions.

Tracking PayPal with Google Analytics

These options aren’t good. They will give us inaccurate conversion data, we won’t be reporting on revenue and more importantly, we’ll never be able to tie actual dollar value back to our marketing channels.

Enter PayPal’s Instant Payment Notification (or IPN) which we can use to automatically send details about each transaction to other systems and platforms. We’ll need a way to connect the two platforms. So what do we do?

Say hello to Zapier! (Hello Zapier!)

Zapier is the solution we’ll use to get PayPal talking to Google Analytics. Zapier is awesome. It’s a bit like glue, we can use it to stick two things together and make them work. You can use it to integrate hundreds of apps and platforms.

Here’s an overview of how we’ll track PayPal into Google Analytics…

Someone comes to our website, then they decide to purchase, so they click on the PayPal button. When this happens we’ll use some custom code to grab some details from the Google Analytics cookie and send it as a hidden field to PayPal.

When the person has successfully completed payment on PayPal we’ll use the PayPal IPN to send their transaction details to Zapier along with the details from the cookie.

Zapier will receive all of the information from PayPal and format this into a valid hit. We’ll be using the Google Analytics Measurement Protocol. Since this hit will include details from the person’s original session on our website it means we can view their original acquisition channel and previous behavior. This allows us to understand the performance of our website and marketing campaigns against revenue coming from PayPal.

Voila!

That’s how we track PayPal transactions into Google Analytics! Now it’s time to walk through how we configure this using PayPal, Zapier and Google Tag Manager.

Steps for tracking PayPal with Google Analytics:

  1. Get your tracking ID
  2. Create a Zap in Zapier
  3. Add the webhook to PayPal
  4. Add a custom filter to the Zap
  5. Configure the Zap to send data
  6. Enable the ecommerce reports in Google Analytics
  7. Configure Google Tag Manager
  8. Complete a test transaction
  9. Go live

And the great news is that if you’re already using Google Tag Manager, then the only code you’ll need to change is the PayPal button on your website. 

And here’s a preview of the solution:

Let’s jump in!

Step 1: Get your tracking ID

I recommend creating a test property inside Google Analytics. This will allow you to set everything up, test it out and make sure it’s working before applying the solution to your live website and primary Google Analytics reports.

Here’s what you’ll need to do:

  1. Head to the admin section of Google Analytics.
  2. Look for the ‘Account’ column on the left and click on the drop-down.
  3. Click ‘Create New Account’.
  4. Name the account and website (I suggest ‘Testing’ or something similar), enter ‘www.example.com’ as the URL (this won’t impact data being collected), select the other settings and click ‘Get Tracking ID’.
  5. You’ll then be given the tracking code for your test property.

I suggest creating a single HTML page for testing the PayPal tracking solution. (I have my own website I use for this. I just upload the file to a hidden folder and I can play with the code until I get things working.)

I recommend using the PayPal Sandbox for testing transactions. This will allow you to complete transactions without having to make actual payments. I’ve found PayPal’s Sandbox confusing in the past, so I suggest clicking the ‘Log into Dashboard’ button. You’ll need to login with your PayPal account, then you’ll see special email addresses you can use for testing. You can use these when you’re configuring your ‘Buy Now’ button for testing.

You’ll also need a Zapier account on a Premium Plan. Plans start at $20 per month, but you can start with their 14 day free trial to get started and test things out. I know paying for yet another subscription might not sound appealing, but Zapier is amazing. I use it for so many things. I find it easily pays for itself by saving me so much time.

Step 2: Create a Zap in Zapier

Start by creating a new Zap for PayPal for a successful sale.

You’ll be now be given a webhook URL that we’ll need to copy and use in PayPal when we configure our IPN.

Next we need to head to the PayPal Sandbox and use the IPN Simulator (you’ll need to log into your account to use it). We can now send test details through to Zapier.

You’ll need to enter the webhook URL we just copied into the ‘IPN Handler URL’, select ‘Express Checkout’ as the ‘Transaction Type’ and then scroll to the bottom of the page and click ‘Send IPN’.

We’ll finish setting up our Zap in a moment.

Step 3: Add the webhook to PayPal

Now we need to get PayPal to send details through to the Zapier webhook. There are two ways you can do this. The first option is to add the webhook inside your PayPal account. This will mean that all of your successful transaction details are sent through to Zapier.

You’ll need a business account with PayPal to do this and then you’ll need to head to ‘Selling Tools’ and look for ‘Instant Payment Notifications’ (in the Sandbox it’s hard to find so just head over here). Here you’ll be able to enter the webhook URL we copied during the second step (see above).

Another option is to simply set the webhook URL in the individual ‘Buy Now’ button. To do this you’ll need to add the ‘data-callback’. Here’s an example…

<script async="async" src="https://www.paypalobjects.com/js/external/paypal-button.min.js?merchant=hello-merchant@lovesdata.com"
        data-button="buynow" 
        data-name="My Test Product"
        data-amount="5.00"
        data-custom-editable=unknown
        data-callback="https://hooks.zapier.com/hooks/paypal/?cid=12345"
        data-env="sandbox"
></script>

Important points for you:

  1. You’ll see I have ‘data-custom-editable’ in the script. This allows us to see the value as we’re testing out our PayPal tracking. When you’re ready to use this solution on your live website, then change this to ‘data-custom’ so that it’s hidden.
  2. Make sure you change the email address. In my example this is ‘hello-merchant@lovesdata.com’ but this should be your sandbox address when testing (and your real address when it’s live on your website).
  3. Make sure you change the callback URL. This should be the URL provided to you by Zapier.
  4. And finally, if you are interested you can find more details about the PayPal payment button we’re using for this solution.

Step 4: Add a custom filter to the Zap

Now we need to head back to Zapier and add a custom filter to our Zap. To do this click on the plus sign on the left and select ‘Filter’.

We’re going to add a filter that checks to make sure there is a value set for ‘custom’ (and that the value is not set to ‘unknown’) before we send data to Google Analytics. Here’s the filter…

Important: If you didn’t send the test using PayPal’s IPN Simulator from the second step, then you won’t see ‘Custom’ as an option within your filter. You’ll need to head back to Step 2 and trigger the test notification to Zapier.

Step 5: Configure the Zap to send data

Now we need to create the hit that will send data to Google Analytics. This uses the Measurement Protocol which means we’ll be sending data directly into our reports (without the need for any tracking code).

To do this we now need to select ‘Webhooks by Zapier’ as the action for our Zap.

And then we choose ‘Custom Request’ as the ‘Action’. Then we need to select ‘Post’ as the ‘Method’, enter ‘http://www.google-analytics.com/collect’ as the URL and then you’ll need to construct the parameters that are sent to Google Analytics in the ‘Data’ field. They’ll need it to look like this...

You’ll need to use the plus icon on the top right corner of the input to search for and select the fields that have been sent from PayPal.

Important: Make sure to change ‘UA-XXXXX-Y’ to your own Google Analytics Tracking ID.

Here’s an example of the data we’ll be sending using the Measurement Protocol:

v=1                         // Version
&tid=UA-XXXXX-Y             // Tracking ID
&cid=555                    // Client ID
&t=pageview                 // Hit type
&dh=mydemo.com              // Hostname
&dp=/PayPal/Completed       // Page
&dt=PayPal                  // Title
&ti=12345                   // Transaction ID
&ta=PayPal                  // Affiliation
&tr=5.00                    // Revenue
&tt=0.50                    // Tax
&ts=0.00                    // Shipping
&cu=USD                     // Currency
&pa=purchase                // Product action
&pr1nm=My%20Test%20Product  // Product name

Step 6: Enable the ecommerce reports in Google Analytics

Next we need to enable the ecommerce reports inside Google Analytics. To do this we need to head to the admin section and select ‘Ecommerce Settings’ for the reporting view.

Tracking PayPal with Google Analytics

You’ll need to set ‘Enable Ecommerce’ and ‘Enhanced Ecommerce’ to ‘On’.

Step 7: Configure Google Tag Manager

To get started quickly you can import our pre-configured container into Google Tag Manager. After you’ve downloaded the Tag Manager container file you can navigate to your new (or existing) container inside Tag Manager, then select ‘Admin’ and choose the ‘Import Container’ option.

If you’d like to configure Google Tag Manager yourself here are the steps:

We start by creating two user-defined variables. Navigate to ‘Variables’ and then select ‘New’. We’ll name our first variable ‘Google Analytics Cookie’ and we will use this to capture the complete Google Analytics cookie (_ga) from the user’s browser. To do this select ‘1st Party Cookie’ and enter ‘_ga’ as the ‘Cookie Name’.

Then we create another variable. This time we select ‘Custom JavaScript’ as the type and enter the following code: 

function(){
 return {{Google Analytics Cookie}}.match(/\d+\.\d+$/g)[0];
} 

This grabs the Client ID from the Google Analytics cookie so we can send it through to PayPal.

Next we navigate to ‘Triggers’ and create a new trigger using ‘Window Loaded’ as the type.

Now we head to ‘Tags’ and create a new tag called ‘Add Client ID to PayPal Button’ and select ‘Custom HTML’ as the tag type. We then enter the following code for the tag:

<script>
try{
  document.getElementsByName('custom')[0].value = {{Google Analytics Client ID}};
}catch(e){}
</script>

This passes the value of the Client ID (using the variable we just created) and adds this to the PayPal button on our website.

We then select the trigger we just created (called ‘All Pages - Window Loaded’) to fire the tag.

Finally, we need to make sure we have a Google Analytics tag added in the container.

And that’s it. We can publish our container!

Step 8: Complete a test transaction

Now it’s time to test everything out. So refresh the page with your PayPal button and complete a transaction using the PayPal Sandbox. When I was testing this solution I encountered two issues at PayPal’s end – the Sandbox was offline for a period of time and the Sandbox IPN was delayed, so if you encounter these issues, don’t panic, you’ll just need to wait until PayPal has fixed the issues at their end.

You can head to the PayPal Sandbox (at https://www.sandbox.paypal.com) and login using your Sandbox account. Then navigate to ‘History’ and select ‘IPN History’ and select your test transaction. You should see the Client ID contained in the ‘Message ID’.

And you should be able to see the transaction inside Google Analytics.

I recommend navigating to your test page and adding your own UTM tags (campaign tags) so that you can ensure the transaction is correctly tied to the original session on your website. For example, if my page is http://www.demo.com/paypal then I can use http://www.demo.com/paypal?utm_source=ben1&utm_medium=ben1 so I can easily see that it’s me testing.

Step 9: Go live

When you’re ready to go live with the solution on your website make sure you modify your PayPal button. Here’s what we used for our test...

<script async="async" src="https://www.paypalobjects.com/js/external/paypal-button.min.js?merchant=hello-merchant@lovesdata.com"
        data-button="buynow" 
        data-name="My Test Product"
        data-amount="5.00"
        data-custom-editable=unknown
        data-callback="https://hooks.zapier.com/hooks/paypal/?cid=12345"
        data-env="sandbox"
></script>

So in this case I need to change the merchant email address, change ‘data-custom-editable’ to ‘data-custom’ and remove ‘data-env=”sandbox”’.

How it all works

Now we’ve pulled everything together! We’re using Google Tag Manager to capture the Client ID from the Google Analytics cookie and since this is what’s used to tie session data together in our reports (so that we’re reporting on users) we can now tie our PayPal transactions to the appropriate Client ID (and therefore user).

This means we can make use of the acquisition reports and even the multi-channel funnel reports to understand the value we’re generating from PayPal on our website.

Although there are other options for tracking PayPal into Google Analytics, like just tracking the number of clicks on our PayPal button, using Zapier, the PayPal IPN and Google Tag Manager is going to provide the most accurate data possible inside our reports.

Now it’s your turn to track PayPal with Google Analytics!

Need help? Then let us know in the comments or contact my team. We’re here to help!