Where to place Baidu Shangqiao code:Where should I place the Baidu Shangqiao code on my website?
Q: Where should I place the Baidu Shangqiao code on my website?
A: The Baidu Shangqiao code should typically be placed in the <head> section of your HTML document, ideally just before the closing </head> tag. This ensures the script loads early enough to track conversions and user interactions without blocking the page content from rendering. If you are using a CMS like WordPress, you can add it through a plugin such as Insert Headers and Footers, or directly in your theme's header.php file. For most websites, placing the code site-wide—rather than on a single landing page—is recommended so Baidu can collect data across all pages where conversions might occur. Avoid placing the code in the footer if you need it to fire before user actions are recorded, as delayed loading can cause missed tracking. After placement, verify installation using Baidu's debugging tools or your browser's network panel to confirm the script loads and reports correctly.
Q: Can I place the Baidu Shangqiao code in the body instead of the head?
A: Yes, you can place the Baidu Shangqiao code in the body, but it is generally not the preferred location. Placing it in the <head> allows the tracking script to initialize before page content renders, which improves accuracy for conversion tracking and page-load events. If you place it in the body—commonly just before the closing </body> tag—the script loads later, which may cause some events to be missed if users interact quickly or navigate away before the script executes. That said, if your site architecture or CMS restricts head access, adding it before </body> is an acceptable fallback and still captures most data. For single-page html">applications or sites using async loading, consider injecting the code after the DOM is ready. Whichever location you choose, test with Baidu's verification tools to ensure the code fires and records data correctly, and avoid duplicating the snippet across multiple positions.
Q: Where do I place the Baidu Shangqiao code on WordPress or Shopify?
A: On WordPress, the easiest way to place the Baidu Shangqiao code is to use a header plugin such as Insert Headers and Footers, then paste the snippet into the header section so it html">appears site-wide. Alternatively, you can add it to your child theme's functions.php using a wp_head hook, or edit header.php directly (though updates may overwrite manual edits). On Shopify, go to Online Store > Themes > Edit code, open theme.liquid, and paste the code just before the closing </head> tag; this html">applies it across all pages. For both platforms, confirm the code is only inserted once and is not duplicated by other html">apps or scripts. After installation, use Baidu's conversion debugging tools, check page source for the snippet, and monitor network requests to verify that the script loads and reports data properly.
Words of experience
Common scenarios of "Where to place Baidu Shangqiao code"
【技术新人小李】 张哥,我最近在给公司官网做推广,听说百度商桥能跟踪访客和在线咨询,准备接入一下。但是代码到底该放哪儿啊?
【前端主管张哥】 商桥代码本质上是一段JS脚本,一般放在每个页面的</body>标签之前,这样不会阻塞页面主体渲染。
【技术新人小李】 是放在</body>前面而不是<head>里吗?我之前看有些统计代码是放head的。
【前端主管张哥】 统计类代码放head也能跑,但商桥会创建悬浮咨询窗口,还要加载一些资源,放body末尾更稳妥,避免影响首屏加载速度。
【技术新人小李】 明白了。那我们官网是用Vue做的SPA,每个页面没有独立的</body>,这种情况怎么处理?
【前端主管张哥】 SPA的话就不能简单放进index.html的body末尾了,因为路由切换时商桥的悬浮窗可能不会重新初始化。建议在根组件App.vue的mounted钩子里动态注入脚本。
【技术新人小李】 动态注入具体怎么写?能给我个思路吗?
【前端主管张哥】 可以用document.createElement('script')创建标签,设置src或直接写innerHTML为商桥给的代码,然后appendChild到body。注意判断一下是否已经加载过,防止重复注入。
【技术新人小李】 那商桥后台给的那段代码里有个<script>标签包裹,我直接塞innerHTML会不会有问题?
【前端主管张哥】 通过innerHTML插入的script标签默认不会执行,这是浏览器的安全机制。你需要把代码里的JS部分提取出来,用eval或者新建script元素设置text内容再插入。
【技术新人小李】 原来如此,那我是不是可以把商桥的JS代码保存成一个本地文件,然后动态import或者用script标签加载?
【前端主管张哥】 可以,但要注意商桥代码可能会更新,最好还是从他们提供的地址加载,或者定期同步。本地化的话记得保留他们的版本注释,方便排查问题。
【技术新人小李】 好的。还有个问题,如果是多页应用,每个HTML都要粘贴一遍吗?有没有统一管理的办法?
【前端主管张哥】 多页应用如果用了模板引擎或者构建工具,可以把这段代码抽成一个公共的footer模板或者include文件,每个页面引入一次就行。纯静态的话确实得每个页面都加。
【技术新人小李】 我们部分老页面是纯静态的,那手动加的话有没有什么规范,比如放在哪个位置、加注释之类?
【前端主管张哥】 建议在</body>前加一行注释,比如<!-- 百度商桥代码开始 -->,方便以后维护和查找。代码本身不要随意改动,尤其是他们生成的ID和参数。
【技术新人小李】 明白了。那如果页面有弹窗或者全屏遮罩,商桥的悬浮窗会不会被挡住?
【前端主管张哥】 会有这个可能,商桥悬浮窗的z-index是固定的,如果你的遮罩层z-index更高就会盖住它。必要时可以调高商桥容器的层级,或者调整自己遮罩的层级。
【技术新人小李】 好,我大概清楚了。最后确认一下:普通页面放</body>前,SPA在根组件动态注入,多页用公共模板,对吧?
【前端主管张哥】 总结得很到位。上线后记得用商桥后台的“代码检测”功能验证一下是否生效,有问题再找我。