You are here

function google_tag_page_build in GoogleTagManager 7

Same name and namespace in other branches
  1. 7.2 google_tag.module \google_tag_page_build()

Implements hook_page_build().

Adds the snippet to the page array if the insertion conditions are met.

See also

drupal_render_page()

File

./google_tag.module, line 158
Provides primary Drupal hook implementations.

Code

function google_tag_page_build(&$page) {
  if (!google_tag_insert_snippet()) {
    return;
  }

  // Call sequence:
  // - drupal_render_page()
  //   - hook_page_build()
  //   - hook_page_alter()
  //   - drupal_render()
  // - drupal_render()
  //   - callbacks in $elements['#theme_wrappers']
  //     - hook_preprocess_html(): 'html' is the wrapper for page
  //     - templates may add tags after body tag
  //   - callbacks in $elements['#post_render']
  //     - google_tag_page_process(): callback set here
  $base_path = 'public:/';
  $include_classes = variable_get('google_tag_include_classes', 0);
  list($realm_name, $realm_key) = google_tag_realm_values();
  $realm_name .= $realm_name ? '/' : '';
  $realm_key .= $realm_key ? '.' : '';
  $types = $include_classes ? array(
    'data_layer',
    'script',
  ) : array(
    'script',
  );
  $data_layer = variable_get('google_tag_data_layer', 'dataLayer');
  if ($include_classes && module_exists('datalayer') && $data_layer == 'dataLayer') {
    $classes = variable_get('google_tag_data_layer_classes', array());
    if ($classes) {

      // Add data_layer using dataLayer module.
      datalayer_add($classes);
    }
    $types = array(
      'script',
    );
  }

  // Add data_layer and script snippets to head (by default).
  $include_script_as_file = variable_get('google_tag_include_file', 1);
  if ($include_script_as_file) {
    foreach ($types as $type) {

      // A stream wrapper may be passed as $data to drupal_add_js($data) because
      // drupal_get_js() calls file_create_url($data). The latter will work with
      // whichever class implements the stream, e.g. local, S3, or App Engine.
      $path = "{$base_path}/google_tag/{$realm_name}google_tag.{$realm_key}{$type}.js";

      // @todo Will it matter if file is empty?
      drupal_add_js($path, array(
        'group' => JS_LIBRARY * 2,
        'requires_jquery' => FALSE,
        'defer' => 'true',
      ));
    }
  }
  else {
    foreach ($types as $type) {
      $url = "{$base_path}/google_tag/{$realm_name}google_tag.{$realm_key}{$type}.js";
      $contents = @file_get_contents($url);

      // @see drupal_get_js()
      // For inline JavaScript to validate as XHTML, all JavaScript containing
      // XHTML needs to be wrapped in CDATA.
      if ($contents) {
        drupal_add_js($contents, array(
          'type' => 'inline',
          'group' => JS_LIBRARY * 2,
          'requires_jquery' => FALSE,
        ));
      }
    }
  }

  // Add noscript snippet to page_top region.
  $type = 'noscript';
  $url = "{$base_path}/google_tag/{$realm_name}google_tag.{$realm_key}{$type}.js";
  $noscript = @file_get_contents($url);
  if ($noscript) {

    // Note: for any theme that follows the pattern of core html.tpl.php in the
    // system module (e.g. bootstrap theme), this does not place the snippet
    // immediately after the body tag but rather after the 'skip-link' div.
    $page['page_top']['google_tag'] = array(
      '#markup' => $noscript,
      '#weight' => -10,
    );
  }
}