Online payment platforms such as PayPal are a common choice for businesses that sell products or services online. Tracking transactions and values on these platforms is essential to understand the effectiveness of your marketing efforts and optimize your sales strategy. In this blog post, we will guide you through how to track PayPal transactions and values with Google Analytics.
Introduction to PayPal Transactions and Google Analytics
Google Analytics is a powerful tool that provides valuable insights into website traffic and user behavior. With Google Analytics, you can track various website metrics, including transaction values, revenue, and conversion rates. However, tracking PayPal transactions and values requires additional setup.
Setting up PayPal Transactions in Google Analytics
To track PayPal transactions in Google Analytics, you need to set up e-commerce tracking. E-commerce tracking allows you to track transaction-related data, such as transaction ID, product name, quantity, and revenue.
To set up e-commerce tracking in Google Analytics, go to the “Admin” section of your Google Analytics account and select “E-commerce Settings” under the “View” column. Enable e-commerce tracking and save the changes.
Setting up PayPal IPN
After enabling e-commerce tracking, you need to set up PayPal Instant Payment Notification (IPN). IPN is a notification service that PayPal uses to notify your website of payment transactions. IPN enables your website to automatically update the transaction status and send confirmation emails to customers.
To set up PayPal IPN, go to your PayPal account and navigate to the “Instant Payment Notification (IPN)” section under the “Selling Tools” menu. Enable IPN and enter the notification URL, which is the URL of your website’s IPN handler script.
Creating a Thank You Page
After setting up PayPal IPN, create a custom “Thank You” page on your website to track the transaction data. The “Thank You” page should be displayed to customers after they complete a purchase on your website.
On the “Thank You” page, add the Google Analytics tracking code to track the transaction data. Additionally, add the following code to extract the PayPal transaction data:
<?php
// Read the post from PayPal and add 'cmd'
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "<your-auth-token>";
$req .= "&tx=$tx_token&at=$auth_token";
// Post back to PayPal to validate
$ch = curl_init('<https://www.paypal.com/cgi-bin/webscr>');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if(!($res = curl_exec($ch))) {
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
// Parse the PayPal response
$item_name = '';
$item_number = '';
$payment_status = '';
$payment_amount = '';
$payment_currency = '';
$txn_id = '';
$receiver_email = '';
$payer_email = '';
$custom = '';
$lines = explode("\\n", $res);
foreach($lines as $line) {
$line = trim($line);
if(empty($line)) continue;
$pos = strpos($line, '=');
if($pos !== false) {
$key = substr($line, 0, $pos);
$val = substr($line, $pos + 1);
$val = urldecode($val);
switch($key) {
case 'mc_gross':
$payment_amount = $val;
break;
case 'mc_currency':
$payment_currency = $val;
break;
case 'txn_id':
$txn_id = $val;
break;
case 'payment_status':
$payment_status = $val;
break;
case 'receiver_email':
$receiver_email = $val;
break;
case 'payer_email':
$payer_email = $val;
break;
case 'custom':
$custom = $val;
break;
}
}
}
?>
Section 5: Tracking PayPal Transactions in Google Analytics
After creating the custom “Thank You” page and adding the tracking code and PayPal transaction code, you can now track PayPal transactions in Google Analytics. To do this, create a new goal in Google Analytics and set the destination as the URL of the custom “Thank You” page.
In addition to tracking transactions, you can also track transaction values. To track transaction values, add the following code to the PayPal transaction code:
<?php
// Get the product price from your database or product catalog
$product_price = 50.00;
// Add the product price to the Google Analytics e-commerce tracking code
echo "<script>
ga('ecommerce:addItem', {
'id': '$txn_id',
'name': '$item_name',
'sku': '$item_number',
'price': '$product_price',
'quantity': '1'
});
ga('ecommerce:addTransaction', {
'id': '$txn_id',
'affiliation': '$receiver_email',
'revenue': '$payment_amount',
'shipping': '0',
'tax': '0'
});
ga('ecommerce:send');
</script>";
?>
This code gets the product price from your database or product catalog and adds it to the Google Analytics e-commerce tracking code. The ga('ecommerce:addTransaction')
function adds the transaction information, including the transaction ID, the email address of the receiver, the revenue, shipping, and tax. The ga('ecommerce:addItem')
function adds the product information, including the product ID, name, SKU, price, and quantity.
By adding this code, you can track not only the number of PayPal transactions but also the revenue generated by these transactions in Google Analytics.
Testing the Integration
After adding the tracking code and PayPal transaction code, it’s important to test the integration to ensure that everything is working correctly. To do this, make a test purchase on your website and verify that the transaction is tracked in Google Analytics. You can also use the Google Analytics Debugger Chrome extension to troubleshoot any issues that you encounter.
Conclusion
Tracking PayPal transactions in Google Analytics is an important part of understanding the revenue generated by your website. By following the steps outlined in this post, you can easily track PayPal transactions and revenue in Google Analytics using the PayPal transaction code and Google Tag Manager.
Remember that it’s important to test the integration thoroughly to ensure that everything is working as expected. By doing so, you can make data-driven decisions about how to optimize your website to increase revenue and improve the user experience for your customers.