square google analytics

For those of you that have read my blog before, you’ll know how much I enjoy integrating Google Analytics with 3rd party data sources for analysis beyond your typical website and mobile app behaviors. Earlier last year, I wrote a couple posts about extending Slackalytics in Google Analytics and this technique inspired me to do something similar with Square.

One of the best things about Google Analytics is that it allows you track and analyze just about anything using the measurement protocol. In this case, I was interested in a way I could extend a point of sale (POS) system with Google Analytics to track offline purchase activity. This could be greatly helpful for small-medium size businesses that sell products and services online and in person.

In this post, I will show you how to integrate Square transactions with Google Analytics.

Note: This post is a little technical, so I will assume that you have some level of development knowledge. I’ll go into as much detail as possible with some reference links to other sites for additional context and instructions.

Setting up Google Analytics

Create a new property

In order to send our data to Google Analytics, we’ll want to start out by creating a new property. Use whichever account you’d like and navigate to Admin > Property > Create New Property. I’ll name it Square and you can put anything in the Website URL field. Since we’re not technically tracking a website, the URL does not matter here. If you were looking to send transactions into an existing property, then you can skip this step.

google analytics property setup

Enable User-ID

In this example, I’m going to assume that you have 1 Square merchant account with multiple registers. For example, I might own a clothing store where each of the sales reps have a mobile device to take transactions. Setting up the user-ID will allow you to attribute specific sales to each rep.

To enable User-ID, go to Admin > Property > Tracking Info > User-ID. Follow the instructions and set up a new User-ID view. Since all hits we send to GA will contain a User-ID, we will not need to use the default view that was automatically created when we created the GA property.

google analytics user id view

Enable ecommerce

Since we’ll be sending ecommerce transactions to our property, we’ll need to enable ecommerce. Go to Admin under your User-ID view and follow the instructions to enable this functionality.

google analytics enhanced ecommerce

Create custom dimensions

We will want to create 3 custom dimensions for the payment type (cash or credit card), transaction time stamp and user ID. All of these dimensions should have a hit level scope and should be created in the correct order as shown below. It’s very important they’re created in this order because the code will reference these IDs. Go to Admin > Property > Custom Definitions > Custom Dimensions to create them.

google analytics custom dimensions

Set up the app

I’m going to use Heroku to deploy this app because it’s pretty easy and free. I’ve added all the code to Github so you can easily pull it down using the steps below. In addition, I’ve added notes to the code so you can better understand what’s going on.

Download and install Heroku Command Line Interface (CLI).

Clone the repository to the directory of your choice using the command lines below.

git clone https://github.com/iabramo/Square-GA.git
cd Square-GA

Run the following commands to create and deploy the application. Please take note of the URL created after the second command because you’ll need it later.

git init
heroku create
//TAKE NOTE OF THE HEROKU URL CREATED HERE AS YOU WILL NEED IT LATER!
 git add .
git commit -m "initial commit"
git push heroku master

Configure your Square account

Assuming you already have your Square card reader and account set up, you’ll want to create a developer account here.

Next, go to https://connect.squareup.com/apps to create a new application. You can title it Google Analytics.

square create new application

Click on Webhooks from the main navigation. This is where we are going to specify the URL where we’ll send the data when a new transaction is made. Copy and paste the Heroku URL that was noted from the earlier step when we created the Heroku instance and add ‘/sq’ to the end of it.

square webhooks

Configure Heroku variables

Next, we’ll want to create a Heroku variable to set our GA property ID and Square access token by running the command below. Just replace UA-XXXXX-XX with your GA property ID and XXX with your access token from Square. The access token can be found directly from the app you created in Square.

heroku config:set gaPropertyId=UA-XXXXXXX-Y
heroku config:set squareAccessToken=XXX

Test it out

Everything is all set up and ready to test! An easy way to do this is by opening up your realtime reports in Google Analytics to see the events flowing in. Make a purchase with your credit card or cash, and you should see an event pop up.

google analytics real time

You can also view the logs directly from Heroku to see the data coming in from Square and sent to GA. I added some console logs so you can see that in action.

View the reports

Now that we have the app working, it’s time to view the data in the reports. Since, I’ve configured the app to take in products, you’ll be able to see them in the reports as well.

For example, let’s say we own a clothing store with a t-shirt that has 3 different color variations. You can add these products to your Square account and they will automatically be passed to GA when the purchase is made. You’ll also notice the tax amount is sent to GA as well.

square products

google analytics product report

And that’s it! Even if you’re not a Square customer, I hope this inspires you to think about different ways you can use Google Analytics to store and manage data. Over time, all enhancements and bug fixes will be made to the Github repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.