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

Viewed 2065

Using Google Optimize I want to run an A/B test which includes loading an external script for one of the variants. In order to do this, I need to be able to add my script before the closing <body> tag, preferably.

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

If I select the body tag using the visual editor, I do not have the option to Edit HTML. You can Insert HTML, but if I try to append the script tag, Google tells me I have to remove it.

Is there a way to do this?

1 Answers

you might try adding following snippet instead of inserting the script directly in HTML block:

<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>
Related