Skip to main content

Overview

This guide walks you through activating Permutive contextual cohorts in Google Ad Manager (GAM) for ad targeting. The implementation calls the Contextual API directly (without the Permutive SDK) and passes the returned cohort codes to GAM as key-value targeting. This approach is designed for environments where user consent is not available, allowing you to target ads based on page content without processing user data.
Prerequisites:
  • A Permutive workspace API key
  • Admin access to Google Ad Manager to configure key-values
  • Contextual Cohorts created and activated in the Permutive Dashboard

Configure GAM Key-Values

Before implementing the code, configure a key-value in Google Ad Manager.
1

Navigate to Key-Values

In Google Ad Manager, navigate to Inventory > Key-Values and click New key-value.
2

Create the key

Configure the key with these settings:
  • Name: prmtvctx
  • Display name: Permutive Contextual
  • Value type: Predefined
  • Report on values: Include values in reporting

Implementation

Choose the implementation for your platform. Each example calls the Contextual API /segment endpoint and passes the returned cohort codes to GAM.
Add this script to the <head> of your page, as early as possible to ensure targeting is attached to the first ad call. You may need to customize this snippet to work with your setup.Replace the following placeholders:
  • $API_KEY — the same API key you use in your main Permutive deployment
  • $PAGE_PROPERTIES — the same page properties object you pass to window.permutive.addon('web', { page: {...} }), excluding any user-related properties
Personal Data: The Contextual API request must not contain any user-related data points. Make sure to omit any user-related properties that you might be recording as part of your Pageview event.
// new script for contextual
;(function (apiKey, pageProperties) {
  var url = 'https://api.permutive.com/ctx/v1/segment?k=' + apiKey
  var request = new window.XMLHttpRequest()

  request.open('POST', url, true)
  request.onreadystatechange = callback
  request.send(JSON.stringify({
    pageProperties: enrichClient(pageProperties),
    url: document.URL
  }))

  function callback () {
    if (request.readyState === 4 && request.status === 200) {
      var apiResponse

      try {
        apiResponse = JSON.parse(request.responseText)
      } catch (e) {
        return
      }

      // expose API response to main SDK
      window.permutive.addon('contextual', apiResponse)

      window.googletag = window.googletag || {}
      window.googletag.cmd = window.googletag.cmd || []
      window.googletag.cmd.push(function () {
        var pubads = window.googletag.pubads()
        var ctxcohorts = apiResponse.gam || []
        var targetingKey = 'prmtvctx'
        var targetingValues = ctxcohorts.concat('rts') // always include "rts"

        pubads.setTargeting(targetingKey, targetingValues)
      })
    }
  }

  function getClient () {
    return {
      url: document.URL,
      referrer: document.referrer,
      type: 'web',
      user_agent: navigator.userAgent,
      domain: window.location.hostname,
      title: document.title
    }
  }

  function enrichClient (pageProps) {
    try {
      var clientObject = {
        client: getClient()
      }

      if (typeof pageProps === 'object' && !Array.isArray(pageProps)) {
        return Object.assign({}, pageProps, clientObject)
      } else {
        return clientObject
      }
    } catch (e) {
      return {}
    }
  }
})($API_KEY, $PAGE_PROPERTIES)

// existing deployment - DO NOT ADD AGAIN
// window.permutive.addon('web', { page: $PAGE_PROPERTIES })
The enrichClient function adds client properties (URL, referrer, user agent, domain, title) to the payload, enabling additional targeting options.
Implementation timing: Add the script as early as possible in the page <head> to ensure the Contextual API call completes before the first ad request is made.

Testing

After implementing the code, verify that contextual targeting is working correctly:
1

Test in staging

Deploy the implementation in a staging or test environment first. Load a page with the script and check your browser’s network tab to confirm the Contextual API request completes successfully and returns cohort codes.
2

Verify GAM targeting

Inspect the ad request in your browser’s developer tools to confirm that the prmtvctx key-value is present with the expected cohort codes.
3

Share testing link

Share a testing link with Technical Services so we can help validate the deployment before releasing to production.

Troubleshooting

Verify that:
  • You are using the correct API key
  • Contextual cohorts have been created and activated in the Permutive dashboard
  • The URL being passed matches content that has been classified
Check that:
  • The API call completes before the ad request is made
  • The key-value prmtvctx is correctly configured in your ad server
The Permutive API supports CORS for web implementations. If you encounter CORS errors, verify that your domain is correctly configured in your Permutive workspace settings.

Next Steps