Gossamer Forum
Home : Products : Links 2.0 : Customization :

Hit average instead of total hits.

(Page 1 of 2)
> >
Quote Reply
Hit average instead of total hits.
The biggest complaint I tend to get for my links is that the newer sites have no chance of catching up with the older sites with a heap of hits.

As a way to fix this, I thought it might be possible to modify the code to have an average hit count. For link sites that are really busy and have a heap of click thru's, you could work out an average for the day, and for link sites that don't get that much traffic you could do it weekly or monthly. Sites that don't have a days worth of clicks could just display a N/A or something.

Then using these new hit rates, you could make the Popular page based on total hits, average daily hits, or weekly hits (or all three maybe)..

Any sort of variation on this would interest me.

Regards,

Juz
___________________________
http://toonseek.sweeet.com
Quote Reply
Re: Hit average instead of total hits. In reply to
I have modified my Links to calculate the average hits per day to determine what is Cool/Popular. In nph-build.cgi, sub build_update_newpop, change:

Code:
# Check to see if the record is popular...
if ($values[$db_hits] >= $cutoff) {
print "\tUpdating record: $id, marking as popular ($values[$db_hits]).\n";
$values[$db_ispop] = "Yes";
}
else { $values[$db_ispop] = "No"; }

to read:

Code:
# Check to see if the record is popular...
my $days = &days_old($values[$db_modified]);
($days < 1) and ($days = 1); # avoid divide by zero error
my $hitsday = int($values[$db_hits] / $days);
if ($hitsday >= $cutoff) {
print "\tUpdating record: $id, marking as popular ($values[$db_hits]).\n";
$values[$db_ispop] = "Yes";
}
else { $values[$db_ispop] = "No"; }

I hope this helps.

[This message has been edited by Bobsie (edited May 18, 1999).]
Quote Reply
Re: Hit average instead of total hits. In reply to
Did all that and seems to work fine (ie, I didn't stuff up when making the mod!).. but there is a problem - nothing is marked as popular/cool any more.

The reason would be (I gather) is the bit where the popular cutoff is calculated.

My popular cut off is being reported as 472 hits - that's a total figure, the new calculated one is comparing a daily amount to the total amount of hits - so no one has that many hits in any one day so no one is being marked as cool/popular.

Looks like the section I need to modify is in:

# Sort the popular list, and set the cutoff mark.

But my perl knowledge is limited and there's a few things in there that I don't undestand!

If you could help with this slight modification too I'd appreciate it heaps!

Thanks!
Juz
Quote Reply
Re: Hit average instead of total hits. In reply to
In the section you are speaking of, I did make a mod. I should have included it with my earlier message but forgot it. I changed:

Quote:
($cutoff = $popular[$db_popular_cutoff - 1]);

to read:

Quote:
($cutoff = $db_popular_cutoff);

Then I changed $db_popular_cutoff in links.cfg to 1 (nph-build.cgi resets it to 2 in memory, though, while building). This was the only way I could get past the cutoff and eliminates the percentage basis for calculating the popular links.

With over 1600 links, I only have 22 cool links, ranging in age from 4 days ago to about a year ago, with hits ranging from a low of 13 to a high of 1103 (as I write this). The popular sites are strictly based on average hits per day since the links was added.

Note, if a link owner modifies a link, the modified date will change which will certainly skew the results of the calculation. The more hits there are for a lesser amount of days, the higher the average hits per day is going to be. Because of that, I modified my Links to add a field called DateAdded which is the date a record was added to the database and it never changes. I use that field to calculate the age of the link and, thus, the average hits per day on the link. The link owner can modify the link all s/he wants to, it will not affect the calculation and results in a truer picture of the average hits.

I hope this helps.

[This message has been edited by Bobsie (edited May 18, 1999).]
Quote Reply
Re: Hit average instead of total hits. In reply to
By making the $db_popular_cutoff = 1 (which it then modifies itself to 2) does that mean any site that gets atleast 2 hits a day from you will get classed as popular?

I did it like that and had 140 popular sites Smile and a page with way too many links on it.. hehehe

What I was wondering was:

($cutoff = $popular[$db_popular_cutoff - 1]);

From my limited programming knowledge, I presume that's referring to the an array called popular.. and element number cutoff-1 (being that the first one is 0 etc) is the record that the cut off point falls upon...

Is there anyway to use that to get the total hits for $popular[cutoff-1] as well as how many days old it is? - if so, you could work out the correct cut off hits amount. That way my top 15 would stay as the top 15.. Smile

Hope you understand that.. basically:

You'd want to find out the (in my case) 15th most popular site.. find out it's hits and how many days it is, average the hits out (hits/days) and use the result as a cut off..

Is the cut off calculated before or after the new code makes it a daily average?.. To be accurate it would have to be..

Now I'm confusing myself. I'll leave it at this and hope you manage to decipher what I mean!

thanks
Juz


Quote Reply
Re: Hit average instead of total hits. In reply to
  
Quote:
By making the $db_popular_cutoff = 1 (which it then modifies itself to 2) does that mean any site that gets at least 2 hits a day from you will get classed as popular?

Yes, that is the case. Of course, you don't need to set $db_popular_cutoff to 1. You can set it to a higher number, say 10.

Quote:
I did it like that and had 140 popular sites and a page with way too many links on it.

That is probably because a lot of those sites had a modification done which makes the date of the link more current than it would have been otherwise, or there are a lot of popular sites, or the cutoff is too low. Read on for more about this.

Quote:
($cutoff = $popular[$db_popular_cutoff - 1]);
From my limited programming knowledge, I presume that's referring to the an array called popular.. and element number cutoff-1 (being that the first one is 0 etc) is the record that the cut off point falls upon...

Yes, there is an @popular array built just before the section you are speaking of. It is arranged by number of hits, in descending order, I believe. $db_popular_cutoff-1 marks the point in the array where the cutoff is determined. The "-1" is used because the array is zero-based, so the 10th element is element 9 in reality (the first element being element 0).

So, if $db_popular_cutoff is set to 10, all links that have at least the number of hits specified in the 9th element of @popular are considered popular. The way the unmodified code was written, this should be the 10 resources with the most hits.

Applying the "average hits per day" mod to the $cutoff calculation changes everything. Setting $db_popular_cutoff to 10 with that mod does not necessarily mean that only the top 10 links will be listed. In this case, it means that a link must have at least an average of 10 hits per day to be considered popular. By adjusting that number up or down, you can decrease or increase the number of links displayed on the Cool page. You just need to find the setting you can live with, and then use it.

As the age of the links increases, the average number of hits per day decreases (unless they really do get that many hits per day on average) up until the point where the modified date of the link changes to a more recent date. When that happens, the average hits per day for that link will jump dramatically. For example, if a link 2 years old had 2000 hits, the average hits per day is 3 (2.7 rounded up). If the modified date changes to today's date, guess what? The average hits per day just changed to 2000 for that link and it is going to appear in the Cool page. In 7 days, assuming no more hits on the link, it will still have 286 hits on average per day, and 67 hits on average per day after a month has elapsed.

That's why I mentioned the 'DateAdded' field I added to links.def/links.db. It contains an unchangeable date that represents the date the record was added and is used to calculate the link's age, instead of the modified date. Using it, you get a truer average hits for day for each link no matter how many times the link has been modified.

One more point before I finish. If you change over to the "average hits per day" then there is no real way of listing just the top 15 as the mod is currently written. To do that would require further modification, maybe just grab the first 15 links in the @popular array and set "isPopular" to "Yes" for those links, and all other links to "No". I am not sure if that would work or not.

I hope I have made things a bit clearer to you and that some of this helps.

[This message has been edited by Bobsie (edited May 19, 1999).]
Quote Reply
Re: Hit average instead of total hits. In reply to
Bobsie, thanks heaps for the time you take to help out - really appreciated!

Now I noticed your modified "3 levels of new + updated" modification - is it possible for me to just follow the steps there to add the date_added field and then use your script to update my database contents to have the new field??

I'm assuming it would work fine..

So then, what would be the code (using the newly added date_added field (not 100% sure of the name you gave that field, so use the one you used of course!)to do the daily average hits thing?

If you have the time to fill me in on the details I'd appreciate it - if not, that's ok!

Juz
Quote Reply
Re: Hit average instead of total hits. In reply to
 
Quote:
Now I noticed your modified "3 levels of new + updated" modification - is it possible for me to just follow the steps there to add the date_added field and then use your script to update my database contents to have the new field??

Yup! The DateAdded field I have been talking about in this thread and the one in the graphics mod are one and the same field. Be aware, though, that the script will use the current "Date" field to build the "DateAdded" field. So, if any resources have been modified recently (or since they were first added) by the link owner, the DateAdded field will contain erroneous dates and will skew the "average daily hits" calculations. The only way around that is if you kept copies of all the email you received when the owner added the link, you could use those to adjust the DateAdded fields individually after running the script.

Quote:
So then, what would be the code (using the newly added date_added field (not 100% sure of the name you gave that field, so use the one you used of course!)to do the daily average hits thing?

All the code is in this thread, although not necessarily all in one message. See my first and second messages above.

I hope this helps.
Quote Reply
Re: Hit average instead of total hits. In reply to
Bobsie,

I just want to clarify one thing. If I have installed your "3-Level New" mod, do I still use the exact code in this thread for the Hit Average Mod?

You said that by using the DateAdded field (which I have added because of the 3-Level New Mod) instead of the date modified, the problem with the hit average jumping up can be avoided. So do I have to change anything in the code you posted in this thread?

Thanks in advance.
Quote Reply
Re: Hit average instead of total hits. In reply to
t_dog,

As far as I know, the code in this thread works with the new 3-Level New & Updated mod. I use it on my system that way. But, let me know if you run into any trouble.
Quote Reply
Re: Hit average instead of total hits. In reply to
 
Hi bobsie...

I did the above to make my pages calculate the average instead of the total... my question is, the pages would still display the total, right? it would just the average as the basis for cool sites.... is there a way to display the average/day as well?

thanks.
Quote Reply
Re: Hit average instead of total hits. In reply to
The code only affects how many links are displayed on the Cool page. It has no affect on the other totals in Links.

You should be able to display the hits per day for the displayed links. You would probably need to do this in sub site_html_link though. Just do the same calculation that was done in nph-build.cgi, something like this:

Code:
my $days = &days_old($rec{'DateAdded'});
($days < 1) and ($days = 1);
my $hitsday = int($rec{'Hits'} / $days);

You can then use $hitsday in the site_html.pl, sub site_html_link to display the information as part of the link. If using templates, you need to pass the variable to the template file, link.html, via hitsday => $hitsday, and then use <%hitsday%> in link.html.

I hope this helps.

[This message has been edited by Bobsie (edited June 18, 1999).]
Quote Reply
Re: Hit average instead of total hits. In reply to
 
I'm not that well versed in programming... could you elaborate more in layman's terms on how to do that? What I would like to do is to display the average hits per day on all records... including on the what's cool page...

this way, nobody would complain to me that their site has more "total hits" than some of the ones that are listed on the "what's cool" page...

thanks!
Quote Reply
Re: Hit average instead of total hits. In reply to
Bobsie,

I am still getting a divided by zero error.

Code:
my $hitsday = int($rec{'Hits'} / $days);

correct? But I thought that this line

Code:
($days < 1) and ($days = 1);

turns all "0 days" into "1 day".

What is wrong?
Quote Reply
Re: Hit average instead of total hits. In reply to
 
Thanks Bobsie!

I wasn't using DateAdded so I changed it to Date... I wish I knew as much as you... once again, you solved my problem

Thanks again.
Quote Reply
Re: Hit average instead of total hits. In reply to
rp,

Put the code I mentioned in sub site_html_link of either site_html.pl or site_html_templates.pl. Note that I have modified the code I posted earlier because of the divide by zero error mentioned by t_dog.

Now, if you are not using templates, then in the same routine, you could replace this:

Code:
return &load_template ('link.html', {
detailed_url => "$db_detailed_url/$rec{'ID'}$build_extension",
%rec,
%globals
});

to read:

Code:
return &load_template ('link.html', {
hitsday => $hitsday,
detailed_url => "$db_detailed_url/$rec{'ID'}$build_extension",
%rec,
%globals
});

You could then use <%hitsday%> in your link.html template where you wanted to display the average hits per day, something like:

Quote:
Avg Daily Hits: <%hitsday%>

t_dog,

Yes you are right. That should eliminate the divide by zero error. The only problem is, I neglected to include that code in the site_html/_template.pl code. It is there now. Thanks for pointing it out to me.
Quote Reply
Re: Hit average instead of total hits. In reply to
 
Okay.. now it's bogging my mind... I did the above but when I try to display the ave. hits, it doesn't do it.

It only displays:

Average Hits Per Day:

It doesn't even display a zero on it... Any ideas Bobsie? I know I have most of it right.. just probably missing something minor... The script is calculating the average fine... otherwise I would get a few more cool sites... But I just can't get it to dispay it... Check it out.

http://www.rplink.com or http://216.221.189.246

Thanks
Quote Reply
Re: Hit average instead of total hits. In reply to
 
here is what i have in my site_html_templates.pl :

sub site_html_link {
# --------------------------------------------------------
# This routine is used to display what a link should look
# like.

my %rec = @_;

# Set new and pop to either 1 or 0 for templates.
($rec{'isNew'} eq 'Yes') ? ($rec{'isNew'} = 1) : (delete $rec{'isNew'});
($rec{'isPopular'} eq 'Yes') ? ($rec{'isPopular'} = 1) : (delete $rec{'isPopular'});

my $days = &days_old($rec{'DateAdded'});
($days < 1) and ($days = 1);
my $hitsday = int($rec{'Hits'} / $days);

return &load_template ('link.html', {
hitsday => $hitsday,
detailed_url => "$db_detailed_url/$rec{'ID'}$build_extension",
%rec, %globals
});
}


Quote Reply
Re: Hit average instead of total hits. In reply to
  
Quote:
It doesn't even display a zero on it.

It did when I looked at it. Did you refresh/reload the page after re-building?

Also, your links are so new, there is none that really would have an average except for the one that is Cool... but even that one shows a zero. Are you using the 'DateAdded' field in your links database? If not, change:

Quote:
my $days = &days_old($rec{'DateAdded'});

to read:

Quote:
my $days = &days_old($rec{'Date'});

I hope this helps.

[This message has been edited by Bobsie (edited June 18, 1999).]
Quote Reply
Re: Hit average instead of total hits. In reply to
I am glad it is working for you now. Be aware, though, that using the 'Date' field instead of the new 'DateAdded' field has a pitfall. If someone modifies a link, the 'Date' field will change to the date it was modified. Since the 'Hits' field has not been reset as well, the average hits per day are going to be sky-high for that record and will show that link as about the most popular even though it may not really be. That is why I use an unchangeable 'DateAdded' field.

[This message has been edited by Bobsie (edited June 19, 1999).]
Quote Reply
Re: Hit average instead of total hits. In reply to
Hi,

Whenever I tired to display $hitsday on links, it showed 0. And every link had the same value.

I used code in sub site_html_link(site_html.pl) as follows;

my $days = &days_old($values[$db_modified]);
($days < 1) and ($days = 1); # avoid divide by zero error
my $hitsday = int($values[$db_hits] / $days);

I also tried to replace "&days_old($values[$db_modified]);" part for the one Bobsie suggested. But, it also showed 0.
So, I checked what I got for "$days".
10763.
For every link, I got 10763 as "$days".
Even if I modified db,10763 stayed.
10763 was showed up for the newly registered, too.
This is why I had 0 for $hitsday.
Anybody knows why I always have 10763 for $days regardless when or which?

Two more questions,
1)Bobsie pointed out the Date field would change after modifying a link.
However, mine always stays.

2)My top tens are sorted by total hits.
How do I make them be sorted by daily average hits? I think I should change $link{'Hits'} in sub build_rate_page(nph-build.cgi).

My nph-build.cgi and db_utils.pl are saved as nph-build.txt and db_utils.txt at http://clubrich.com/temp.

Thanks,

sofine
Quote Reply
Re: Hit average instead of total hits. In reply to
sofine,

Quote:
I used code in sub site_html_link(site_html.pl) as follows;

my $days = &days_old($values[$db_modified]);
($days < 1) and ($days = 1); # avoid divide by zero error
my $hitsday = int($values[$db_hits] / $days);

The is the code you would use in nph-build.cgi, not site_html.pl. The site_html.pl code is:

Quote:
my $days = &days_old($rec{'Date'});
($days < 1) and ($days = 1);
my $hitsday = int($rec{'Hits'} / $days);

If a resource is modified by a user, the Date will change. If a resource is modified in the admin screen, the date does not change.

I hope this helps.

[This message has been edited by Bobsie (edited June 22, 1999).]
Quote Reply
Re: Hit average instead of total hits. In reply to
Thank you Bobsie.
You have solved two of my problems.
Now I see the daily avg hits and know why Date does not change.


Somebody said a mod in db_utils.pl made links be sorted by daily average hits.
The suggested code was as follows;

sub build_sorthit {
my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, $hit, $i, @sorted);

for ($i = 0; $i < $num; $i++) {
$days_old = int &days_old ($unsorted[$db_modified + ($i * ($#db_cols+1))]) &#0124; &#0124; 1;
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))] / $days_old;
}
foreach $hit (sort { return $sortby{$b} <=> $sortby{$a} } (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

But, as a result, I can not define what the order of links is. All links seems to be randomly showed up.
Could you please point out what is wrong?


I also want top rated links to be sorted by daily avg hits. I tried to modify sub build_rate_page in nph-build.cgi without any luck. What should I use instead of $link{'Hits'}?

Thanks again.

sofine
Quote Reply
Re: Hit average instead of total hits. In reply to
I have been reading this thread over and over, but I don't seems to get it right. I made the changes to NPH build according to Bobsie's lines, but I must be missing something... I changed the pop cutoff in links.cfg to 2 and even resetted my hits. No sites are appearing in the pop cutoff list...
Could someone post again the necessary steps, one by one? Thank you.
This is what my script looks like:
Quote:
# Check to see if the record is popular...
my $days = &days_old($values[$db_modified]);
($days < 1) and ($days = 1); # avoid divide by zero error
my $hitsday = int($values[$db_hits] / $days);
if ($hitsday >= $cutoff) {
print "\tUpdating record: $id, marking as popular ($values[$db_hits]).\n";
$values[$db_ispop] = "Yes";
}
else { $values[$db_ispop] = "No"; }
and
Quote:
# Sort the popular list, and set the cutoff mark.
@popular = sort { $b <=> $a } @popular;
($db_popular_cutoff < 1) ?
($cutoff = $popular[int($db_popular_cutoff * $#popular)]) :
($cutoff = $db_popular_cutoff);
#($cutoff < 10) and ($cutoff = 10);
Quote Reply
Re: Hit average instead of total hits. In reply to
I'd like someone to post all that we need to do, step-by-step, to be absolutely clear on what to do, too. Because after reading all the above, I am confused as h*ll.

Also, someone tell us how to add fields?

This is not the only mod that I'd like to do, but will probably not end up doing because it's so bloody confusing!!! Frown

*whimper*

------------------

--Sanguinarius
The Real Vampire Directory
www.sanguinarius.org/cgi-bin/links/pages/
> >