You are here

function advagg_mod_ga_inline_to_file in Advanced CSS/JS Aggregation 7.2

Move analytics.js to be a file instead of inline.

Parameters

array $js: JS array.

1 call to advagg_mod_ga_inline_to_file()
advagg_mod_js_pre_alter in advagg_mod/advagg_mod.module
Alter the js array.
3 string references to 'advagg_mod_ga_inline_to_file'
advagg_mod_admin_settings_form in advagg_mod/advagg_mod.admin.inc
Form builder; Configure advagg settings.
advagg_mod_advagg_current_hooks_hash_array_alter in advagg_mod/advagg_mod.module
Implements hook_advagg_current_hooks_hash_array_alter().
advagg_mod_js_pre_alter in advagg_mod/advagg_mod.module
Alter the js array.

File

advagg_mod/advagg_mod.module, line 3473
Advanced aggregation modifier module.

Code

function advagg_mod_ga_inline_to_file(array &$js) {

  // Do nothing if the googleanalytics module is not enabled.
  if (!module_exists('googleanalytics') || !is_callable('googleanalytics_api') || !is_callable('_googleanalytics_cache')) {
    return;
  }

  // Get inline GA js and put it inside of an aggregrate.
  $ga_script = '';
  $debug = variable_get('googleanalytics_debug', 0);
  $api = googleanalytics_api();
  if ($api['api'] === 'analytics.js') {
    $library_tracker_url = '//www.google-analytics.com/' . ($debug ? 'analytics_debug.js' : 'analytics.js');
    $library_cache_url = 'http:' . $library_tracker_url;
  }
  else {

    // Which version of the tracking library should be used?
    if ($trackdoubleclick = variable_get('googleanalytics_trackdoubleclick', FALSE)) {
      $library_tracker_url = 'stats.g.doubleclick.net/dc.js';
      $library_cache_url = 'http://' . $library_tracker_url;
    }
    else {
      $library_tracker_url = '.google-analytics.com/ga.js';
      $library_cache_url = 'http://www' . $library_tracker_url;
    }
  }
  $ga_script = _googleanalytics_cache($library_cache_url);
  if (variable_get('googleanalytics_cache', 0) && $ga_script) {
    $mod_base_url = substr($GLOBALS['base_root'] . $GLOBALS['base_path'], strpos($GLOBALS['base_root'] . $GLOBALS['base_path'], '//') + 2);
    $mod_base_url_len = strlen($mod_base_url);
    $ga_script = substr($ga_script, stripos($ga_script, $mod_base_url) + $mod_base_url_len);
  }
  else {
    $ga_script = $library_cache_url;
    if ($api['api'] === 'ga.js' && $GLOBALS['is_https']) {
      if (!empty($trackdoubleclick)) {
        $ga_script = str_replace('http://', 'https://', $ga_script);
      }
      else {
        $ga_script = str_replace('http://www', 'https://ssl', $ga_script);
      }
    }
  }
  if (!empty($ga_script)) {
    foreach ($js as $key => $value) {

      // Skip if not inline.
      if ($value['type'] !== 'inline') {
        continue;
      }
      $add_ga = FALSE;

      // GoogleAnalytics 2.x inline loader string.
      if ($api['api'] === 'analytics.js') {
        $start = strpos($value['data'], '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();');
        $end = strpos($value['data'], '})(window,document,"script",');
        if ($start === 0) {

          // Strip loader string.
          $js[$key]['data'] = substr($value['data'], 0, $start + 133) . substr($value['data'], $end);
          $js[$key]['data'] = advagg_mod_defer_inline_js($js[$key]['data']);
          $add_ga = TRUE;
        }
      }

      // GoogleAnalytics 1.x inline loader string.
      if ($api['api'] === 'ga.js') {
        $start = strpos($value['data'], '(function() {var ga = document.createElement("script");ga.type = "text/javascript";ga.async = true;ga.src =');
        $end = strpos($value['data'], '";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(ga, s);})();');
        if ($start !== FALSE && $end !== FALSE) {

          // Strip loader string.
          $js[$key]['data'] = substr($value['data'], 0, $start) . substr($value['data'], $end + 91);
          $js[$key]['no_defer'] = TRUE;
          $add_ga = TRUE;
        }
      }
      if ($add_ga) {

        // Add GA analytics.js file to the $js array.
        $js[$ga_script] = array(
          'data' => $ga_script,
          'type' => 'file',
          'async' => TRUE,
          'defer' => TRUE,
        );
        $js[$ga_script] += $value;
        break;
      }
    }
  }
}