Month: May 2018

  • Drupal: How to use the Context Module to include javascript on specific pages

    I needed a solution which allowed me to include Optimizely’s A/B testing snippet on certain pages, and have the ability to change those page rules without modifying code.

    I do this using the Context module, I apply a class to the body for a node with the path “demo”, like so:

    Then, in html.tpl.php, in the <head> of my document, I have the following code:

    <?php 
      if (!empty($classes)) {
        if (strpos($classes, 'has-optimizely') !== false) {
          print '<script src="https://cdn.optimizely.com/js/xxxx.js"></script>';
        }
      }
    ?>