如何将 Google Adsense 放入 GWT

发布于 2024-07-13 16:37:09 字数 47 浏览 9 评论 0 原文

有谁知道如何将 Google adsense 广告放入 GWT 网络应用程序中?

Do anyone know how to put Google adsense ads inside a GWT web application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

随梦而飞# 2024-07-20 16:37:09

您可以将 Adsense 中的 javascript 代码放入 GWT 启动的单个 HTML 页面中。 这样广告就不会显示在与 GTW 相同的区域,而是显示在 GWT 代码的上方/下方。 对于广告来说可能没问题。

此示例在应用程序上方放置了一个横幅:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>www.javaoracleblog.com</title>
    <script type="text/javascript" language="javascript" src="com.javaoracleblog.aggregator.nocache.js"></script>
  </head>
  <body>
<script type="text/javascript"..
ADsense code here 
</script>
    <!-- OPTIONAL: include this if you want history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
  </body>
</html>

为了向 Google WT 指示 Google adsense 网站可以信任,您需要将正则表达式匹配 URL 添加到 -whitelist 命令行参数。

请注意,这可能无法解决上面“为什么我放弃 GWT”文章中描述的问题。

You can put the javascript-code from Adsense in the single HTML page that GWT starts with. This way the advertising will not be displayed in the same area as GTW but above/below the GWT code. For advertising that could be ok.

This example places a baner above the application:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>www.javaoracleblog.com</title>
    <script type="text/javascript" language="javascript" src="com.javaoracleblog.aggregator.nocache.js"></script>
  </head>
  <body>
<script type="text/javascript"..
ADsense code here 
</script>
    <!-- OPTIONAL: include this if you want history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
  </body>
</html>

In order to indicate to Google WT that the site of Google adsense can be trusted you need to add a regex matching URL to the -whitelist command line argument.

Note that this will probably not solve the problems desribed in the above "Why I dumped GWT" article.

醉生梦死 2024-07-20 16:37:09

根据 AdSense 上的此帖子

简短版本,您无法使用 Adsense
通过 Ajax 而不破坏
计划政策/条款和条件

长版...

通过 xmlhttp 调用传递的广告代码
没有被渲染,它只是被视为
文本(因此,responseText)。 唯一的
执行js代码的方法是使用
“responseXML”加上
“exec()”命令。

例如...

如果您的 xml 包含某些内容
行:

这是
来自外部的内容
文件 JavaScript 代码
转到这里

您将分配一个变量(称为
例如 page_data)使用
ajax_obj.responseXML,运行 XML
通过解析器并运行

exec(js 变量或来自 XML 的行
在这里);

Adsense 并没有多大帮助
的观点,但这就是它的完成方式。

还值得一提的是为什么我抛弃了GWT

另一个问题是我的 adsense
横幅。 因为我没有很多
页面上的内容,横幅是
有时偏离主题。 一个更大的
问题是横幅仍然存在
当人们搜索时也是如此
不同的关键字(因为 ajax
刷新没有触发adsense
刷新)。 我通过执行以下操作解决了这个问题
通过页面刷新来搜索而不是
ajax 调用。 ajax部分
网站仅限于排序、分面、
i18n 和显示提示。

According to this thread on AdSense:

Short version, you can't use Adsense
via Ajax without breaking the
programme policies/t&c's

Long version...

Ad code passed through an xmlhttp call
is not rendered, it's just treated as
text (hence, responseText). The only
way to execute js code is to use
"responseXML" coupled with the
"exec()" command.

For instance...

If your xml contains something along
the lines of:

This is the
content from the external
file javascript code
goes here

You would assign a variable (called
page_data for instance) using
ajax_obj.responseXML, run the XML
through a parser and run

exec(js variable or line from XML
here);

Not really helpful from an Adsense
standpoint, but that's how it's done.

It's also worth mentioning Why I dumped GWT:

Another problem were my adsense
banners. Since I didn’t have a lot of
content on the page, the banners were
sometimes off topic. An even bigger
problem was that the banners stayed
the same when people searched for
different keywords (since the ajax
refresh didn’t trigger an adsense
refresh). I solved this by doing the
search with a page refresh instead of
an ajax call. The ajax part of the
site was limited to sorting, faceting,
i18n and displaying tips.

梦里兽 2024-07-20 16:37:09

您可以查看我对 InfoQ 进行的采访。 它包括我书中的一个示例章节,并且恰好是关于 SEO 的。

这并不简单,但我认为本章中的解决方案可以让 GWT 在 SEO 很重要的环境中很好地工作。 基本的解决方案是实现我称之为“引导”的东西。 这意味着您的页面会获取通常在 GWT-RPC 请求中遇到的信息,并将它们序列化到页面中。 然后,GWT 小部件会在没有 RPC 请求的情况下加载此信息。 当您的页面将信息序列化为 JavaScript 时,编写

请查看此处包含的 PDF:InfoQ GWT 它包含所有详细信息。 整个示例项目位于:google 代码,源代码位于 github 上。

You might check out the interview I did with InfoQ. It includes a sample chapter from my book and it happens to be on SEO.

It's not trivial, but I think the solutions in the chapter let GWT work nicely in an environment where SEO is important. The basic solution is to implement something I call 'bootstrapping'. This means that your pages take the info that would normally come across in GWT-RPC requests and serialize them into the page. The GWT widget then loads this information without an RPC request. While your page serializes the info into JavaScript, it's easy to also write a <noscript> to the page that can be used for SEO.

Take a look at the PDF included here: InfoQ GWT it goes into all the detail. The whole sample project is here: google code with source on github.

如此安好 2024-07-20 16:37:09

如果您确实希望 AdSense 位于 GWT“内部”,我会使用 Frame 小部件。 基本上,Frame 小部件会生成一个

这是实际的解决方案:

您应该将

<ui:style>
    .sponsor {
    border: 0em;
    width: 20em;
    height: 6em;
    float: right;
    display: inline;
    }
</ui:style>
<g:HTMLPanel>
    <g:Frame ui:field="sponsor" url="issue/extern/Google-AdSense.html" styleName="{style.sponsor}"/>
</g:HTMLPanel>

.ui.xml文件和逻辑放入相应的 .java 文件:

@UiField
    Frame sponsor;

您还应该输入实际的 Google AdSense 代码(

其外观如下:右上角的广告。

顺便说一句,这也是将 Google Analytics 代码嵌入到 GWT 中的方法。

If you really want AdSense to be kinda "inside" GWT I'd use the Frame widget. Basically the Frame widget generates an <iframe ...> inside your GWT code. First I've thought iframe, UGH! But the <iframe> tag is still part of the HTML5 spec and has been even extended by some attributes that seem to be there for exactly this "sandboxing" purpose. And with corresponding CSS styling you will not have any scrollbars around your <iframe>.

And here is the actual solution:

You should put

<ui:style>
    .sponsor {
    border: 0em;
    width: 20em;
    height: 6em;
    float: right;
    display: inline;
    }
</ui:style>
<g:HTMLPanel>
    <g:Frame ui:field="sponsor" url="issue/extern/Google-AdSense.html" styleName="{style.sponsor}"/>
</g:HTMLPanel>

into your .ui.xml file and the logic into the corresponding .java file:

@UiField
    Frame sponsor;

Also you should put the actual Google AdSense code (the <script> stuff) into a separate HTML file inside GWT's public folder which is - in this case - called Google-AdSense.html and is located inside the extern folder inside the public folder. issue (in the Frame url attribute) is in this case the GWT output folder.

And here is how it looks like: The ad in the upper right corner.

Btw this is also the way to embed the Google Analytics code into GWT.

剧终人散尽 2024-07-20 16:37:09

我是这样做的。 我这里有一个演示和源代码:http://code.google.com/ p/gwt-examples/wiki/DemoGwtAdsene

Here is how I do it. I have a demo and source code here: http://code.google.com/p/gwt-examples/wiki/DemoGwtAdsene

梦开始←不甜 2024-07-20 16:37:09

如果您在创建主机页时多加注意,则可以一起使用 AdSense 和 GWT,而无需使用框架或其他技巧。

关键是将您的 AdSense 代码包含在主页中,然后操作包含广告的 dom 元素,但不将它与页面分离。 因此,只要 dom 结构不变,您就可以将广告重新定位到其他 gwt 代码的主体中。

如果您确实分离并重新附加包含的 dom 元素,那么它将在 Chrome 和 Firefox 中正常工作,但 IE 将显示空白。 我最初尝试将广告 DIV 元素移动到 DockLayoutPanel 中,并认为一切都很好,直到我迟来的在 IE 中测试它。

所以这是可以的:

Element element = Document.get().getElmentById("ad");
element.getStyle().setPosition(ABSOLUTE);
element.getStyle().setTop(20, PX);

但这不是:

myPanel.add(ElementWrapper.wrap(element));

因为将一个小部件添加到另一个小部件会重新定义它的父级。

这意味着您不能使用任何内置的 LayoutPanel 内容来保存广告 div,因为 Layout 无法包装现有元素(它在其构造函数中创建自己的 DIV)。 您也许可以修改布局面板的内容,以便它包装一个元素并且不会重新设置它的父级...但我还没有尝试过。

我在 IE6+、Chrome 和 Firefox 中测试了结果。 缺点是除非加载新页面,否则无法刷新广告。 但就我而言,GWT 用于增强 html 页面,因此这不是问题。 无论如何......用户是否更有可能点击不同的广告而不是他们多次阅读的广告? 不确定它是否如此重要。

You can use AdSense and GWT together without using frames or other hacks if you take some care in how you create your host pages.

The key is to include your AdSense code in the host page and then to manipulate the dom element containing the ad but to not detach it from the page. So you can reposition the ads into the body of your other gwt code as long as the dom structure is not changed.

If you do detach and reattach the containing dom element then it will appear to work in Chrome and Firefox but IE will show a blank space. I tried initially to move the ads DIV element into a DockLayoutPanel and thought everything was great until I belatedly tested it in IE.

So this is OK:

Element element = Document.get().getElmentById("ad");
element.getStyle().setPosition(ABSOLUTE);
element.getStyle().setTop(20, PX);

But this is not:

myPanel.add(ElementWrapper.wrap(element));

because adding a widget to another widget re-parents it.

This means that you cannot use any of the built-in LayoutPanel stuff to hold your ad div because Layout cannot wrap an existing element (It creates its own DIV in its constructor). You may be able to modify the layout panel stuff so it wraps an element and does not re-parent it... but I have not tried this yet.

I've tested the results in IE6+, Chrome and Firefox. Downside is that you cannot refresh the ads unless you load a new page. But in my case GWT was used to enhance html pages so that was not an issue. In any case... are users more likely to click on a different ad than one they read several times? Not sure it is so vital.

最偏执的依靠 2024-07-20 16:37:09

我可以使用 DFP 广告管理系统标准版 + 异步发布商代码 + AdSense 集成来执行此操作:

以下是代码:

在您的主机页上,放置您的发布商代码,例如:

    <head>
        <script type="text/javascript" src="http://www.googletagservices.com/tag/js/gpt.js"></script>
            <script type="text/javascript">
              var slot1 = googletag.defineUnit('/XXXX/ca-pub-YYYYYYYYYYYYYYY/transaction', [468, 60], 'div-gpt-ad-ZZZZZZZ-0').addService(googletag.pubads());
              googletag.pubads().enableSingleRequest();
              googletag.enableServices();
            </script>

...
    </head>

我使用 uiBinder 创建了一个视图,其中包含一个带有 id 的 div在头部指定,如下所示:

    <g:HTMLPanel height="62px" width="100%">

            <div id='div-gpt-ad-ZZZZZZZ-0' style='width:470px; height:60px;'>
            </div>
    </g:HTMLPanel>

在视图的 onLoad() 方法上,初始化广告,如下所示:

@Override
protected void onLoad() {

    setupAd();
}

public static native void setupAd() /*-{
    $wnd.googletag.cmd.push(function() {$wnd.googletag.display('div-gpt-ad-ZZZZZZZ-0')});
}-*/;

要刷新广告,只需为头部指定的插槽调用刷新广告:

public static native void refreshAd() /*-{
    $wnd.googletag.pubads().refresh([$wnd.slot1]);
}-*/;

就这样!

有关发布商标签的更多信息,请检查:
http://support.google.com/dfp_sb/bin /answer.py?hl=zh-CN&answer=1649768

现在,我正在努力了解如何让 AdSense 机器人抓取我的 ajax 应用程序。 我已经实现了 Ajax 抓取方案:

https://developers.google。 com/webmasters/ajax-crawling/docs/getting-started

但我从 AdSense 论坛获得信息,AdSense 机器人 (Mediapartners-Google) 不适用于“转义片段”Ajax 方案。
有人知道谷歌是否计划在这方面取得任何进展吗?

这限制了这种方法只能提供基于兴趣的广告,因为基于上下文的广告服务依赖于 ajax 抓取方案。

I could do this using DFP Small Business + Async Publisher tag + AdSense integration:

Here is the code:

On of your host page, put your publisher tag, something like:

    <head>
        <script type="text/javascript" src="http://www.googletagservices.com/tag/js/gpt.js"></script>
            <script type="text/javascript">
              var slot1 = googletag.defineUnit('/XXXX/ca-pub-YYYYYYYYYYYYYYY/transaction', [468, 60], 'div-gpt-ad-ZZZZZZZ-0').addService(googletag.pubads());
              googletag.pubads().enableSingleRequest();
              googletag.enableServices();
            </script>

...
    </head>

I've created a view with uiBinder, with a div with the id specified at the head, like this:

    <g:HTMLPanel height="62px" width="100%">

            <div id='div-gpt-ad-ZZZZZZZ-0' style='width:470px; height:60px;'>
            </div>
    </g:HTMLPanel>

On the onLoad() method of the view, you initialize the ad, like this:

@Override
protected void onLoad() {

    setupAd();
}

public static native void setupAd() /*-{
    $wnd.googletag.cmd.push(function() {$wnd.googletag.display('div-gpt-ad-ZZZZZZZ-0')});
}-*/;

To refresh the ad, just call refresh ad for the slot specified at head:

public static native void refreshAd() /*-{
    $wnd.googletag.pubads().refresh([$wnd.slot1]);
}-*/;

That's all!

For more information about the publisher tag, check:
http://support.google.com/dfp_sb/bin/answer.py?hl=en&answer=1649768

Now I'm struggling to know how to make AdSense bot to craw my ajax application. I've implemented the Ajax Crawling scheme:

https://developers.google.com/webmasters/ajax-crawling/docs/getting-started

But I've got the information from AdSense forum that the AdSense bot (Mediapartners-Google) doesn't work with "escaped fragment" Ajax scheme.
Does anybody know if Google plan to make any progress on that?

This limits this approach to serve just interest based ads, since the context based ad serving depends on ajax crawling scheme.

再见回来 2024-07-20 16:37:09

Google 的 AdSense 机器人会抓取您的网页以确定投放哪些广告。 因此,您不应将 AdSense 放置在主要包含动态内容的网页上。 效果不会很好。

也许您应该研究一下其他广告程序?

Google's AdSense bot crawls your page to determine what ads to serve. Therefore, you should not put AdSense on pages with mostly dynamic content. It won't work well.

Maybe you should look into other ad programs?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文