Optimize
3.5K members online now
3.5K members online now
Learn how to create your first experiment and get tips on creating A/B, redirect, and multivariate tests.
Guide Me
star_border
Reply

Adding Javascript to a variant <head>

Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆

Hello Optimize Community,

 

Is there any way to run Javascript code to the <head> of a variant that you built using the Optimize Editor?

 

It seems like you can only run new Javascript on visual elements that you can select via the Editor (makes sense, as most tests deal with visual page elements). But if there's a way to place the code higher up on a variant's page please let me know!

 

Cheers,

 

 - Tim

1 Expert replyverified_user

Adding Javascript to a variant &lt;head&gt;

Explorer ✭ ✭ ✭
# 2
Explorer ✭ ✭ ✭

Hi, Tim.

Wouldnt it work for you?

var scriptElement = document.createElement("script");

...

$('head').append(scriptElement);

...

 

Marked as Best Answer.
Solution
Accepted by Jason O (Community Manager)
February 2017

Adding Javascript to a variant &lt;head&gt;

Google Employee
# 3
Google Employee

Hey Tim, 

 

Yes - you can actually select the head of the document using the advanced selection flow - leftmost part of the secondary bar in the editor.  Hope that helps!


Jon

Adding Javascript to a variant &lt;head&gt;

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

This doesn't seem to solve the issue. When I select the head and javascript, it only allows me to run functions instead of being able to embed scripts like below: 

 

<script type="text/javascript" src="https://www.example.com/api/v1/hQZKYBwq5lLQs9tGmTDexQ/load.js" async></script>