Tracking first visits to a website

In the world of marketing and advertising there is always the need to understand what kind of effect the activity is having. After all you only do marketing because you hope it will drive your business forward in some way.

In essence there are only 2 ways to try to understand the effects of advertising:

  1. Micro Level – This specific user saw an ad and then converted 2 days later.
  2. Macro Level – We normally get 100 conversions a week, but when we advertised on TV we achieved 200 conversions in that week. Normally calculated as a result of some regression analysis.

The methodology in this post is a much more granular approach to the macro level analysis of advertising activity.

Web analytics tools such as Google Analytics or Adobe Analytics are very good tools at telling us how many people do what and when, eg our site received 50 visits at 11:53 on Friday 10th August. These tools are not so good at answering such questions as ‘the customer who converted at 18:30 on Saturday 11th August, when was their first visit?

This post will detail how to answer this question. Firstly we are going to need to implement some custom JavaScript code.

JavaScript code to track first visit data on this website. Implemented on all pages of the website.

<!-- First Visit cookie tag-->
<script type="text/Javascript">

// Helper functions
//
//

function writeCookie(name, value, days){
  // By default there is no expiration so the cookie is set to temporary
  var expires = "";
  
  // Specifying a number of days makes the cookie persistent
  if (days){
    var date = new Date();
	date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000))
	expires = "; expires=" + date.toString();
  }
  
  // Set the cookie to the name, value, and expiration date
  // edit the path so this is only applicable on the desired domain
  // ";path=/;domain=.seniors.com.au"
  document.cookie = name + "=" + value + expires;  
}

function readCookie(name){
  // Find the specified cookie and return its value
  var searchName = name+ "=";
  var cookies = document.cookie.split(';');
  
  for(var i = 0; i < cookies.length; i++){
    var c = cookies[i];
	while (c.charAt(0) == ' '){
	  c = c.substring(1, c.length);
	}
	if (c.indexOf(searchName) == 0){
	  return c.substring(searchName.length, c.length);
	}
  }
  return null;
}

function eraseCookie(name){
  // Erase the specified cookie
  writeCookie(name, "", -1);
}

function makeCookies(){
  // make the stasis cookies
  writeCookie("__first_visit_time", firstVisitTime, 30);
  writeCookie("__first_referrer", firstReferrer, 30);
  writeCookie("__first_landing_url", firstLandingURL, 30);
  writeCookie("__last_update_date", lastUpdateDate, 30);
}

// cookie names
// __first_visit_time
// __first_referrer
// __first_landing_url
// __last_update_date


var lastUpdateDate = readCookie("__last_update_date")

var today = new Date();
today = today.toDateString();

if (lastUpdateDate){
  if (lastUpdateDate != today){
    var firstReferrer = readCookie("__first_referrer");
    var firstLandingURL = readCookie("__first_landing_url");
    var firstVisitTime = readCookie("__first_visit_time");
    
    makeCookies();
  }

}
else {
  var firstVisitTime = new Date();
  firstVisitTime = firstVisitTime.toString();
  
  var firstReferrer = document.referrer;
  // if the referrer is an empty string
  if (firstReferrer == ""){
    firstReferrer = "___";
  }
  
  var firstLandingURL = document.URL;

  lastUpdateDate = new Date();
  lastUpdateDate = lastUpdateDate.toDateString();
  
  makeCookies();
}

</script>

 

This code leverages the information available to the page via the Document Object Model (or DOM).

For browsers to be able to provide a quality experience to the user, they need access to various data points and to react to that data. Data such as how wide is the browser window can help the browser decide how to display images. For example as a web developer you may want an image to be the full width of the browser no matter how wide the browser is. Allowing the browser to access the data relating to the width of the browser window enables the browser to resize the image so it is always the full width.

For those who are not familiar with the DOM and how it is used by the browser it is worth having a closer look. The browser can have access to an array of data points relating to your browsing session and also about your machine. Examples include browser dimension, current date and time, language and operating system.

In fact browsers are able to pick up such detailed data that there is a tracking framework known as device or browser fingerprinting that can uniquely identify users.

For the use case of recording the details of the first visit we will only make use of 3 of these data points:

  1. Current page URL
  2. Previous page URL
  3. Current time.

The JavaScript code above when deployed on a website will record in a cookie the above details on the first page load, the first time a user visits a website.

Cookies, because of their persistent nature, now make this information accessible at future times.

Where this all ties together is if you have offline activity such as radio or TV activity driving online actions. Presumably the offline activity will drive some people go to the website. This is where the details of their first visit is recorded in the cookie. Eventually some of these people will go on to do a desired business action, such as buy a pair of shoes. It is at this point, at the point of conversion that we can retrieve the first visit data from the cookie and store it alongside the conversion data.

We now have conversion data enriched with details about their first visit. Not only can we now answer questions like ‘after coming to our website for the first time how long does it take someone to convert on average‘ but we can marry this first visit data up with TV or radio activity data and say ‘Our prime time ad aired at 7pm, which resulted in 300 new people to the website in the following 20 minutes. Of these 300 new people to the website 50 converted within the hour, and another 75 converted over the next 3 days‘.

Without the first visit data we would only be able to count those conversions that came in in a close proximity to the ad airing, and not counted those who take longer to pull the trigger. Giving us a fuller picture of what kind of effect the advertising is having.

For a live example of this methodology in action check out the First Visit Data page.

Send a Comment

Your email address will not be published.