As a web analyst with a technical background, I sometimes have ideas that are a mix of marketing knowhow and my basic JavaScript knowledge. This post is about one of them. It’ll show you how to measure what type of clients buy on your website. Are they new, or existing clients? It includes a how-to guide to set this up in Google Tag Manager.


Page referral information contains the URL of the previous page. For example: when I visit, and click to, my referral on the second page will be This is how Google Analytics tracks referrals source information. The referral is easy to get with JavaScript:


So how can we use this simple code snippet to improve client acquisition insights?

Improve client acquisition insights with referrals####

You can use referral information to track the previous page from another domain, or within the same domain. The latter option is the one we will use to gain more info about client acquisition. Let’s use a standard webshop as an example.

On a webshop, users generally can go through one of the following two flows when ordering:

  • Basket > Login > Overview > Transaction; or
  • Basket > Register > Overview > Transaction.

The difference between the two flows is step 2: the login or register page. On the overview page, we can use the referral information to tell us if a visitor is an existing, or new customer:

Basic JavaScript code

if(document.referrer === ''){
    //track new customer
  } else {
    //track existing customer

With this code snippet, you can measure the type of customer that visit your page.


The implementation of the script is key. You’ll only want to load the script on the page after the register page. In case of the example flow, the overview page. To give you an example, here’s a possible Google Tag Manager setup (no dataLayer changes required):

1: GTM Variable

Step one is setting up the GTM variable. We’ll use a Custom JavaScript tag. Make sure to change the referral to the correct one of your website.
GTM Client Type Variable

2: GTM Tag

Step two is setting up the GTM tag. We’ll use a Google (Universal) Analytics event tag. You can set it as the event label, add a custom dimension for it or set both. Make sure to change the custom dimension index to match it with your Google Analytics setup.
GTM Client Type Tag

3: GTM Trigger

Step three is to set up a trigger that matches with the right page. In our example case, this is the /overview page.
GTM Client Type Trigger


With these three things set up, you’re ready to measure your customer types without the need for developers. Keep in mind to:

  • Find the right pages that define your new or existing customer rule;
  • Set the right referrer in the variable; and
  • Set the right page that triggers the tag.

You can use the referral information in other cases as well. For example: you could use it to track the previous page of a product detail page, to track how people land there.

What did we learn from this####

The main point of this post is ‘hybrid knowledge’. If you teach yourself marketing as a developer, it becomes easier to translate a marketer’s question into a technological answer. It works the other way around as well. If you’re a marketer and invest in coding, you know what’s possible on a technical level. You don’t need to be a great developer, but you should be able to write down what you need theoretically.

This way, the world of marketing and development will find it easier to understand each other. And together, you’ll grow to a higher level.

Leave a Reply