将查询字符串传递到Google发布商代码

时间:2015-05-05 22:51:55

标签: javascript query-string key-value google-dfp

我需要将当前网址中的查询字符串传递给GPT(Google发布商代码),该代码会在网页加载时调用DFP广告管理系统。

例如,如果这是URL: http://example.net/article/100-days.html?c=xyz

我需要将c = xyz作为键值插入。我有超过50套这些c =查询字符串,并且需要GPT调用针对任何字符串出现的广告(无论是 c = xyz 还是 c = abc ,等):

<head>

 <script type="text/javascript">
   var googletag = googletag || {};
   googletag.cmd = googletag.cmd || [];
   (function() {
     var gads = document.createElement("script");
     gads.async = true;
     gads.type = "text/javascript";
     var useSSL = "https:" == document.location.protocol;
     gads.src = (useSSL ? "https:" : "http:") + "//www.googletagservices.com/tag/js/gpt.js";
     var node =document.getElementsByTagName("script")[0];
     node.parentNode.insertBefore(gads, node);
    })();
</script>

 <script type="text/javascript">
   googletag.cmd.push(function() {
   googletag.defineSlot('/6355419/Travel/Europe/France/Paris',[300, 250], "banner1"); // adds the first slot with it's own slot level custom targeting

   googletag.pubads().setTargeting("c","xyz"); // adds custom targeting that applies to the entire page - i.e. all the slots on the page.

     googletag.enableServices();
   });
 </script>
</head>

我认为这需要一个get函数,但我不确定在JS中最有效的方法是什么。

1 个答案:

答案 0 :(得分:1)

从这里尝试答案: How can I get query string values in JavaScript?

类似的东西:

googletag.pubads().setTargeting( 'c', getParameterByName('c') );