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.

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



    Hi Thomas,
    Congratulations for your oustanding blog.

    One question, if the category in the page is updated due to user interaction through ajax, so some part of the page content change and the rest (header, rest of content,…) remains the same, could it be refreshed the ad on the ajax callback (in order to accomodate to the new category)?

    Is it posible with Google Ad Manager (I gues not with adsense)?

    Many thanks and keep up the great job

      1. Moisés

        Hi Thomas,

        Thank you very much for your answer. In the link you have provided I think I have the answer: since the user interaction in my page is through clicking a button I guess I can ask for a different add (In the link you have provided it states that a new add can be downloaded when user clicks a button).

        Just a last question, If you were to implement ads based on the category the user is watching, what do you think is more appropiate : using adsense custom channels or Google ad manager (as in the example described in your article: Using the post category in WordPress for key-value targeting in Google Ad Manager )

        Thanks a lot for your time.

        fyi: I posted related questions in 3 different blogs and you were the only one answering and THE SAME DAY!!!. Congratulations.

        1. Thomas Maier Article Author

          Happy to help.
          AdSense and Ad Manager are two different services. AdSense delivers ad content, while Ad Manager is more about managing and placing ads. So for controlling which ad codes to show where, I would use Google Ad Manager or a dedicated software like our Advanced Ads plugin. If you are happy with the support here then you will love our plugin support as well.

          1. Moisés

            Hi Thomas,

            Thanks for your comments.

            Now I am centered on going live with the web, then I have to study the options deeply.

            Best regards