Passing Customers’ Data to Google Analytics from Shopify

20 July 2020

post_image

Hey, world of analytics experts and enthusiasts!
We want to share with you a quick ‘how to’ guide on passing customer identifier and customer type (new vs. repeat) into Google Analytics from Shopify using Google Tag Manager.
Note: the same logic could be used to track purchases by new vs. repeats customers within ad platforms – Facebook Ads, Google Ads, etc.
Business impact of this data being collected:
1. Ability to measure/analyze customer LTV (by customer Id) and retention.
2. Viewability into what drives (channels, sources, campaigns, landing pages, etc.) new customer acquisition vs. repeat customers.
Step 1: Add ‘Customer ID’ and ‘Customer Type’ custom dimensions in Google Analytics:

Step 2: Make sure you have installed GTM snippet in your Shopify account. You can use the following guide.
Step 3: Go to the ‘Settings’ -> ‘Checkout’ in Shopify:

Find ‘Additional scripts’ field under ‘Order processing’ section:

And add the following script to this field:

{% if first_time_accessed %}
   {% if order_number %}
      <script>
         var customerType = {{customer.orders_count}};
         customerType == 1 ? customerType = "new" : customerType = "repeat";
         window.dataLayer = window.dataLayer || [];
         dataLayer.push({
             'event': 'GTMevent',
             'eventCategory' : 'purchase',
             'eventAction' : 'complete',
             'eventLabel' : '{{customer.id}} | ' + customerType,
             'customerId': '{{customer.id}}',
             'customerType': customerType
         });
      </script>
   {% endif %}   
{% endif %}

The code should look like:

Save the changes.
Step 4: Add the following dataLayer variables in the GTM:
1) Customer Type variable:

2) Customer Id variable:

3) Event Category variable (as we will be passing the data via event hit into GA):

4) Event Action variable:

5) Event Label variable:

Step 5: Add the custom event trigger to fire on ‘purchase’ event:

Step 6: Add Google Analytics Event tag to push the data into analytics:

That’s it! Publish GTM container and start collecting your data in Google Analytics.

Author: Alex Zakharych, analyst and data scientist at Viden.

Related Insights

The Manifest Names Viden as Belarus’ Most Recommended Digital Marketing Agency

30 September 2021

We’re absolutely excited to announce that The Manifest officially named Viden as Belarus’ most recommended digital marketing agency for 2021! Our company wouldn’t be where we are on The Manifest without our clients. Thanks to their amazing feedback, we’ve continuously improved our services and solutions.
Learn more

Viden Records a New 5-Star Rated Review on Clutch

28 September 2021

It’s always a pleasure receiving such great reviews from our past and present clients. We always strive to provide exceptional service to our clients and see them achieve their goals.
Learn more

iOS 15 Rolled Out: New Privacy Features, Marketing Impact and Solutions

27 September 2021

With iOS 15, Apple has released new privacy features that have a great impact on email marketing. Take a look at how they will affect your email campaigns and how to adapt to the oncoming changes.
Learn more