analyticsninja

Awesome GA Scroll Tracker

Nov 14th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //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)
  2.  
  3. // This script was originally written by Justin Cutroni, see http://cutroni.com/blog/2012/02/21/advanced-content-tracking-with-google-analytics-part-1/
  4.  
  5. var readerTime = 30; // Seconds after scroll to bottom of content before visitor is classified as "Reader"
  6. var readerLocation = 150; // # px before tracking a reader
  7. var callBackTime = 100; // Default time delay before checking location
  8. // Set some flags for tracking & execution
  9. var timer = 0;
  10. var contentLength = 0; // Content Length -> Length of content area
  11. var scroller = false;
  12. var endContent = false;
  13. var didComplete = false;
  14. // Set some time variables to calculate reading time etc.
  15. var pageTimeLoad = 0;
  16. var scrollTimeStart = 0;
  17. var timeToScroll = 0;
  18. var contentTime = 0;
  19. var endTime = 0;
  20. jQuery(function($) {
  21. // Check if content has to be scrolled
  22. if ($(window).height() < $('#contentArea').height()) { // Replace contentArea with the name (class or ID) of your content wrappers name
  23. pageTimeLoad = new Date().getTime();
  24. contentLength = $('#contentArea').height();
  25. _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Allowed',window.location.pathname,contentLength,true]);
  26. }
  27. // Check the location and track user
  28. function trackLocation() {
  29. bottom = $(window).height() + $(window).scrollTop();
  30. height = $(document).height();
  31. // If user has scrolled beyond threshold send an event
  32. if (bottom > readerLocation && !scroller) {
  33. scroller = true;
  34. scrollTimeStart = new Date().getTime();
  35. if (pageTimeLoad > 0) {
  36. timeToScroll = Math.round((scrollTimeStart-pageTimeLoad)/1000);
  37. } else {
  38. timeToScroll = ""
  39. }
  40. // Article scroll started
  41. _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Started',window.location.pathname,timeToScroll,true]);
  42. }
  43. // If user has hit the bottom of the content send an event
  44. if (bottom >= $('#contentArea').scrollTop() + $('#contentArea').innerHeight() && !endContent) {
  45. timeToScroll = new Date().getTime();
  46. contentTime = Math.round((timeToScroll-scrollTimeStart)/1000);
  47. if (contentTime < readerTime) {
  48. _gaq.push(['_setCustomVar',1,'ReaderType','Scanner',3]);
  49. _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Content Scanner',window.location.pathname,contentTime,true]);
  50. } else {
  51. _gaq.push(['_setCustomVar',1,'ReaderType','Reader',3]);
  52. _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Content Reader',window.location.pathname,contentTime,true]);
  53. }
  54. endContent = true;
  55. }
  56. // If user has hit the bottom send an event
  57. if (bottom == height && !didComplete) {
  58. endTime = new Date().getTime();
  59. totalTime = Math.round((endTime - scrollTimeStart)/1000);
  60. _gaq.push(['_trackEvent','Page Scroll','Page Scroll: Page Bottom',window.location.pathname,totalTime,true]);
  61. didComplete = true;
  62. }
  63. }
  64. // Track the scrolling and track location
  65. $(window).scroll(function() {
  66. if (timer) {
  67. clearTimeout(timer);
  68. }
  69. // Use a buffer so we don't call trackLocation too often.
  70. timer = setTimeout(trackLocation, callBackTime);
  71. });
  72. });
Add Comment
Please, Sign In to add comment