You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DFP中使用URL片段作为Key-Value/定向目标?

在Google Ad Manager(原DFP)中基于URL路径设置Key-Value定向的完整指南

Hey James, 针对你要在DFP(现在已更名为Google Ad Manager)中实现基于URL里/merchandise/路径的定向需求,我整理了一套完整的步骤和代码实现方案,帮你精准触发对应广告的展示:

一、后台Key-Value参数的规范配置

首先得在GAM后台做好基础配置,别直接用/merchandise/当Key,这样既不规范也不利于后续扩展:

  • 进入GAM后台的库存 > Key-values页面,创建一个新Key,比如命名为page_category(用清晰的语义化名称,方便后续维护)
  • 给这个Key添加对应Value:merchandise(对应URL里的/merchandise/路径),同时可以提前把jobs、car这类可能的路径对应的Value也加上,方便后续复用
  • 保存后,在你的广告单元或订单的定向设置中,选择page_category这个Key,匹配merchandise Value,完成后台的定向规则配置

二、页面端完整代码实现(URL路径映射到Key-Value)

你提到之前看到的教程代码不完整,这里是可直接复用的完整逻辑,核心是从URL中识别路径,再把对应分类传递给GAM:

// 1. 获取当前页面的URL路径
const currentPath = window.location.pathname;

// 2. 定义URL路径与Key-Value的映射关系
const pathCategoryMap = {
  '/merchandise/': 'merchandise',
  '/jobs/': 'jobs',
  '/car/': 'car'
  // 后续新增路径直接在这里添加即可
};

// 3. 匹配当前路径对应的分类,默认用'default'兜底
let pageCategory = 'default';
for (const targetPath in pathCategoryMap) {
  if (currentPath.includes(targetPath)) {
    pageCategory = pathCategoryMap[targetPath];
    break; // 找到第一个匹配项就停止,避免多路径冲突
  }
}

// 4. 将Key-Value传递给GAM广告代码
window.googletag = window.googletag || { cmd: [] };
googletag.cmd.push(function() {
  // 设置全局定向参数
  googletag.pubads().setTargeting('page_category', pageCategory);
  
  // 初始化你的广告单元(替换成实际的广告单元ID和尺寸)
  googletag.defineSlot('/12345678/your_ad_unit_id', [728, 90], 'div-gpt-ad-12345678-0')
    .addService(googletag.pubads());
  
  googletag.pubads().enableSingleRequest();
  googletag.enableServices();
});

三、简化版代码(针对你的URL结构优化)

你的URL结构是固定/eu/之后跟着分类路径,比如/eu/merchandise/...,可以直接提取路径片段来简化代码:

const currentPath = window.location.pathname;
// 拆分路径并过滤空字符串,得到['eu', 'merchandise', ...]
const pathSegments = currentPath.split('/').filter(seg => seg.trim() !== '');
// 取第二个片段作为分类,兜底用'default'
const pageCategory = pathSegments[1] || 'default';

// 后续传递给GAM的代码和上面一致
window.googletag = window.googletag || { cmd: [] };
googletag.cmd.push(function() {
  googletag.pubads().setTargeting('page_category', pageCategory);
  // 初始化广告单元...
});

四、验证定向是否生效

  • 使用GAM后台的广告预览工具,输入包含/merchandise/的测试URL,检查广告是否正确展示
  • 在页面浏览器控制台执行googletag.pubads().getTargeting('page_category'),确认返回值为merchandise,说明参数已正确传递

内容的提问来源于stack exchange,提问作者James Tan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:34:03