You are here

optimizely.module in Optimizely 6

Optimizely module

Originally for Visual Website Optimizer by Awesome Software, http://www.awesome-software.net/ and Ted Cooper (ELC) http://drupal.org/user/784944

Ported to Optimizely by netstudio.gr and Yannis Karampelas (http://drupal.org/user/1145950).

Adds a small amount of javascript to the page which loads the A/B test from the optimizely.com website. All configuration after setting the Account ID happens on the Optimizely website.

File

optimizely.module
View source
<?php

/**
 * @file
 * Optimizely module
 *
 * Originally for Visual Website Optimizer by Awesome Software, http://www.awesome-software.net/
 * and Ted Cooper (ELC) http://drupal.org/user/784944 
 *
 * Ported to Optimizely by netstudio.gr and Yannis Karampelas (http://drupal.org/user/1145950).
 *
 * Adds a small amount of javascript to the page which loads the A/B test from
 * the optimizely.com website. All configuration after setting
 * the Account ID happens on the Optimizely website.
 */

/**
 * Implements of hook_perm().
 */
function optimizely_perm() {
  return array(
    'administer optimizely',
  );
}

/**
 * Implements hook_init().
 */
function optimizely_init() {

  // Grab the ID, but only proceed if it's set to something.
  $optimizely_id = variable_get('optimizely_id', 'NONE');
  $optimizely_onoff = variable_get('optimizely_onoff', 'on');
  if ($optimizely_onoff == 'on' && $optimizely_id != "NONE") {
    $protocol = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $optimizely_script = $protocol . ':' . '//cdn.optimizely.com/js/' . $optimizely_id . '.js';
    drupal_set_html_head('<script type="text/javascript" src="' . $optimizely_script . '"></script>');
  }
}

/**
 * Implements hook_menu().
 */
function optimizely_menu() {
  $items['admin/settings/optimizely'] = array(
    'title' => 'Optimizely Setup',
    'description' => 'Configure your Optimizely account ID, and turn the module on/off.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'optimizely_setup_form',
    ),
    'access arguments' => array(
      'administer optimizely',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Settings form
 */
function optimizely_setup_form($form_state) {
  $form['introduction'] = array(
    '#type' => 'markup',
    '#markup' => '<p>' . t('In order to use this module, you need an <a href="@url">Optimizely</a> account. A Free 30 day trial account is available.', array(
      '@url' => url('http://www.optimizely.com/'),
    )) . '</p><p>' . t('Most of the configuration and setup for the A/B and other tests is done by logging into your account on the Optimizely website.') . '</p>',
  );
  $form['optimizely_onoff'] = array(
    '#type' => 'radios',
    '#title' => t('Enable Optimizely'),
    '#description' => t('To start using Optimizely, this setting must be enabled. This setting can be used to globally disable Optimizely from working on your site without disabling yours tests on the Optimizely website.'),
    '#default_value' => variable_get('optimizely_onoff', 'on'),
    '#options' => array(
      'off' => 'Disabled',
      'on' => 'Enabled',
    ),
  );
  $form['optimizely_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Optimizely ID Number'),
    '#default_value' => variable_get('optimizely_id', 'NONE'),
    '#description' => t('Your numeric account ID. This is the number after <q>/js/</q> in the Optimizely Tracking Code.'),
    '#size' => 15,
    '#maxlength' => 20,
    '#required' => TRUE,
  );
  return system_settings_form($form);
}

/**
 * Validation for settigns form.
 * Verify that the account ID is numeric
 */
function optimizely_setup_form_validate($form, &$form_state) {
  if (!preg_match('/^\\d+$/', $form_state['values']['optimizely_id'])) {
    form_set_error('optimizely_id', t('Your Optimizely ID should be numeric.'));
  }
}

Functions

Namesort descending Description
optimizely_init Implements hook_init().
optimizely_menu Implements hook_menu().
optimizely_perm Implements of hook_perm().
optimizely_setup_form Settings form
optimizely_setup_form_validate Validation for settigns form. Verify that the account ID is numeric