Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This script is taken from http://www.savio.no/blogg/a/114/tracking-content-scrollers-scanners-og-readers-in-google-analytics Worthwhile read. Also Eivind Savio notes that the ORIGINAL innovator of this script was Thomas Baekdal (www.baekdal.com)
- // This script was originally written by Justin Cutroni, see http://cutroni.com/blog/2012/02/21/advanced-content-tracking-with-google-analytics-part-1/
- var readerTime = 30; // Seconds after scroll to bottom of content before visitor is classified as "Reader"
- var readerLocation = 150; // # px before tracking a reader
- var callBackTime = 100; // Default time delay before checking location
- // Set some flags for tracking & execution
- var timer = 0;
- var contentLength = 0; // Content Length -> Length of content area
- var scroller = false;
- var endContent = false;
- var didComplete = false;
- // Set some time variables to calculate reading time etc.
- var pageTimeLoad = 0;
- var scrollTimeStart = 0;
- var timeToScroll = 0;
- var contentTime = 0;
- var endTime = 0;
- jQuery(function($) {
- // Check if content has to be scrolled
- if ($(window).height() < $('#contentArea').height()) { // Replace contentArea with the name (class or ID) of your content wrappers name
- pageTimeLoad = new Date().getTime();
- contentLength = $('#contentArea').height();
- _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Allowed',window.location.pathname,contentLength,true]);
- }
- // Check the location and track user
- function trackLocation() {
- bottom = $(window).height() + $(window).scrollTop();
- height = $(document).height();
- // If user has scrolled beyond threshold send an event
- if (bottom > readerLocation && !scroller) {
- scroller = true;
- scrollTimeStart = new Date().getTime();
- if (pageTimeLoad > 0) {
- timeToScroll = Math.round((scrollTimeStart-pageTimeLoad)/1000);
- } else {
- timeToScroll = ""
- }
- // Article scroll started
- _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Started',window.location.pathname,timeToScroll,true]);
- }
- // If user has hit the bottom of the content send an event
- if (bottom >= $('#contentArea').scrollTop() + $('#contentArea').innerHeight() && !endContent) {
- timeToScroll = new Date().getTime();
- contentTime = Math.round((timeToScroll-scrollTimeStart)/1000);
- if (contentTime < readerTime) {
- _gaq.push(['_setCustomVar',1,'ReaderType','Scanner',3]);
- _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Content Scanner',window.location.pathname,contentTime,true]);
- } else {
- _gaq.push(['_setCustomVar',1,'ReaderType','Reader',3]);
- _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Content Reader',window.location.pathname,contentTime,true]);
- }
- endContent = true;
- }
- // If user has hit the bottom send an event
- if (bottom == height && !didComplete) {
- endTime = new Date().getTime();
- totalTime = Math.round((endTime - scrollTimeStart)/1000);
- _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Page Bottom',window.location.pathname,totalTime,true]);
- didComplete = true;
- }
- }
- // Track the scrolling and track location
- $(window).scroll(function() {
- if (timer) {
- clearTimeout(timer);
- }
- // Use a buffer so we don't call trackLocation too often.
- timer = setTimeout(trackLocation, callBackTime);
- });
- });
Add Comment
Please, Sign In to add comment