Showing ads only to 20% percent of visitors. Or different ads to another 80% visitors

614 views Asked by At

How can I showing ads to only to 20% percent of visitors in a PHP file? Or showing different ads to another 80% visitors?

Any way that can work, maybe using time intervals, like the visitors that visit the site at minute 0-12 only that show it, and visitors that visit the site at minute 13-59 is not or another method would work.

Please help or share if you have, this should be easy enough and many people should need it, already googling it but still can't find it

3

There are 3 answers

2
Stegrex On

One possible, simple solution using random number generator:

$randInteger = rand(1, 10); // Generate a random number in variable $randInteger.
if ($randInteger <= 2) // If the integer is 1 or 2, show the 20% ad.
{
    showTwentyPercentAd();
}
else if ($randInteger >= 3) // Otherwise, if the integer is 3-10, show the 80% ad.
{
    showEightyPercentAd();
}

Hopefully, this is clear how the code is structured.

0
Yannick Blondeau On

I would simply go with a random number:

if (rand(0, 100) <= 20)
{
  ShowAd(1);
} else {
  ShowAd(2);
}
0
Roet On

Try searching SO?

$adPercent = 20;
if (rand(0, 100) < $adPercent) {
   echo '<div class="ads">Buy now!</div>';
}

Got this from here:

Display Ads To % of Users