Hello! I'm reviewing the code of this module for possible inclusion in one of our projects and the script references concerned me a bit so I checked in with the security team.

The code I'm referring to is here (as an example, I think there are also a few references like this)...

drupal_add_js(sprintf("var _vis_opt_account_id = %d;
   var _vis_opt_protocol = (('https:' == document.location.protocol) ? 'https://' : 'http://');
   document.write('<s' + 'cript src=\"' + _vis_opt_protocol + 'dev.visualwebsiteoptimizer.com/deploy/js_visitor_settings.php?v=1&a=' + _vis_opt_account_id + '&url=' + encodeURIComponent(document.URL) + '&random=' + Math.random() + '\" type=\"text/javascript\">' + '<\/s' + 'cript>');",
             $settings['id']),
         'inline',
         'header'
       );

... and the part that stands out is the '<s' + 'cript src part. I ran this module through the "coder" module and you have zero errors... NICE!

The main concern I have is that we're not tapping into Drupal's JS features as well as we could be. Here are the responses (minus my thank-yous) and a suggestion on how we can get around this:

14:16 < greggles> weal: so, 
http://drupalcode.org/project/visual_website_optimizer.git/blob/b7eb97a:/visual_website_optimizer.module#l92 seems like it's just trying to write the normal 
                  javascript after the page loads to prevent blocking page load 
                  while waiting for that to download
14:18 < greggles> weal: sure thing - I just looked and then asked msonnabaum who 
                  confirmed it's "ok" for security but not great for performance - 
http://www.stevesouders.com/blog/2012/04/10/dont-docwrite-scripts/
14:20 < msonnabaum> weal: just use the "createElement-insertBefore" pattern 
                    instead, it's a better choice

Comments

elc’s picture

Assigned: Unassigned » elc
Status: Active » Closed (works as designed)

Thanks for your thorough look into the module Ryan.

In short, yes, there is a performance issue with using Synchronous mode and is known, however there is a setting to use Asynchronous mode available which avoids the issue and is now the default.

The javascript included in this module is a parameterised copy and paste of the code provided by the Visual Website Optimizer website, so it is not something I have written. I wouldn't even begin to fiddle with it myself as all this module does is allow for that javascript to be added to selected pages which in turn loads the A/B tests from VWO.

There are two different methods provided by VWO to include the javascript in a website - Synchronous and Asynchronous. The later is now the default. VWO keep me apprised of any updates to the javascript, but if there is a difference and I don't know about it, please let me know so that I can update the module.

The line of code linked, which appears to be from the 6.x-1.x branch is the older "Synchronous download" method. This would have performance penalties but was the recommended method until the Asynchronous method was brought out of beta (see blog post below). The Asynchronous method is a few lines of code down on line 115 and was part of the 1.1 release.

Asynchronous loading is now the method recommended by VWO, and if you have installed the 1.2 version of the module, it was configured to use this mode by default. It does not have the performance penalty.

If you have upgraded from a version previous to 1.1, it can be enabled by going to the settings page which is D6:admin/settings/visual_website_optimizer or D7:admin/config/system/visual_website_optimizer and configuring the module to use the newer method.

VWO Blog post about the subject is here.
http://visualwebsiteoptimizer.com/split-testing-blog/asynchronous-code/