As an analyst you want to segment website visitors. For an eCommerce website, perhaps you want to segment your visitors into prospects, leads and customers. But many times, you just know the type, if a visitor purchased (and thus, becomes a “customer”).
Here is our visitor behavior hierarchy:
- If the visitor has purchased, they are a customer
- Otherwise, if the visitor is logged in, they are a lead
- Otherwise, if we don’t know anything, they are a prospect.
This custom script uses
sessionStorage, not cookies. What’s the difference? Cookies are primarily for reading server-side, whereas local storage can only be read client-side. More information: What is the difference between localStorage, sessionStorage, session and cookies? And, if you are located in the EU, don’t forgett about the EU’s cookie law. LocalStorage and sessionStorage are treated as cookies.
Data layer available
In this code snippet, prospect is the default value (i.e. the absence of other information to place the user higher up on the hierarchy). Then, remember this value for a pageview and save the data element.
We use conditional statements to determine if web storage is supported by the browser and if the sessionStorage for the visitor type isn’t set. If so, we dig into the data layer and set the localStorage and sessionStorage to the equivalent visitor type. If there is no information available, we look up if the localStorage is set and if not the visitor type will be
We can also create two more data elements: visitorTypeLogin and visitorTypePurchase.
Here is the custom script for visitorTypeLogin, where the default value is
Custom script for visitorTypePurchase, the default value is
All custom scripts basically do the same thing, with slightly different business logic.
After this, we create three page load rules:
1) On every page, an eVar is set to %visitorType%. (It’s also possible to track this in the Adobe DTM properties, so then you wouldn’t even need an extra page load rule). 2) After a login, the eVar is set to %visitorTypeLogin%. 3) After a purchase, where the eVar is set to %visitorTypePurchase%.
The conditions should be evaluated after the data layer exists in the DOM. As an example, if the data layer is loaded after the opening
<body> tag, the rule should be triggered at the bottom of page. If you’re using Adobe Analytics call, the tracking script should be after these page load rules to avoid two pageviews.
Data layer not available
If a data layer is not available, there is a slight change in the first two custom scripts and as you can imagine it will make tracking less accurate.
Custom script for visitorType, the default value is prospect:
Custom script for visitorTypeLogin, the default value is lead:
The page load rules in your tag manager would be the same.
View last Words
The first caveat is that this data will not be, and could never be, 100% accurate. For visitors who are not logged in, it is just not possible to know definitely whether they are an existing customer or a prospect. Your accuracy will improve over time as customers return and can be identified, even if they are not logged in. However, this does not work if they delete their cookie or use a different device. (see Peter O’Neill in “Understanding your Website Visitors”)
Understanding your Website Visitors: Prospects vs CustomersUnderstanding your Website Visitors: Prospects vs Customers
What is the difference between localStorage, sessionStorage, session and cookies?
EU’s cookie law