Tracking user behaviour can help you tailor your website to users' needs and keep them on your site. We've developed a script to track how far down a page a user scrolls before navigating away from that page, which should be especially useful for websites with longer pages. If for example you found that hardly any users actually scrolled to the very bottom of your web page you may then decide to either condense your content or adjust it so more users are likely to view it.
What skills do I need to apply scroll detection?
- MooTools framework
What do I need to start tracking user scroll behaviour?
For the purpose of this tutorial we will create a demo page for this code to run on but this can be applied to any page on a site (see 'How to Integrate into your site' below).
Overview of scroll detection
Before going into detail it's worth providing a textual sketch of the technology involved and how it will be used.
- AJAX and PHP used to save the data
- A MySQL database to store the data
- Finally some HTML and CSS to display the results
Let the coding begin...
<link href="stylesheet.css" rel="stylesheet"></link>
<!-- Date Picker -->
<link href="datepicker.css" rel="stylesheet"></link>
Scroll detection database storage
Now you have the web page, you will need to create a MySQL database and table to store the captured data. In the database we will store the depth the user scrolled to (ScrollDepth), the file name of the web page the user is on (Page), and the date (DateAdded).
CREATE DATABASE scrolltrack_db;
USE DATABASE scrolltrack_db;
CREATE TABLE userscrolls (`ID` int(11) NOT NULL auto_increment,
`ScrollDepth` float(22,2) default NULL,
`Page` varchar(255) default NULL,
`DateAdded` datetime default NULL,
PRIMARY KEY (`ID`),
KEY `FirstNameIndex` (`ScrollDepth`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Scroll detection configuration
There are certain options that you can amend within the control.js file:
runtracker: This value can be either true or false. If set to true then tracking will be stored in the database table. When debugging or viewing the results it is best to set this to false.
showresults: This value can be either true or false. If set to true then the results of the tracking will be displayed on the pages with any results.
domain_home_page: This needs to be set to the value of the file name for your home page so that if a user is on your domain with no filename selected they will still be tracked, e.g. http://www.bluelinemedia.co.uk
You will also need to set up your database settings at the top of the ajax.php file.
Let's start some scroll tracking
Now the script is up and running let's get some test data in there so we can see this script in action. First of all make sure 'runtracker' is set to true. Then load the web page and scroll down a certain amount, then close the page. Repeat this process numerous times to build up a few tracking results. Once you have a set of results set 'runtracker' to false and set 'showresults' to true to view the data that has been tracked.
Add user scroll tracking to your website
You will then need to amend the options in the control.js file. Make sure 'debug' is set to true as this will help you to identify if the script is working: you should see this message on the page: 'Scroll detection is active!'. Finally set up your database settings in ajax.php and you're good to go.
Filtering the user scrolling results
Included in this script is a filter box that will appear in the top right corner of the web page when showresults is set to true. This will display the date when tracking was started (i.e. date first tracking entry was recorded) and a date form. The date form enables you to view the tracking data for a particular date or month or year. Just leave the day and/or month values empty to leave the filtering more vague. Clicking on the calendar icon will popup a date picker to make it easier to choose a specific date.
So what do these results mean?
If you have enabled the showresults option then you will see all the results displayed on the page. Transparent horizontal bars will appear denoting the results. The results are grouped every 50 pixels to make the results easier to read. The width of the bar represents the percentage number of the result and shows the number of users who have not viewed that area of the page. The text within the bar gives the exact percentage. For example if there is a bar on your page that says 60%, then this means only 40% of users have read that far down the page.
Generally you will see no bars at the top of the page with them becoming wider as you scroll down the page of results. If the bar is the full 100% width then no users have viewed that content on your web page.
This should give you a good indication of how much of your content most users are actually viewing. Once you know how users are really using your pages, you can enhance content and improve results.