How to hide certain elements on my page based on referral traffic?

In particular, how do I hide ads? I ask this question after reading this: coding horror record

In it he claims

As a courtesy, disable ads for Digg, Reddit, and other popular referring URLs. This audience does not value advertising, and it is least likely to click them anyway.

I agree with what he says. So how do I do this?

+7
source share
3 answers

I would use PHP to do this, as the JavaScript code to hide the ads will make you look like you hide ads for everyone and earn revenue from them (Google is smart, so they will find you for something like this).

With PHP, however, you can change the page before it reaches the user, fixing this problem. Basically, you conditionally check where the browser came from:

<?php $sites = array("reddit.com", "digg.com"); if (!in_array(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST), $sites)) : ?> <div>your ads</div> <?php else:?> <div>Hello reddit person</div> <?php endif; ?> 

You will need to force your site to run PHP code (it will be dynamic) in order to conditionally display your ads. This code will not work as reddit not a url but you get the idea; check the reddit.com .

+2
source

Well, I don’t know how advertising is processed on your site, but in StackOverflow it can be something like

 function hideAds() { var elems = document.getElementsByClassName( "everyonelovesstackoverflow" ) for( var i = 0; i < elems.length; i++ ) elems[ i ].parentNode.removeChild( elems[ i ] ) } // change the logic as you like. You may need to parse document.referrer if( document.referrer == <some referrer url> ) hideAds() 
+1
source

You need to show ads if necessary, and not hide ads if they do not fit, otherwise you are likely to violate the AdSense terms, for example. hiding ads after they appear. At the very least, wait until the ads show until you check the referrer (conditions may also be violated, but much less likely). Also review the terms of your advertising service to ensure that the use of technology on the client side, such as javascript, to dynamically display ads does not contradict the conditions.

To answer your question, simply identify the HTTP Referrer (the field is actually called the HTTP "Referer", which is an error) and insert advertisements if it is not from websites that you "like." For API / examples, you can google for http referrer your_language . For example, in PHP it is $_SERVER['HTTP_REFERER'] . In python, this depends on the web structure you are using. As someone mentions in javascript, this is document.referrer , etc. Javascript is the easiest solution, but read the terms again.

0
source

All Articles