<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/analyzing-customer-data-from-square/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://stevenmortimer.com/analyzing-customer-data-from-square"> Blog-rss on stevenmortimer.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<ul>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#the-square-data-model" target="_blank">The Square Data Model</a></li>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#authenticating" target="_blank">Authenticating</a></li>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#pulling-transaction-data" target="_blank">Pulling Transaction Data</a></li>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#spend-by-customer-group" target="_blank">Spend by Customer Group</a></li>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#issues-with-the-apis" target="_blank">Issues with the APIs</a>
<ul>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#cannot-request-specific-fields" target="_blank">Cannot Request Specific Fields</a></li>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#cannot-update-customer-groups-programmatically" target="_blank">Cannot Update Customer Groups Programmatically</a></li>
<li><a rel="nofollow" href="https://stevenmortimer.com/analyzing-customer-data-from-square#customer-id-not-on-transactions" target="_blank">Customer ID Not On Transactions</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h2>The Square Data Model</h2>
<p>Whether you own your own business or consult for a business using Square to capture payment data, Square can offer some amazing opportunities to gain insights by leveraging their Connect v1 &amp; v2 APIs. The Square data backend operates much like a CRM system that holds information about <em>transactions</em> that <em>customers</em> make when purchasing <em>items</em> offered at a <em>location</em>. Naturally, there are API endpoints for each of the object types italicized above (Locations, Customers, Transactions, and Items) and many more endpoints for administrative tasks (employees, roles, timecards, refunds, etc.).</p>
</div>
<div>
<h2>Authenticating</h2>
<p>While working with Square data I decided to develop an R package <a rel="nofollow" href="https://github.com/StevenMMortimer/squareupr" target="_blank"><b>squareupr</b></a> that makes it easier to retrieve Square data from R so that you can focus on the analysis. After installing the <strong>squareupr</strong> package you must authenticate by supplying a personal access token (PAT) or using an OAuth 2.0 flow. You can find your PAT by logging into the <a rel="nofollow" href="https://squareup.com/" target="_blank">Square Dashboard</a> -> Apps -> My Apps. Create an app or click “Manage App” if you’ve already created one and there you should see your personal access token:</p>
<div>
<img src="https://i0.wp.com/stevenmortimer.com/blog/analyzing-customer-data-from-square/finding-your-pat.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<pre><code># The squareupr package is not yet available on CRAN so you must install from GitHub
# install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;StevenMMortimer/squareupr&quot;)

library(tidyverse)
library(squareupr)

# authenticate using your Personal Access Token (PAT)
sq_auth(personal_access_token = &quot;sq-Th1s1sMyPers0nalAcessT0ken&quot;)</code></pre>
<p>The package also offers OAuth 2.0 authentication. More information is available <a>here</a>.</p>
</div>
<div>
<h2>Pulling Transaction Data</h2>
<p>As mentioned above there are endpoints for every major type of data stored by Square. The <a rel="nofollow" href="https://docs.connect.squareup.com/api/connect/v2" target="_blank">API documentation</a> does a very good job at laying out how requests should be made to each of those endpoints. One thing to note is that it is important to first pull down the location details for your business because the location is often required when searching for things like transactions and items.</p>
<pre><code>our_locations &lt;- sq_list_locations()
our_locations$name &lt;- &quot;{HIDDEN}&quot;
our_locations %>% select(id, name, address, timezone, 
                        capabilities, status, created_at)
#> # A tibble: 5 x 7
#>   id            name     address  timezone  capabilities status created_at
#>   </code></pre></div>