You are here

function ga_push_method_utmp_php_request in GA Push 7

Same name and namespace in other branches
  1. 8 inc/ga_push.utmp.php.inc \ga_push_method_utmp_php_request()

Sends tracking data to GA.

Parameters

array $data: Data prepared for GA.

array $options: Custom options from ga_push_add().

string $method: Method to use (GET or POST).

See also

ga_push_add()

https://developers.google.com/analytics/devguides/collection/protocol/v1...

1 call to ga_push_method_utmp_php_request()
ga_push_method_utmp_php in inc/ga_push.utmp.php.inc
GA Push Method callback: UTMP (php).

File

inc/ga_push.utmp.php.inc, line 201
UTMP: method and functions for Universal Tracking Measure Protocol.

Code

function ga_push_method_utmp_php_request($data = array(), $options = array(), $method = 'POST') {
  $data['v'] = 1;

  // Options variables:
  $data['tid'] = !empty($options['tid']) ? $options['tid'] : variable_get('googleanalytics_account', '');
  $data['cid'] = !empty($options['cid']) ? $options['cid'] : ga_push_method_php_utmp_get_uuid();

  // Optional values:
  $optional = array(
    'ua',
    'uip',
  );
  foreach ($optional as $value) {
    if (array_key_exists($value, $options)) {
      $data[$value] = $options[$value];
    }
  }

  // @NOTE: Proxy Server:
  // Some environments are not able to send hits to Google Analytics directly.
  // To collect the IP and user agent from the client device and not the proxy
  // server, you can specify both values in the measurement protocol, and they
  // will override the values Google Analytics normally obtains from the
  // request headers.
  $user_agent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
  if (!empty($user_agent)) {
    $data += array(
      'ua' => $user_agent,
    );
  }
  $ip = ip_address();
  if (!empty($ip)) {
    $data += array(
      'uip' => $ip,
    );
  }
  $url = 'https://' . GA_PUSH_METHOD_UTMP_HOST_ENDPOINT;
  $httpr_options = array(
    'method' => $method,
    'data' => drupal_http_build_query($data),
  );

  // @NOTE: log the request/response. We do it in different watchdogs on purpose
  // to detect non response requests.
  $debug = variable_get('ga_push_debug', FALSE);

  // LOG the request:
  if ($debug) {
    watchdog('ga_push', 'GA PUSH UTMP @type request with: @data', array(
      '@type' => $data['t'],
      '@data' => print_r($httpr_options, TRUE),
    ), WATCHDOG_DEBUG);
  }

  // @TODO: Check errors here so caller doesn't have to do it.
  $response = drupal_http_request($url, $httpr_options);

  // LOG the response:
  if ($debug) {

    // Cast object to array:
    $response_log = (array) $response;

    // Ignore data as it is a GIF.
    unset($response_log['data']);
    watchdog('ga_push', 'GA PUSH UTMP @type response with: @data', array(
      '@type' => $data['t'],
      '@data' => print_r($response_log, TRUE),
    ), WATCHDOG_DEBUG);
  }
}