Internet Performance Delivered right to your inbox

How To Use Cookies To Better Understand Your Customers’ Needs

Every online business wants to cater to their customers’ needs in the most effective way possible. Sometimes that means knowing your target audience better as a whole, but sometimes it means knowing a little more about your customers as individuals. Nowadays, there are all sorts of paid software and services to help better understand your online customers by tracking their behavior and spending habits on your site.

However, there are some great cost-effective ways to help you monitor your online users as well. One of the most useful ways is to utilize cookies, which are a great non-invasive way to gather information about visitor behavior on your website and learn how to improve their user experience at the same time.

Setting Your Cookies

Cookies are any web developer’s or marketer’s best friend. They help us map user behavior and require very little knowledge to set up. Cookies are just little pieces of information that are stored on a user’s browser, provided that the user is allowing cookies to be stored at that time. Each cookie is set to store a certain value for a determined amount of time, which can range anywhere from when the browser is closed to months.

The following code is the general formula to declare a cookie:

setcookie($name, $value, $expire, $path, $domain, $secure, $httponly)
  • $name – the name you designate to the cookie (string value)
  • $value – most important, the piece of information you choose to store (open value)
  • $expire – the time when the cookie will expire, expressed as a Unix timestamp (integer value)
  • $path – the path within your site that the cookies will track, if entire site then use “/” (string value)
  • $domain – the site the cookies will track, include a leading period, ex. “.dyn.com” to track all subdomains (string value)
  • $secure – whether or not the cookie requires a secure connection in order to be set (boolean value)
  • $httponly – whether or not the cookie will be accessible only through the HTTP protocol (boolean value)

Important: In order for cookies to work properly, they need to be set before any HTML is generated. For each website, this may be a slightly different place but a good practice would be to simply set them before the HTML doctype declaration.

Cookies can help you optimize your user experience

Select What You’d Like To Track

If you decide to generate your own cookies, be sure to think through what information would be most useful for you to know in order to provide a better service or product to your customer. Most non-personal data can be captured in cookie form, so what you choose to track is limited only by your imagination.

After setting up a few variables, you can track anything from pages viewed, to time spent on pages, to user browser language, to the number of times the user may have visited your site. All this information can help you to better-recognize the various customer segments you may have and devise different approaches to dealing with each.

A Few Adjustments to WordPress for Cookies

For those of you who use WordPress like us, you can actually set multiple cookies at once by declaring and calling your own functions. Simply include all setcookie() functions within a custom function and then include that custom function in the header of your site. When that single custom function is called (ex: wp_set_all_cookies()), all your cookies can be set at the same time. This is very useful for anyone tracking multiple types of cookie data.

Here is what a basic WordPress cookie function might look like:

<?php
function wp_set_all_cookies() {
if ($_COOKIE["viewed1"] != get_the_title()) {
setcookie("viewed3", $_COOKIE["viewed2"], time()+3600, "/", ".cookie.com");
setcookie("viewed2", $_COOKIE["viewed1"], time()+3600, "/", ".cookie.com");
setcookie("viewed1", get_the_title(), time()+3600, "/", ".cookie.com");
}}
?>

This function will set three different cookies (named viewed1, viewed2, and viewed3) to track the title of the past three posts/pages the user viewed. They will expire an hour after being set and will be available to both the entire domain and on all subdomains of cookie.com. Also, the function includes an “if statement” that is quite useful for ensuring that a cookie is not set for repeated views or refreshes. If the page title matches the one currently set in viewed1, then it will not set a duplicate cookie for that page.

WordPress custom functions make it very easy to set cookies and retrieve cookie information. However, a few adjustments will need to be made to WordPress’s default code in order to override some unfavorable browser behavior.

Why are my cookies wrong in Firefox?

When setting cookies to track the current page using the WP function get_the_title(), most browsers obey and place that single page/post title into a cookie. However, due to WordPress’s default behavior of caching URLs specified in the href of the <link rel="next"> code; Firefox ends up storing the title of both the current page as well as the title of the cached page (regardless of whether you ended up visiting it) in cookies. This bug can be remedied by initializing the following code along in functions.php:

remove_action(‘wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

WordPress includes these links to facilitate easier user navigation from blog post to blog post. However, Dyn’s website primarily uses pages instead of posts, so the previous and next links were not accurate for our content as they’d been created in a random order. Therefore, we had no reason to keep these links and felt comfortable removing them.

Gathering The Cookie Information

Once the cookies are set properly (and I suggest testing them on your own across all major browsers), you are free to then pull the information you have decided to store using:

if(isset($_COOKIE['yourcookiename’])) {$cookievariable = $_COOKIE['pageview1'];}

This simply checks to see if the cookie exists on the browser, and if it does, it takes the value stored in that cookie and places it in a variable that you can later call. Voila! The information is now pulled from the user and is ready for you to use.

From here, you have a few options: 1) store the information in variables to display back to the user, 2) send the information along with values when the user submits a form or 3) do whatever is most beneficial to you! For those using WordPress, you can pull down and parse multiple cookies by calling a single custom function. I highly recommend doing this to save time and effort later down the road.

Remember, getting information via cookies doesn’t work for every online visitor, so sometimes you may not get all or any cookie information from them. Cookies can be easily deleted or blocked if the user chooses to do so and we must respect the fact that some people aren’t comfortable with any information being stored on their browsers. However, in general, most users allow them to exist on their browsers.

Using Cookie Info To Benefit The Customer

Now that you have all the information available to you, you need to think critically about how to best use it to improve the experience.

Will the information be displayed back to the user in the form of product recommendations that are similar to those they viewed previously? Do you want registered or premium users to be greeted by name upon visiting? Is this information that should be sent along with support or sales inquiry forms so that your teams have a more well-rounded view of the customer even before first contact?

The information you can retrieve and the ways you can use it are far from limited. Once you start being able to separate the individual customer from the crowd, you can begin to offer an experience that will make them feel more visible and understood.

Overall, cookies allow for a great symbiotic relationship between the business and its customers. Businesses are able to make more informed decisions based on information that the customer would never have thought to provide, and customers therefore receive a more personalized and targeted shopping experience.

Sometimes to initialize change, all it takes is something small…as small as a cookie, perhaps.


Share Now

Whois: Dan McAuliffe

Dan McAuliffe is the User Experience Manager for Dyn, the world leader in Internet Performance Solutions that delivers traffic management, message management, and performance assurance. Follow him on Twitter: @Dan_McA and @Dyn.