Conditional Advertising

I have entered the dark side. I now serve Google Adsense ads on my site. 🙂 I mentioned this idea last week and that I only wanted to serve contextual ads, and only to the 87% of people who were visiting my site via search engine referral. As it turns out, conditionally serving ads wasn’t very hard at all. I woke up early on Saturday and before my wife woke up, I had it all working.

To keep the code simple I determined whether or not someone was coming from a search enging by checking for “q=” or “p=” in their referring url. MOST search engines use one of these 2 variable names in their query string for the search terms.

Here’s how I set it up in PHP with Movable Type:

  1. Check for “q=” or “p=” in the referer and set a variable in the header include. Simplified PHP: Thanks Ryan.

    //Should we show Google Ads?
    $referer = $_SERVER["HTTP_REFERER"];
    $showads = preg_match("/[qp]=/i", $referer) > 0;
    
  2. Edit my MT Individual Entry Archive template to insert a “large rectangle” ad unit between every 15th comment in the comment loop.
    <?php if ($showads && <MTCommentOrderNumber> % 15 == 0){ ?>
    <script type="text/javasctipt>...</script>
    <?php } ?>
    
  3. Place another “skyscraper” ad unit in my sidebar.
    <?php if ($showads){ ?>
    <script type="text/javasctipt>...</script>
    <?php } ?>
    

That’s it. Pretty simple really. Google only allows you to display 3 ad units per page, but they control that themselves. So, even if you have 20 javascript ad sense requests on a page, Google will only render the first 3. So on pages that have over 45 comments, there will be ads below the 15th, 30th, and 45th comment and that’s it. On pages that have less than 15 comments, only the sidebar ad will show up.

Example of page with only comment ads:
Google “Popcorn Ceiling Removal” and click on my page.

Example of page with only sidebar ad:
Blingo Search “win at blingo” and click on my page.

I read through the AdSense Terms & Conditions and this type of conditional display of AdSense for Content blocks doesn’t seem to violate any rules. If anyone finds out somehow that it does, please let me know. I would recommend this technique to anyone who gets a lot of search engine traffic to specific posts on their personal site. I don’t think it’ll help you build your adsense empire (LOL), but I’ve been making a few bucks a day since I set it up.

8 Comments

  1. You could simplify the PHP a little bit:

    $showads = strrpos($referer, 'q=') ||
    strrpos($referer, 'p=');
    

    or even more by getting into regular expressions:

    $showads = preg_match("/[qp]=/i", $referer) > 0
    

    I think that expression is right.

    This page has a pretty good regex intro, they are crazy useful:
    http://weblogtoolscollection.com/regex/regex.php

  2. I did the same thing to my site over the weeked, and damn, you should see the money I’m rolling in!

    Seriously though, thanks for the tip on detecting a search engine. I just checked for google.com, but your way is easier and gets me more $$$$.

  3. So I’m only suppose to see an ad if I came from a search engine?

    Because I see one now in the sidebar and I came via Bloglines.

  4. Rolling in money Justin? Sounds like you’ve got AD FEVA! If I visit your site and see any ads for lowermybills.com I’m gonna have to hurt ya…with a Cowbell.

    Yea Chris, I fixed that. Ryan is right, use his second example with regular expressions. The problem is that the php strrpos looks for ANY of the characters in your string and returns the first result. Since I have a “p” in my url, it returned true. doH! I’ll fix the example.

  5. Sweetness. I might have to sign up for an adsense account…I’ve just always been a little put off by big companies who want my social security number.

  6. Yeah, I get sidebar ads from clicking through your rss feed in LJ.

    Doesn’t bother me, just thought you’d appreciate the feedback. Also, there are five of them….

    YOU WHOLE POST IS LIES!!1!one!!!

  7. that’s a genius idea. It totally avoids pissing off your regular readers while putting a tax on the passers through. Brilliant!

  8. Great idea! Thanks for the tip!

Leave a Comment

Latest Tweet
  • @mariafrey Same here. It’s funny because we started an physical calendar w/ the kids this morning and I didn’t think about it until noon. :)
Somewhat Recent
The Whole Nine Yards