katlis Posted July 11, 2008 Share Posted July 11, 2008 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. Quote Link to comment Share on other sites More sharing options...
sasa Posted July 11, 2008 Share Posted July 11, 2008 $url = rand(1,100)<=30 ? $url2 : $url1; Quote Link to comment Share on other sites More sharing options...
katlis Posted July 11, 2008 Author Share Posted July 11, 2008 Thanks, but how does this keep track of the trend, assuring a 30% linkout to url2? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 It doesn't. You're going to have a hell of a time ensuring 30% because of fluctuations in traffic. You'd be better off just doing what sasa wrote because it'll be pretty close based on probability. Quote Link to comment Share on other sites More sharing options...
katlis Posted July 11, 2008 Author Share Posted July 11, 2008 Not lookin for "pretty close", but thanks for the help. Any other ideas? Clicks to url1 and url2 will be tracked in a table... the function should just try to get to a 30% rate every hour. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 11, 2008 Share Posted July 11, 2008 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) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 Yeah, but eventually he'll have like 29.3% or something and no one will click again by the end of the hour and he won't exactly have 30%. I'd really just use sasa's method, but whatever. =/ Good luck with that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.