Forum Moderators: open

Message Too Old, No Replies

Javascript to analyze Visitor time on page?

Surely it's possible

         

Philosopher

5:23 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So I've designed a new site...it's actually a one page salesletter for a new product.

It's one of the normal long-form salesletters. What I'm wanting to do is find the average time a visitor spends on the page.

Since it's a simple one page salesletter, log files won't work.

I would imagine this is possible with some javascript, but I'm clueless when it comes to javascript so I don't know if this would be something relatively simple or complex.

I've hunted around and can't find anything along these lines.

Any help pointing me in the right direction would be much appreciated.

Fotiman

6:10 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since you can't rely on the unload event, your best bet would probaby be some sort of AJAX solution and setInterval. For example, you would use setInterval to call a function every 5 seconds or so (I'm not sure what level of detail you're looking to capture and you may want to tweak that value up or down depending on your traffic). This function would then perform an AJAX connection to the server, passing it some unique identifier for the current user along with the total time spent. You might to look into a framework for handling the AJAX portion (so you know it works for multiple browsers). The Yahoo UI Library has a Connection Manager that may be of interest to you.

Philosopher

6:40 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok...so it's at least possible. That's good news.

Since I'm an idiot with javascript and AJAX, I'll see if I can find a freelancer to code it for me. It will likely cost less to do that, than my time in trying to figure it out right now.

whoisgregg

9:00 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using Prototype [prototypejs.org], the code to provide this functionality is just a few lines. I've even included the code to show you where the user is scrolled to on the page so you can track how far down they've read. (Although you probably want window height too.) :D

<script type="text/javascript" src="/js/prototype.js"></script>
<script type="text/javascript">
var uniqueId = '<?php echo session_id();?>'; // or generate one with javascript/asp/whatever
function log(){
var url = '/log.php?id=' + uniqueId + '&st='+document.body.scrollTop;
new Ajax.Request(url, { method: 'get' });
setTimeout('log()', 1000); // 1000 = 1 second, increase to reduce server load
}
Event.observe(window, 'load', function() {log(); });
</script>

Of course, you might not want to include such a hefty library for a single purpose, but I'm sure you could find more uses for it. :)

The_Hat

9:15 pm on Apr 27, 2007 (gmt 0)

10+ Year Member



I would bet there is a PHP solution that would do it as well.

whoisgregg

9:24 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would require *some kind* of client side scripting. Either flash or javascript can do the trick. Then, the backend script just logs the information to a database.

Dabrowski

9:51 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



*Ahem*

Did someone say freelance coder?

Philosopher

9:06 pm on Apr 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow whoisgregg,

Above and beyond. Thanks man. Much appreciated.

colandy

12:31 pm on May 1, 2007 (gmt 0)

10+ Year Member



I already have one that determines if a user is still online. Uses AJAX with PHP & mySQL. DB has a timestamp, this updates every 2 seconds, when the timestamp is over 2 seconds old I know the user has left.

Not that hard but you do need AJAX.

(used in a chat app, once I determine that user has left, user name removed from user list and message displayed to say user has left)

whoisgregg

1:27 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Happy to help. :)