Hiding Ads from Paying Customers or Active Users

It's pretty common to use Google Adsense to serve ads on your site to generate supplemental income. But if you run a membership driven or e-commerce website, you may decide that you want to hide ads from those valuable users/customers and only show them to anonymous users. Doing so in Drupal is very easy, particularly if you have placed your ads in blocks.

Create the Premium Role

Depending on how your site is set up, you may be able to skip this step. For example, if you want to hide ads from all authenticated users then you can skip on down. But let's say you want to differentiate between authenticated users and paying customers and you only want to hide ads from those who have contributed financially to the well being of your business. Start by browsing to Admin/People/Permissions/Roles and create a new role. By default, this new role will inherit all the permissions that you have established for the authenticated users role, and that's fine for the purposes of this demonstration.

Create Your Ad Blocks

To use this technique you'll first need to create an AdSense block. Browse to your AdSense account, create a new ad unit (any size will do) and then click Get Code. Copy that code to your clipboard (CMD/CTRL-V) and then jump back to your Drupal administrative page. Click on Structure, Blocks, Add New Block. Show block by roleAt this point you can paste the code into the Body field, making sure to select the Full HTML formatting option, and then you can position the block in whichever region you're so inclined.

Now, let's only display this ad unit to anonymous and authenticated users. To do so, click the Roles option and select the two roles in question. The others will not see the advertisements.

Rules for Roles

While the previous modification works for existing users who are members of the paid user role, the goal here is to automate things a bit more so that when new users make a purchase they'll automatically be granted this role and, ultimately, have the ads hidden from their view. To handle that task we turn to the Rules module which, you may or may not have already installed depending on other modules that your site uses.

Once Rules is installed you can create a rule by clicking on Configuration/Rules. If you're not familiar with rules they're based on three actionable items: Events (what's happening to trigger your rule), Conditions (what specifically we're looking for in an Event), and Actions (the task that the rule is going to perform.) In this case we're going to build a rule that will add a role to a user but only after they've had an order reach the completed status. There are different ways that you can handle this such as firing the rule once a customer checks out, but there are situations where there may be a delay in processing during which time the customer could cancel and yet they've already been granted the role. So triggering the rule once an order gets to the completed status is the best bet in this case.

Creating the rule

Start by clicking on Add event and select "After updating an existing commerce order". Save the event and then click Add condition in the next section. Select "Data comparison" since we're checking an order status and either type or use the drop-down arrow to select "commerce-order:state" as the parameter. Finally, click Add action under the Actions area and select "Add user role", then select the name of the new role that you want to assign to the user.

Other Options

That's all there is to it! Now, when a user makes their first purchase they will be granted the elevated role which will subsequently hide any Google AdSense blocks from their view. If you don't have an e-commerce site but you still want to utilize this technique there are plenty of other triggers (Events, in Rules parlance) that you could use. For example, perhaps you want to elevate users who participate in your sites's forum -- you could fire the rule once they make their first post and then create another rule to remove the elevated role if they don't make a post in a certain length of time. The possibilities are endless!

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
8 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.


Do you have a question about something you saw here in our blog? Are you interested in a custom website for your business? If so, please contact us here.