简体   繁体   English

如何将 AdSense 的广告单元添加到使用 Docusaurus 构建的 web 页面的正文中

[英]How to add AdSense's Ad Unit to the body of a web page built with Docusaurus

Is it possible to add AdSense's ad unit to the body of a Docusaurus web page?是否可以将 AdSense 的广告单元添加到 Docusaurus web 页面的正文中?

Whenever I paste the Display ad unit code into a markdown page of my site, the browser throws "Uncaught SyntaxError: expected expression, got end of script" .每当我将展示广告单元代码粘贴到我网站的 markdown 页面中时,浏览器都会抛出"Uncaught SyntaxError: expected expression, got end of script"

BTW, I am not referring to the sidebar area.顺便说一句,我不是指侧边栏区域。 I need it within the content of the pages.我在页面内容中需要它。

Any ideas on how to add it successfully?关于如何成功添加它的任何想法? Thanks.谢谢。

Not conveniently on a single page rendered by the docs plugin, no, but you could insert it as a custom sidebar setup :在 docs 插件呈现的单个页面上不方便,不,但您可以将其作为自定义侧边栏设置插入:

module.exports = {
  myHtmlSidebar: [
    {
      type: 'html',
      value: '<div class="my_ad_div"></div>', 
      defaultStyle: true, // Use the default menu item styling
    },
  ],
};

Then, using the gtag plugin, you should be able to hoist the JS that loads the ad code, but show up only on pages where you put a container for it to land in the sidebar.然后,使用 gtag 插件,您应该能够提升加载广告代码的 JS,但仅显示在您将容器放置在侧边栏中的页面上。 This would take some juggling to manage, or you could swizzle some theme components directly.这需要一些杂耍来管理,或者您可以直接调整一些主题组件。 That leads me to the other option - wrapping it.这让我想到了另一个选择——包装它。

With Docusaurus, you also get a lot of the creature comforts that React has to offer (such as Helmet wrappers), which Docusaurus implements as <BrowserOnly> .使用 Docusaurus,您还可以获得 React 必须提供的许多生物舒适性(例如头盔包装器),Docusaurus 将其实现为<BrowserOnly>

You should be able to come up with something so you have somewhat granular control without breaking any upgrade paths, even if you end up writing your own little ad component.您应该能够想出一些东西,以便在不破坏任何升级路径的情况下进行一些精细的控制,即使您最终编写了自己的小广告组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM