2017-07-29 56 views
0

我在javascript代码下面为搜索引擎访问者和社交媒体访问者显示了google adsense广告。但代码不适用于广告。如何仅向搜索引擎访问者和社交媒体访问者显示AdSense广告?

当我在下面的代码上放置adsense广告时,当我从referrer网站访问网址时显示我的广告,以及当我访问直接网址时没有引荐其显示我的广告。我只想展示AdSense广告只搜索引擎和社交媒体访问者,而不是推荐人和推荐人,只有推荐人访问者可以看到AdSense广告。

代码:

<script type="text/javascript"> 
if (document.referrer.match(/google|bing|facebook|twitter/)) { 
    var test = ' 
      <script type="text/javascript"> 
       google_ad_client = ""; 
       <!-- 336x280 --> 
       google_ad_slot = ""; 
       google_ad_width = 336px; 
       google_ad_height = 280px; 
      </script> 
      <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 
      '; 
    document.write(test); 

} else { 
    document.write("its not referrer"); 
} 
</script> 

当我使用这下面的代码它工作正常,但是当我放置广告它不工作。

代码:

<script type="text/javascript"> 
if (document.referrer.match(/google|bing|facebook|twitter/)) { 
    document.write("its a referrer visitor"); 

} else { 
    document.write("its not referrer"); 
} 
</script> 

请帮我解决这个问题。谢谢

回答

0

既然你已经在一个JavaScript块,你可以马上执行代码,而不是document.write-新的JavaScript代码。使用adsense的asynchronous mode,您还可以获得一些页面加载改进。

<!-- Google Adsense will place the ad within the ins-block. Display none 
    by default, we will change that in javascript --> 
<ins class="adsbygoogle" 
    style="display:none; width:336px;height:280px;" 
    data-ad-client="ca-pub-xxxxxxxxxxxxxxxx" 
    data-ad-slot="1234567890"></ins> 

<script type="text/javascript"> 
    if (document.referrer.match(/google|bing|facebook|twitter/)) { 
     // Show the ins-block 
     document.getElementsByClassName('adsbygoogle')[0].style.display = 'inline-block'; 
     // Load adsense-javascript using 
     var script = document.createElement('script'); 
     script.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"; // Note: different url due to async 
     script.async = true; // Not necessary, but speeds up loading your page 
     var firstScript = document.getElementsByTagName('script')[0]; 
     firstScript.parentNode.insertBefore(script, firstScript); 
    } 
</script> 
+0

感谢您的回答...广告不显示 –

+0

我的错误,谷歌处理异步不同。我修改了我的回答 –

+0

,其中我设置了以下代码:'' –

相关问题