了解Doubleclick代码标记

时间:2015-04-07 23:44:09

标签: javascript php wordpress double-click google-dfp

Google Doubleclick

让我们遇到一些难以理解的问题

横幅不断展现和消失。

调试告诉我们:

  

gpt.js:49无法在'Document'上执行'write':这是不可能的   从异步加载的外部脚本写入文档   除非明确打开。


我们在php中分别调用代码,因为Wordpress和LOLS

使用此函数,我们调用主JS

function googletag(){
    $googletag = "
    var googletag = googletag || {};
    googletag.cmd = googletag.cmd || [];
    (function(){
        //console.log('Failed to display 3rd party ad, displaying google ad instead');
        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);
    })();
";

echo "\n<script>\n/* <![CDATA[ */$googletag\n/* ]]> */\n</script>\n";
}

然后是DFP元素:

    <?php
function doubleclick(){
 global $post;
 $url = get_template_directory_uri();

 $doubleclick = '';

 if(is_category('11142')) {

  $doubleclick .="<script src=\"$url/js/double/slot-allaccess.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('363')) {

  $doubleclick .="<script src=\"$url/js/double/slot-artroom.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('11098')) {

 $doubleclick .="<script src=\"$url/js/double/slot-beauty.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('360')) {

  $doubleclick .="<script src=\"$url/js/double/slot-chefs.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('11096')) {


  $doubleclick .="<script src=\"$url/js/double/slot-cookbook.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('365')) {

  $doubleclick .="<script src=\"$url/js/double/slot-decor.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('362')) {

 $doubleclick .="<script src=\"$url/js/double/slot-drinks.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('354')) {

  $doubleclick .="<script src=\"$url/js/double/slot-fashion.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('357')) {

 $doubleclick .="<script src=\"$url/js/double/slot-gossip.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_home() || is_front_page()) {

 $doubleclick .="<script src=\"$url/js/double/slot-home.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('361')) {

  $doubleclick .="<script src=\"$url/js/double/slot-hotspots.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('349')) {

 $doubleclick .="<script src=\"$url/js/double/slot-jewels.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('17')) {

 $doubleclick .="<script src=\"$url/js/double/slot-listen.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('364')) {

  $doubleclick .="<script src=\"$url/js/double/slot-mixtape.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('11097')) {

  $doubleclick .="<script src=\"$url/js/double/slot-musichall.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('356')) {

 $doubleclick .="<script src=\"$url/js/double/slot-muscwatch.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('11143')) {

  $doubleclick .="<script src=\"$url/js/double/slot-redcarpet.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('369')) {

 $doubleclick .="<script src=\"$url/js/double/slot-scents.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('15')) {

  $doubleclick .="<script src=\"$url/js/double/slot-see.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('13')) {

  $doubleclick .="<script src=\"$url/js/double/slot-smell.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('368')) {

  $doubleclick .="<script src=\"$url/js/double/slot-spa.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('355')) {

  $doubleclick .="<script src=\"$url/js/double/slot-spotted.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('16')) {

  $doubleclick .="<script src=\"$url/js/double/slot-taste.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('350')) {

 $doubleclick .="<script src=\"$url/js/double/slot-tech.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('352')) {

  $doubleclick .="<script src=\"$url/js/double/slot-timekeepers.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('14')) {

  $doubleclick .="<script src=\"$url/js/double/slot-touch.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('351')) {

  $doubleclick .="<script src=\"$url/js/double/slot-toysfb.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('353')) {

  $doubleclick .="<script src=\"$url/js/double/slot-traveling.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} elseif(is_category('367')) {

  $doubleclick .="<script src=\"$url/js/double/slot-wellness.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

} else {

  $doubleclick .="<script src=\"$url/js/double/slot-notas.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";

}

if(!empty($doubleclick)) {
  $doubleclick = "\n<!-- Doubleclick Tag Script -->\n" . $doubleclick . "\n";
  echo $doubleclick;
}

}

?>

然后我们放置其中一个横幅:

function top_banner(){
    if ( is_home() || is_front_page()) {

        echo "<!-- home_960x90a -->";
        echo "<div id=\"div-gpt-ad-1427326520458-38\" style=\"width:728px; height:90px;margin:0 auto;\">\n";
        echo "<script>\n/* <![CDATA[ */\n";
        echo "googletag.cmd.push(function() { googletag.display('div-gpt-ad-1427326520458-38'); });\n";
        echo "/* ]]> */\n</script>\n";
        echo "</div>\n";
}

但是没有任何工作,或者有时工作正常。

1 个答案:

答案 0 :(得分:0)

显然,您尝试加载的代码正在使用使用document.write的广告系列,并且它们是异步加载的。您应该将广告系列(您或您的提供商)迁移到异步解决方案或同步加载代码:https://support.google.com/dfp_premium/answer/1638622