5.3K members online now
5.3K 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
Highlighted

Optimize not recognizing part of global JavaScript file

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi,

 

I'm using Optimize on a WordPress site with a custom contact form that integrates with Salesforce. On this form, I have a bit of JavaScript that shows an additional field based on a selection, and also changes two hidden input field values.

 

This code is compiled and minified into a global JS file, which is included in the <head>. It works outside of Optimize, but when I set up a test experiment, the JS doesn't run. I ended up copying the JS from my global JS file and pasting it into Optimize and it worked perfectly.


My question is why is this? Why would Optimize not recognize this JS until it was placed within Optimize? Other elements (carousel, lightbox, Handlebars, etc) worked as expected.

1 Expert replyverified_user

Optimize not recognizing part of global JavaScript file

Google Employee
# 2
Google Employee

The problem maybe that the script may have not been loaded yet at the time that the optimize container loads.

 

If you want to use your own code in Optimize experiments (either in changes or for targeting rules), you need to include this as a sync script,

above the GA/Analytics installation.

 

See: Tagging best practices

at

https://support.google.com/360suite/optimize/answer/6262084?hl=en

Optimize not recognizing part of global JavaScript file

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

I'm using Tag Manager to manage GA and Optimize. Per the instructions, the Tag Manager snippet is to be placed as close to the opening <head> tag as possible. By moving it further down below my global JS file, GA/Tag Manager scripts be shouldn't be negatively impacted, correct? It is my understanding Tag Manager should be placed high in the markup to prevent JS conflicts.

Marked as Best Answer.
Solution
Accepted by topic author David A
4d

Optimize not recognizing part of global JavaScript file

Google Employee
# 4
Google Employee

Yes, you are right that Tag Manager should be placed as close to the opening head as possible,

but still it has to be positioned below any other scripts that it depends on.

 

If you have scripts that you don't intent to use inside GTM or Optimize code, try to keep them below the Tag Manager snippet.

 

See notes in the article above about order:

Placement of the Optimize code snippets is important. Generally, the order of the tags should be:

  1. Any JavaScript (e.g. jQuery) that you wish to use in Optimize experiments. We recommend you keep these to a minimum.
  2. Initialization of any variables needed for targeting (data layer, JavaScript, cookies, etc).
  3. Page-hiding snippet.
  4. Modified Analytics tracking code with Optimize plugin.
  5. Tag Manager container snippet.
  6. Other JavaScript, trackers, and ad tags.

Since you are using Tag Manager what applies in your case is:

  1. Any JavaScript (e.g. jQuery) that you wish to use in Optimize experiments. We recommend you keep these to a minimum.
  2. Initialization of any variables needed for targeting (data layer, JavaScript, cookies, etc).
  3. Page-hiding snippet.
  4. Tag Manager container snippet.
  5. Other JavaScript, trackers, and ad tags.

 

Optimize not recognizing part of global JavaScript file

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

I'll move them around and give it a try. Thanks for the info!

Experts in the Topic
Dimitris D