I recently finished an exciting project where we implemented Google Ad Manager key values for ads based on the category a post is 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 complicated than that. Still, 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.

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 a post may have 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
<?php function webgilde_get_categories(){ if ( is_single() ) { $cats = get_the_category(); } else { $cats = array( get_category( get_query_var( 'cat' ) ) ); } $cat_slugs = array(); foreach( $cats as $_cat ){ $cat_slugs[] = $_cat->slug; } return $cat_slugs; }; ?>
  • 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 if you want to change the behavior about when which category is picked.

Let me say a few words about the used category slug. This usually is 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 permanence 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:

googletag.defineSlot('/123123123/ad-unit-by-category', [300, 250], 'div-gpt-ad-123123123123-0') .addService(googletag.pubads());

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

googletag.defineSlot('/123123123/ad-unit-by-category', [300, 250], 'div-gpt-ad-123123123123-0') .setTargeting('category', ['<?php if( function_exists( 'webgilde_get_categories') ) { echo implode( "', '", webgilde_get_categories() ); }; ?>'] ) .addService(googletag.pubads());

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 that you use for this key in Google Ad Manager. It might help you to remember later what setting you have made here.

Option to add new key-values in Google Ad Manager
Adding new Google Ad Manager key values

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
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 target ad units with the “news” value.

Example of Google Ad Manager key-values targeting
Google Ad Manager key-values targeting

That’s 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 have an All-Access license, you can use the Google Ad Manager Integration add-on for the smooth integration of your GAM ads. In the following video, you can see how easy it is and that you do not have to touch any code

Start video about Google Ad Manager Integration
Click on the preview image to load and start video from YouTube. By clicking you agree that your information will be sent to and processed by YouTube, LLC, 901 Cherry Avenue, San Bruno, CA 94066, USA. Read more