如何使用 Google 优化在 A/B 实验的结束正文标记之前添加脚本标记?

How do I add a script tag before the closing body tag for an A/B experiment using Google Optimize?

使用 Google 优化 我想 运行 一个 A/B 测试,其中包括为其中一个变体加载外部脚本。为此,我需要能够在结束 <body> 标记之前添加我的脚本,最好是。

    <script type="text/javascript" src="https://example.com.js" async></script>
</body>

如果我 select 使用可视化编辑器的 body 标签,我没有编辑 HTML 的选项。您可以插入 HTML,但如果我尝试附加脚本标签,Google 告诉我必须将其删除。

有办法吗?

您可以尝试添加以下代码片段,而不是直接在 HTML 块中插入脚本:

<script>
var script = document.createElement('script');
script.onload = function () {
    //do stuff with the script
};
script.src = "https://example.com.js";

document.body.appendChild(script);
<\script>