View source
<?php
use Drupal\Component\Utility\UrlHelper;
use Psr\Http\Message\ResponseInterface;
define('GA_PUSH_METHOD_UTMP_HOST_ENDPOINT', 'ssl.google-analytics.com/collect');
function ga_push_method_utmp_php($push, $type, $options) {
switch ($type) {
case GA_PUSH_TYPE_EVENT:
$event_data = ga_push_method_php_utmp_construct_event_request_data($push);
ga_push_method_utmp_php_request($event_data, $options);
break;
case GA_PUSH_TYPE_ECOMMERCE:
if (isset($push['trans']) && isset($push['items']) && count($push['items'])) {
$ecommerce_data = ga_push_method_utmp_php_construct_ecommerce_request_data($push);
ga_push_method_utmp_php_request($ecommerce_data['transaction'], $options);
foreach ($ecommerce_data['items'] as $item_data) {
ga_push_method_utmp_php_request($item_data, $options);
}
}
break;
case GA_PUSH_TYPE_EXCEPTION:
$exception_data = ga_push_method_php_utmp_construct_exception_request_data($push);
ga_push_method_utmp_php_request($exception_data, $options);
break;
case GA_PUSH_TYPE_PAGEVIEW:
$pageview_data = ga_push_method_php_utmp_construct_pageview_request_data($push);
ga_push_method_utmp_php_request($pageview_data, $options);
break;
case GA_PUSH_TYPE_SOCIAL:
$social_data = ga_push_method_php_utmp_construct_social_request_data($push);
ga_push_method_utmp_php_request($social_data, $options);
break;
}
}
function ga_push_method_php_utmp_construct_event_request_data(array $push) {
$push['t'] = 'event';
return ga_push_analyticsjs_measurement_protocol_mapping($push);
}
function ga_push_method_utmp_php_construct_ecommerce_request_data(array $push) {
$data = [];
$tdata = [
't' => 'transaction',
];
$tdata += ga_push_analyticsjs_measurement_protocol_mapping($push['trans']);
$data['transaction'] = $tdata;
$data['items'] = [];
foreach ($push['items'] as $value) {
$idata = [
't' => 'item',
];
$idata += ga_push_analyticsjs_measurement_protocol_mapping($value);
$data['items'][] = $idata;
}
return $data;
}
function ga_push_method_php_utmp_construct_exception_request_data(array $push) {
$push['t'] = 'exception';
return ga_push_analyticsjs_measurement_protocol_mapping($push);
}
function ga_push_method_php_utmp_construct_pageview_request_data(array $push) {
$push['t'] = 'pageview';
return ga_push_analyticsjs_measurement_protocol_mapping($push);
}
function ga_push_method_php_utmp_construct_social_request_data(array $push) {
$push['t'] = 'social';
return ga_push_analyticsjs_measurement_protocol_mapping($push);
}
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;
$data['tid'] = !empty($options['tid']) ? $options['tid'] : $id;
$data['cid'] = !empty($options['cid']) ? $options['cid'] : ga_push_method_php_utmp_get_uuid();
$optional = [
'ua',
'uip',
];
foreach ($optional as $value) {
if (array_key_exists($value, $options)) {
$data[$value] = $options[$value];
}
}
$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;
$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':
$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;
}
}
function ga_push_method_php_utmp_get_uuid() {
if (isset($_COOKIE['_ga'])) {
list($version, $domain_depth, $cid1, $cid2) = explode('.', $_COOKIE["_ga"], 4);
return $cid1 . '.' . $cid2;
}
return ga_push_method_php_utmp_gen_uuid();
}
function ga_push_method_php_utmp_gen_uuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
}