You are here

function _dfp_js_global_settings in Doubleclick for Publishers (DFP) 7

Same name and namespace in other branches
  1. 7.2 dfp.module \_dfp_js_global_settings()

Helper function to include javascript variables, etc in the header above all slot definitions.

1 call to _dfp_js_global_settings()
dfp_preprocess_html in ./dfp.module
Implements preprocess_html().

File

./dfp.module, line 663

Code

function _dfp_js_global_settings() {

  // Initialize the google varibales and inject user-defined javascript.
  $js = 'var googletag = googletag || {};' . "\n";
  $js .= 'googletag.cmd = googletag.cmd || [];';
  $js .= variable_get('dfp_injected_js', '') . "\n";

  // Add a place to store the slot name variable
  $js .= 'googletag.slots = googletag.slots || {};';
  $options = array(
    'type' => 'inline',
    'group' => JS_LIBRARY,
    'every_page' => TRUE,
    'weight' => -10,
    'force header' => TRUE,
    'scope_lock' => TRUE,
  );
  drupal_add_js($js, $options);

  // Include a script tag for the Google Tag Services.
  // @todo: One day this should include an async=true attribute. See #1140356.
  //        However, there is a good chance this might break <= IE8.
  $options['type'] = 'external';
  $options['weight']++;
  drupal_add_js("//" . DFP_GOOGLE_TAG_SERVICES_URL, $options);

  // Add global settings with a heavy weight so that they appear after all the
  // defineSlot() calls otherwise IE8 and Opera fail to display ads properly.
  $js = 'googletag.cmd.push(function() {' . "\n";
  if (variable_get('dfp_async_rendering', 1)) {
    $js .= '  googletag.pubads().enableAsyncRendering();' . "\n";
  }
  else {
    $js .= '  googletag.pubads().enableSyncRendering();' . "\n";
  }
  if (variable_get('dfp_single_request', 1)) {
    $js .= '  googletag.pubads().enableSingleRequest();' . "\n";
  }
  switch (variable_get('dfp_collapse_empty_divs', 1)) {
    case 1:
      $js .= '  googletag.pubads().collapseEmptyDivs();' . "\n";
      break;
    case 2:
      $js .= '  googletag.pubads().collapseEmptyDivs(true);' . "\n";
      break;
  }
  if (variable_get('dfp_disable_init_load', 0)) {
    $js .= '  googletag.pubads().disableInitialLoad();' . "\n";
  }
  if (variable_get('dfp_set_centering', 0)) {
    $js .= '  googletag.pubads().setCentering(true);' . "\n";
  }

  // Set global targeting values for this page.
  $targeting = variable_get('dfp_targeting', array());
  drupal_alter('dfp_global_targeting', $targeting);
  $targeting = dfp_format_targeting($targeting);
  foreach ($targeting as $key => $target) {
    if (!empty($target['target']) && !empty($target['value'])) {
      $js .= '  googletag.pubads().setTargeting(' . $target['target'] . ', ' . $target['value'] . ');' . "\n";
    }
  }
  $js .= '});' . "\n";
  $js .= variable_get('dfp_injected_js2', '') . "\n";
  $js .= 'googletag.enableServices();';
  $options = array(
    'type' => 'inline',
    'group' => JS_DEFAULT,
    'every_page' => TRUE,
    'weight' => 10,
    'force header' => TRUE,
    'scope_lock' => TRUE,
  );
  drupal_add_js($js, $options);
}