How to Automatically Track Shopify Refunds in Google Analytics

Google Analytics is a great reporting tool for Shopify stores because it lets you  track visitors, sessions, and other customer behavior like e-commerce order transactions. However, there is a big problem that Shopify merchants encounter when using Google Analytics: it’s missing order refunds and returns.

Historically, getting refunds from Shopify orders to show up in Google Analytics was a manual job. It is a method that is time-consuming, prone to human error, and results in a lack of real-time data.

That’s where Mesa comes in. By automating this workflow, you can create real-time reversal transactions in Google Analytics. Let’s take a look at how out how it’s done.

Step 1

Start by creating a new workflow from the Mesa Dashboard.

In the “Connect This” menu, choose “Shopify”, then select “Refund” and “Created”. This tells Mesa to trigger your workflow every time a refund occurs on your Shopify store.

Next, we’ll tell Mesa that we want this workflow to send some information to Google Analytics. Start by choosing “Google Analytics”, then select “Transaction” and “Create”. Now click the Make My Automation button.

Step 2

Mesa will take you to the workflow builder, pre-populated with your previous selections, so you can customize the automation:

Click to expand into the Transform section. You’ll notice that you can set custom values for the various Google Analytics properties (here called Keys).

We’ll need to use a little bit of custom code in order to calculate the order refund total so that we can pass it along to Google Analytics. To do that, we’ll need to add an additional step by clicking the plus sign button (+) that’s between the “Shopify Refund Created” and “Transform” steps. The type of step we’re going to be adding is called “Custom”. You should now see this:

Click “Edit Code”. You’ll now be taken to the scripts editor:

Here on the scripts editor we can instruct Mesa to total the value of the refund and output it into a custom key for us to use in the “Transform” section. You can copy and paste the following code into the editor:

const Mesa = require('vendor/Mesa.js'); module.exports = new class {   script = (payload, context) => {     // Loop through transactions on the refund, and total up the refund amount     let refundAmount = 0;     payload.transactions.forEach(transaction => {       refundAmount -= transaction.amount;     });     // Add to payload     payload.total_refund_amount = refundAmount;     // We're done, call the next step!     Mesa.output.next(payload);   } }

Your editor should look something like this:

Screen Shot 2020-07-09 at 11.51.46 AM

Now that we’ve put together the custom property, let’s save the code, and return to the builder.

Step 3

Within “Transform”, we are first going to select tr from the Key menu. Tr is Google Analytics’ “transaction revenue” property. Since we want to map our custom property into tr, place {{custom.total_refund_amount}} in the Value.

We must add in the refund ID so that Analytics knows which transaction this is being mapped to, so click Add Row. Now select ti for “transaction ID” in the Key and place {{shopify_refund.id}} in Value.

Because the transaction ID must be unique in Analytics, it’s best to use the refund.id property rather than refund.order_id, in case there are multiple refunds for the same order. Or you could put both together, for example ‘{{shopify_refund.order_id}} – {{shopify_refund.id}}’.

You should be looking at this now.

Step 4

The final step is simply to enter your Google Analytics ID into the Google Analytics Create Transaction section, so Mesa knows which Analytics account to output the data to.

Turn it on

Now you should be looking at a complete workflow that looks like this:

We always recommend testing your workflows first. Your transactions should appear in Google Analytics under Conversions>Ecommerce>Sales Performance. You can see what my test refund showed up as below:

Install Mesa from the Shopify App Store

Delivering Happiness: 5 Workflows for a Great Post-Purchase Experience