I recently finished a very interesting project where we implemented key-value targeting for ads in Google Ad Manager based on the category a post as belonging to. In other words: such a setup would allow you to sell and display ads for specific topics. An automotive company could take over the Car category or you can add your Amazon fallback affiliate ads in posts in the Books category.

The client’s project was a lot more complex than that, but I thought that category-targeting for Google Ad Manager might interest a broader audience, so here is the basic setup to start implementing it in WordPress.

You're using WordPress and would like to get Advanced Ads for free?

What I used here

  • The site is based on WordPress, so the tutorial doesn’t work for other systems
  • Installed and used my Advanced Ads WordPress plugin
  • We used inline tags only (no header code needed), but the tutorial applies to traditional codes, too

How to use the WordPress post category for key-values targeting

PHP code

First, we need to implement a few lines of code in WordPress, to get the categories from the post.

I am saying categories because it is possible that a post has more than just one.

  • Log in to your WordPress Dashboard
  • Go to Advanced Ads > Ads and create a new ad
  • Enter a title and choose the Plain text and code ad type and add the code from below to the code field

  • Enable the Allow PHP option
  • Click on the Publish button at the top right
  • Go to Advanced Ads > Placements and create a new HEAD ad and assign the just created ad to it

The code above collects the category slugs belonging to the current post or the category if this is a category archive. It ignores any hierarchy. In the client project, we picked categories only from specific hierarchies. You can edit this code or reach out to me for help if you want to change the behavior about when which category is picked.

A word on the used category slug. This is normally your category name converted into a string that could be used in the URL. E.g., if your category name is “Car”, the slug would be just “car”. If you change your category name later, the slug might persist. This is good for SEO purposes. You could rewrite the code above to use the category name instead.

Google Ad Manager ad unit tags

Next, we need to adjust the code used from Google Ad Manager to send the key-value pair.

The key can be anything. We will just call it “category”. The value is the category slug.

Our original Ad Manager codes will contain lines like this:

We are going to add our key-value targeting code directly between these two lines.

What does it do?

This code uses the function we implemented above to send the categories to Google Ad Manager. You can use that information to target ads as shown in the next step.

Your first step to increase your ad revenue

Subscribe to my monthly newsletter and get additional ad optimization tips to boost your ad revenue. It is free and you can opt out at any time.

Manage key-values in Google Ad Manager

Now that you are sending the category or categories to Google Ad Manager, you might also want to use them when displaying your ads.

In your Ad Manager account, go to Inventory > Key-values.

Click on New Key.

Enter the Name, which must be identical to the key you are using in your ad code. Mine from above was category.

Enter a Display name, which is used for this key in Google Ad Manager. It might help you to remember what it was about.

Option to add new key-values to Google Ad Manager

The Values type option depends on whether you want to manage a specific list of available values to choose from in Google Ad Manager or enter them when you select key-values for targeting later. Predefined values make sense if there is a limited number of values, like with post categories.

Click on Add Values to add some. In the example below, I added three categories.

Google Ad Manager values example

Click on Apply to save the values.

Click on Save to save the key-value options.

Using key-values in Google Ad Manager

You can use key-values for targeting line items.

Go to Line items > {Choose the line item} > Settings.

Go to the Add targeting section and click on key-values.

Now you can choose the key (e.g., “category”) and select the values. You will find a list of choices from the key-values you set up earlier here. If you investigate the available options you will see that you can also choose to target only ad units that don’t have a specific value or use string operators like begins with here.

In the example below, I just target ad units with the “news” value.

Google Ad Manager key-values targeting

This is it. The line item with that category will be displayed only for assigned ad units that also have the key “category” with the value “news” in it.

What else you need to know

If you are a user of our Advanced Ads Pro plugin then you might want to disable cache-busting for the placement that use this technology. If you need that feature then let us know.

Do you need more help integrating Google Ad Manager on your site? Reach out and let me know how I can help.

The Author

Leave a Reply

Your email address will not be published. Required fields are marked *