Trying to use DFP targeting in WordPress – need advice on an issue-

OK here is my scenario: I have the following custom targeting criteria defined in DFP:

  • key: smag-lps, values are static page names as matched in WordPress
    to target a landing page for an ad
  • key: postid, values – user is
    allowed to enter the id of a post to target a post for an ad.

The goal is to deliver DFP ads targeting either a page name OR a post ID to be chosen when creating the ad. In my WordPress themes header.php I tried:

Read More
<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.pubads().set("adsense_background_color", "FFFFFF");
      });
</script>
<script type='text/javascript'>
      googletag.cmd.push(function() {
        googletag.defineSlot('/2152723/SM-Leaderboard1', [728, 90], 'div-gpt-ad-1366056066154-0').addService(googletag.pubads());
            .addService(googletag.pubads())
            .setTargeting("smag-lps", "<?php echo get_current_page(); ?>")
                    .setTargeting("postid", "<?php echo get_the_ID(); ?>");
        googletag.defineSlot('/2152723/SM-Leaderboard2', [728, 90], 'div-gpt-ad-1366056066154-1').addService(googletag.pubads());
            .addService(googletag.pubads())
            .setTargeting("smag-lps", "<?php echo get_current_page(); ?>")
                    .setTargeting("postid", "<?php echo get_the_ID(); ?>");
googletag.pubads().enableSingleRequest();
        googletag.enableServices();
      };
</script>

It is tanking my site when I try to use php echo retrieve the current page or ID. Any suggestions on what I am doing wrong?

-M

Related posts

Leave a Reply

1 comment

  1. You can try this (you are in script tag and maybe the php code go wrong with google code)
    adding the quotes into the echo string.

    .setTargeting("smag-lps", <?php echo '"'.get_current_page().'"'; ?>)
    .setTargeting("postid", <?php echo '"'.get_the_ID().'"'; ?>);