Jump to content

Formula for redirecting users to a page 30% of the time


katlis

Recommended Posts

I have a link that I need to track clicks on, and have the users be directed to $url1 or $url2. I only want the link to go to $url2 30% of the time. So I figure I need to track the times each url is visited... but how do I calculate the link randomness with a percentage weight? I'm thinking I want to have the script calculate this every hour, and try to meet 30% every hour. Any ideas?

 

I appreciate any help!

 

Forgot to mention I will be tracking clicks with MySQL.

I agree with the above comments as well. That method will result in 30% ratio in the long run. But, since you are tracking in a database and don't feel that is sufficinet, here is another method.

 

When the page with the link in question loads, run a short script to get the percentage of times url 1 vs url 2 have been visited and set the link accordingly.

 

This example makes several assumptions as I do not know the structure of your tables.

<?php

//Query the link hits by URL
$query = "SELECT count(*) FROM table GROUP BY url ORDER BY url";
$result = mysql_query($query) or die (mysql_error());

//Set the count for each url from the query results
//Assumes url1 comes before url2 in the results
$url1_count = mysql_result($result, 0);
$url2_count = mysql_result($result, 1);

//Set the url for this page load
$this_url = (($url1_count/$url2_count)<2.33)?'url1.htm':'url2.htm';

//Create the link
echo "<a href=\"$this_url\">The Link</a>";

?>

 

If you are wondering why I used 2.33, it is mathmetically the equivalent to taking the count of link 2 and dividing by the total of link 1 and link 2. It is more efficient doing it that way as it involves less math operations (one division vs an addition and a division).

 

Ex: Link 1 = 7 hits, link 2 = 3 hits

 

3 / (7 + 3) = .30

7 / 3 = 2.33 (repeating)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.