Link to home
Start Free TrialLog in
Avatar of nlesser
nlesserFlag for United States of America

asked on

Need a Web Page Hit Counter that works on an intranet...i. e. no outside service provider allowed.

I would like to deploy some invisable hit counters on pages of a new corporate intranet site and have their counts liked back to a central page with visable counters showing the activity.  Ideally, I would be able to resent all the counters with one press of the button.

Is there a JavaScript that will do this?

Neil
Avatar of pinaldave
pinaldave
Flag of India image

Hi nlesser,
I have used this yrs before. I am sure this still works.
<!-- This script has been in the http://www.javascripts.com Javascript Public Library! -->
<!-- Note that though this material may have been in a public depository, certain author copyright restrictions may apply. -->
<html>
<head>
<title></title>
</head>

<body>
<script language="Javascript">

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
      break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie(name) {
  var exp = new Date();
  FixCookieDate (exp); // Correct for Mac bug
  exp.setTime (exp.getTime() - 1);  // This cookie is history
  var cval = GetCookie (name);
  if (cval != null)
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var expdate = new Date();
var num_visits;
expdate.setTime(expdate.getTime() + (5*24*60*60*1000));
if (!(num_visits = GetCookie("num_visits")))
  num_visits = 0;
num_visits++;
SetCookie("num_visits",num_visits,expdate);

</script>

<script language="Javascript">


document.write("You have been to this page "+num_visits+" times.");


</script>The code<font color="black"><br>
<br>
<br>
<b><pre>
<script language="Javascript">

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
      break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie(name) {
  var exp = new Date();
  FixCookieDate (exp); // Correct for Mac bug
  exp.setTime (exp.getTime() - 1);  // This cookie is history
  var cval = GetCookie (name);
  if (cval != null)
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var expdate = new Date();
var num_visits;
expdate.setTime(expdate.getTime() + (5*24*60*60*1000));
if (!(num_visits = GetCookie("num_visits")))
  num_visits = 0;
num_visits++;
SetCookie("num_visits",num_visits,expdate);
//-->
</script>

<script language="Javascript">


document.write("You have been to this page "+num_visits+" times.");
//-->

</script>
<br>
MY page is <a href="http://www.rsl.net/ronjon">here!
<br>

</body>
</html>
   
<!-- Tommy Raven (Min) says Moo. -->



Regards,
---Pinal
Avatar of nlesser

ASKER

Pinal, is the above code the counter that goes on the page being measured for hits?  
If so, here's what I'm missing:
How do i get it to display the count on a central page containing several displays.
Avatar of nlesser

ASKER

Pinal, I see now there is more than one JavaScript.  Which script does what.  I pasted the first into the body of a blank page and it showed the script rather than an invisable counter.   What am I missing??
SOLUTION
Avatar of webwoman
webwoman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The above script works only as long as the user doesn't delete the cookies, if he/she does then the counter will be reset to 0.

You can also use ASP or PHP to have a counter that stores the hits in a file, so nothing happens when cookies get deleted... ;)

If you need a ASP, PHP counter let me know, I'll provide one.

Hope this works...
Hi pinaldave,
Sorry my friend,
Copy everything from <html> to </html>


It should work for you.

<html>
<head>
<title></title>
</head>

<body>


<script language="Javascript">


document.write("You have been to this page "+num_visits+" times.");


</script>The code<font color="black"><br>
<br>
<br>
<b><pre>
<script language="Javascript">

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
      break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie(name) {
  var exp = new Date();
  FixCookieDate (exp); // Correct for Mac bug
  exp.setTime (exp.getTime() - 1);  // This cookie is history
  var cval = GetCookie (name);
  if (cval != null)
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var expdate = new Date();
var num_visits;
expdate.setTime(expdate.getTime() + (5*24*60*60*1000));
if (!(num_visits = GetCookie("num_visits")))
  num_visits = 0;
num_visits++;
SetCookie("num_visits",num_visits,expdate);
//-->
</script>

<script language="Javascript">


document.write("You have been to this page "+num_visits+" times.");
//-->

</script>
<br>
MY page is <a href="http://www.rsl.net/ronjon">here!
<br>

</body>
</html>


Regards,
---Pinal
Hi nlesser,
now you can change things and put the things between the body things in your codes body.
also
document.write("You have been to this page "+num_visits+" times."); will display the counter.


Regards,
---Pinal
Avatar of nlesser

ASKER

I want the count to display on a central page with the collection of counts displaying. I don't see the code for the central page and how it's linked to obtain the count to display.  
You have to record the counts on a database and then generate the sount summary page from the DB.  You can't just monitor across desktops.  The counts have to be stored on the server.

How you implement it depends on what database and server side scripting language you use.  You are not going to do it with just client side scripting as is being suggested here.

Cd&
well said CobolDinosaur. I agree with him. There are so many other things to consider and include here. We have to see your application and many other parameters to consider. The script mentioned is also desktop version and goes by cookies. You need to have separate file which counts hits or database.
Other issues are also about locking and other things. Once you start implementing things we can work with you to make it better application.
Regards,
---Pinal
A flat file will be enough for a counter...
Avatar of nlesser

ASKER

I don't think I've explained this correctly.  Your solutions seem too complex for such a simple need.  

An internal website consisting of about 50 pages, 5 of which are key subject areas.  I want to accumulate counts on the number of hits on the 5 pages.  I want to see these counts on a private page only I see.  I'm told it's possible to install a counter on each of these 5 pages and reduce the size to a head of a pin and white it out so it isn't seen or noticed unless you happen to move your cursor directly over the counter.  The counter is linked backc to my private counter page where the counts can be read out.  Every time one of the 5 pages is open that page's counter increments.

I'm relatively new to web publishing so the above may sound naive.  I, too, am a cobol dinosaur so I could be "out to lunch" on this one.  What say experts??

Neil
sir you can do it if you use image there as counter. You will be not able to see the image till you highlight that and it will work as counter. You can count how many times the image is displayed.
My friend is using the counter with same color as background. Smiles,
---Pinal
Avatar of nlesser

ASKER

Ok. Color me dense.  Specifically, what code do I put on each of my 5 pages and what code do I put on my page that displays the counts?
Avatar of b_smith_79
b_smith_79

Every web server has invisible hit counters built in, they are called access logs. Hit counters are unreliable, clunky and require you to embed code in every page. No good! They're strictly for amateur public websites where you don't have access to the logfiles.

You do have access to them! So use them! Get yourself a decent piece of log parsing software and you are set. Soooooo much easier.
Didn't I say that? WAYYYY far back there, at the very beginning?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nlesser

ASKER

I totally concur.   I just didn't know how to split points after the fact.  Hearing Web Woman's suggestion later repeated led to my investigation of server log reporting which does the job for me just fine.  If you can handle the sharing, I'd appreciate it.  
P. S.  Please tell me how this is done so I can correct any further situations I might inadvertantly create.

Thanks for bringing this to my attention.

Neil