2013-04-09 50 views
7

在Ember(把手)模板中包含Google AdSense横幅的适当方式是什么? 说我有一个模板,看起来像这样一个观点:Ember模板和Google AdSense

<script type="text/x-handlebars" data-template-name="myPageWithBanner"> 
    <div id="mainContent"> 
    Lorem ipsum main content 
    </div> 
    <div id="banner"> 
    //Normally I would insert a script tag here, but that is not possible 
    </div> 
</script> 

我需要使用预编译模板代码做到这一点,还是有办法,我不知道呢?

回答

3

我没有Google AdSense帐户,所以无法对此进行测试。但这里有几个主要问题:

  1. 不能包括把手模板内<script>标签,甚至没有,如果你使用CDATA。
  2. Google AdSense要求AdSense JavaScript必须在您的信息页中逐字显示,否则将违反TOS。
  3. 根据this StackOverflow answer,目前对AJAX网站的AdSense支持很差。
  4. Google AdSense抓取工具无法在您的网页上看到任何内容,因此我怀疑广告定位是否会起作用。但请参阅下面的一些可能有助于爬虫的事情。

但为了简单起见,我将假设您可以直接与Google讨论TOS问题,并且我将尽力解决技术问题。首先,基于this StackOverflow answer,这里有一个可能的解决方案,让您成为了谷歌的剧本逐字:

<script type="text/x-handlebars"> 
    <h1>Ember AdSense</h1> 
    {{outlet}} 
    <div id="ads"></div> 
</script> 

<script type="text/x-handlebars" data-template-name="index"> 
    <p>Hello, world!</p> 
</script> 

<div id="ads-load" style="display: none"> 

<!-- 
    Apparently this needs to appear verbatim, exactly as Google gave it to 
    you, or it's a TOS violation. 
--> 

<script type="text/javascript"><!-- 
google_ad_client = "ca-pub-XXXXXXXXXX"; 
/* Test Ad */ 
google_ad_slot = "XXXXXX"; 
google_ad_width = 250; 
google_ad_height = 250; 
//--> 
</script> 
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

</div> 

然后,当我们的主模板的负载,我们使用jQuery的广告迁移到我们的应用程序模板:

window.App = Ember.Application.create(); 

// Explicitly declare the view class for our application view. 
App.ApplicationView = Ember.View.extend({ 
    // Called once the view is rendered. 
    didInsertElement: function() { 
     $('#ads-load').appendTo("#ads").css("display", "block"); 
    } 
}); 

至于允许Google抓取工具查看您的内容,Google有official advice for AJAX applications,但我的号码是don't know whether that works with the AdSense crawler。或者,如果您使用pushState来更新显示的网址,那么您需要确保每个网址都可以在您的服务器上被抓取工具请求时呈现。 (The Discourse论坛软件does exactly this。)

请让我知道,如果它让你更接近。

+0

它似乎没有工作。脚本标记插入到正确的div中,但似乎不足以触发它加载横幅。 – Johan

+0

我刚刚更新了基于另一个StackOverflow答案的新代码。这会让你更接近吗?另请注意,AdSense无法看到您的网页内容。这里有一些建议的解决方案:http://stackoverflow.com/questions/10834751/adsense-with-ajax – emk

+0

我其实认为我会让AdSense广告在一个不是Ember处理的div中(即一个静态的) 。这将使我不会违反TOS并摆脱动态添加横幅的问题。我相信你的答案对其他用例来说是正确的,所以我会给你答案。 – Johan