0

How to hide adsense ads to visitors coming from specific campaign?

Until now, I've found the way to hide ads when the URL contains the word "share"

For example:

it won't show the adsense ad if the landing page of my campaign is:

https://example.com/?utm_source=social&utm_medium=share 

I use the following code to do that:

<?php
$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'share') !== false) {
    return false;
} else { ?>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-0000000000000000"
     data-ad-slot="0000000000"
     data-ad-format="link"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php } ?>

The problem is, when the visitor goes to another page within my website, the utm parameter is removed from the URL, and adsense ad show up again.

I need your help to make sure any visitor coming from campaign

?utm_source=social&utm_medium=share don't see any adsense ad, no matter if the visitor navigate my entire website, the visitor should not see the adsense ad.

If you know another workaround for this problem, something more simple, please share it. I will forget the code above and use yours.

Thank you in advance for your responses.

Regards

sshcli
  • 11
  • 6
  • 1
    You want to read the $_GET variables and store them in a session on all your pages, perhaps just include a file that does this at the top of all your pages so you don't have to re write it. Also within that import, if there are no get variables set, check the session – iJamesPHP2 Feb 12 '20 at 15:45

1 Answers1

0

You need to include a file on top of all your pages that checks for those parameters and stores them in a session, try this:

<?php
session_start();

if (isset($_GET['utm_source'])) {
    $_SESSION['utm_source'] = $_GET['utm_source'];
}
if (isset($_GET['utm_medium'])) {
    $_SESSION['utm_medium'] = $_GET['utm_medium'];
}
if (
    (isset($_SESSION['utm_source']) && $_SESSION['utm_source'] == 'social') &&
    (isset($_SESSION['utm_medium']) && $_SESSION['utm_medium'] == 'share'))
{
    // DON'T SHOW ADS
} else {
    // SHOW ADS
}
iJamesPHP2
  • 524
  • 4
  • 13
  • This will overwrite the variables on each page load. It needs to see if the session variables exist first. – aynber Feb 12 '20 at 15:51
  • @aynber Yes, I spotted that as soon as I posted it, I have revised it, if you could please amend your downvote it would be appreciated. – iJamesPHP2 Feb 12 '20 at 15:53
  • Wasn't my downvote, I just commented. And that still looks extremely convoluted. – aynber Feb 12 '20 at 15:55
  • @aynber Convoluted how? – iJamesPHP2 Feb 12 '20 at 16:01
  • Well, that's not how ternaries work. A ternary will return a value based on the T/F of the left expression. Actually setting a value inside of the true value may not work. https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary – aynber Feb 12 '20 at 16:05
  • 1
    @aynber It worked on my machine but yes, you are correct, it is an incorrect use of ternarys, I have edited. – iJamesPHP2 Feb 12 '20 at 16:09
  • @iJamesPHP2 it wasn't my downvote either, it must be a hater somewhere. I'm considering to try your solution, because is the only answer I have until now. Btw, thanks for the answer. – sshcli Feb 12 '20 at 16:10
  • @sshcli If this project is a dynamic website with a lot of backend interaction, I politely suggest looking into PHP frameworks such as Laravel, it is great even for static websites – iJamesPHP2 Feb 12 '20 at 16:12
  • Thanks @iJamesPHP2 your solution works! I have upvoted for your answer. Now I only need to find the way to delete the session when the visitor closes the browser. Because if the same visitor return to the website directly (Not via social share campaign), the visitor will not see any ads, and I want to show ads always, except when coming from campaign social share. – sshcli Feb 12 '20 at 20:12
  • @sshcli Have a look here: https://stackoverflow.com/questions/24402047/php-session-destroy-after-closing-browser/24402832 – iJamesPHP2 Feb 12 '20 at 20:25
  • @iJamesPHP2 very useful. My problem is solved now. Thanks a lot ! See you around – sshcli Feb 13 '20 at 03:37