3

I need to pass a query string from the current URL into a GPT (Google Publisher Tag) that will call to DFP when the page loads.

For example, if this is the URL: http://example.net/article/100-days.html?c=xyz

I need to have c=xyz inserted as a key-value. I have over 50 sets of these c= query strings, and need the GPT to call an ad targeted to whichever string appears (whether it's c=xyz or c=abc, etc.):

<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>

I think this requires a get function, but I'm not certain what the most efficient way to do this is in JS.

josealtuve
  • 31
  • 2

1 Answers1

1

Try the answer from here: How can I get query string values in JavaScript?

Something like:

googletag.pubads().setTargeting( 'c', getParameterByName('c') );
Community
  • 1
  • 1
Alastair
  • 1,710
  • 2
  • 17
  • 33