You are here

function ga_push_method_utmp_php_request in GA Push 8

Same name and namespace in other branches
  1. 7 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 164
UTMP: method and functions for Universal Tracking Measure Protocol.

Code

function ga_push_method_utmp_php_request(array $data = [], array $options = [], $method = 'POST') {
  $id = \Drupal::service('ga_push.google_analytics_id')
    ->getAnalyticsId();
  $data['v'] = 1;

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

  // Optional values:
  $optional = [
    '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 += [
      'ua' => $user_agent,
    ];
  }
  $ip = \Drupal::request()
    ->getClientIp();
  if (!empty($ip)) {
    $data += [
      'uip' => $ip,
    ];
  }
  $url = 'https://' . GA_PUSH_METHOD_UTMP_HOST_ENDPOINT;
  $response = NULL;

  // Prevent browser cached data.
  $data += [
    'z' => mt_rand(),
  ];
  $debug = \Drupal::config('ga_push.settings')
    ->get('debug');
  if ($debug) {
    $httpr_options = [
      'method' => $method,
      'data' => UrlHelper::buildQuery($data),
    ];
    \Drupal::logger('ga_push')
      ->debug('GA PUSH UTMP @type request with: @data', [
      '@type' => $data['t'],
      '@data' => print_r($httpr_options, TRUE),
    ]);
  }
  try {
    switch ($method) {
      case 'POST':
        $response = \Drupal::httpClient()
          ->post($url, [
          'form_params' => $data,
        ]);
        break;
      case 'GET':

        // @TODO better handling for query params.
        $response = \Drupal::httpClient()
          ->get($url . '?' . UrlHelper::buildQuery($data));
        break;
    }
    if ($debug) {
      $response_data = $response instanceof ResponseInterface ? 'Status_code: ' . $response
        ->getStatusCode() . ' / Headers: ' . print_r($response
        ->getHeaders(), TRUE) : '';
      \Drupal::logger('ga_push')
        ->debug('GA PUSH UTMP @type response with: @data', [
        '@type' => $data['t'],
        '@data' => print_r($response_data, TRUE),
      ]);
    }
  } catch (RequestException $exception) {
    watchdog_exception('ga_push', $exception);
    return FALSE;
  }
}