Currently utmp-php pushes don't send over a hostname to Google Analytics.

This can cause issues where GA views are filtered by hostname. For example, if users run development/staging environments that track to the same GA account, then they may set up a view with a filter that only includes traffic from the production domain. As things stand, any activity that gets sent though ga_push via utmp-php would be excluded.

To simply send the current URL as the referrer we could simply add the following 'dh' => $_SERVER['HTTP_HOST'] to $data in ga_push_method_utmp_php_request(). However, there might be an outside chance that someone out there is currently relying on it not sending the referrer to differentiate that activity, so we could make this turn on-able in the module settings.

patch to follow.

Comments

PQ created an issue. See original summary.

pq’s picture

Status: Active » Needs review
StatusFileSize
new2.08 KB

...and here's the patch.

gedur’s picture

Status: Needs review » Needs work

I needed the same functionality in one project some moths ago (only for ecomerce transactions) but I resolved it with a drupal_alter, this is the code:

/**
 * Implements hook_ga_push_add().
 */
function mymodule_ga_push_add_alter(&$push, $type, $method_key, $options) {
  global $base_url;

  // Force the host in ServerSide Push
  # https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
  if ($method_key == GA_PUSH_METHOD_UTMP_PHP) {
    $host = array('dh' => parse_url($base_url, PHP_URL_HOST));
    
    if ($type == GA_PUSH_TYPE_ECOMMERCE) {

      if (!empty($push['trans'])) {
        $push['trans'] += $host;
      }

      if (!empty($push['items'])) {
        foreach ($push['items'] as $key => $value) {
          $push['items'][$key] += $host;
        }
      }
    }
    else {
      $push += $host;
    }
  }

}

I think is a good idea to activate this feature by UI, some comments:

Despite this only works in UTMP (PHP) this functionality:
- do not require to be the default one as this module could be used as an API and other modules could force UTMP PHP (rules, commerce_google_analytics...)
- The name of the variable should be independent of the method {ga_push_umtp_php_send_hostname} -> like {ga_push_send_hostname} in the description is ok to aware that will only work in UTMP PHP, later this could be ported to other methods.

'dh' should be only overridden if there is no value like the previous variables, this way could be alter before.

if (variable_get('ga_push_umtp_php_send_hostname', FALSE)) {
   $data['dh'] = $_SERVER['HTTP_HOST'];
}

PQ, thank you for your patch.

goz’s picture

@GeduR I fix your code in case rules method is default, we have to use global variable method.

/**
 * Implements hook_ga_push_add().
 */
function mymodule_ga_push_add_alter(&$push, $type, $method_key, $options) {
  global $base_url;

  // Force the host in ServerSide Push
  # https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
  if ($method_key == GA_PUSH_METHOD_UTMP_PHP || ($method_key == GA_PUSH_METHOD_DEFAULT && variable_get('ga_push_default_method') == GA_PUSH_METHOD_UTMP_PHP)) {
    $host = array('dh' => parse_url($base_url, PHP_URL_HOST));

    if ($type == GA_PUSH_TYPE_ECOMMERCE) {

      if (!empty($push['trans'])) {
        $push['trans'] += $host;
      }

      if (!empty($push['items'])) {
        foreach ($push['items'] as $key => $value) {
          $push['items'][$key] += $host;
        }
      }
    }
    else {
      $push += $host;
    }
  }

}
seancasey’s picture

I can confirm that #4 works (though for non-commerce stuff in my case).

In case anyone comes across this thread who doesn't want to create a custom module, the lo-fi solution would be to set $base_url in settings.php.