Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
helpers.php
<?php
if (! function_exists('notify_to_analytics')) {
function notify_to_analytics($target_page)
{
$params = [];
$params['v'] = '1';
$params['t'] = 'pageview';
$params['tid'] = 'UA-XXXXXXXX-XX';
$params['cid'] = sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
$params['dh'] = \Request::server('HTTP_HOST');
$params['dp'] = '/'.$target_page;
$params['dt'] = $target_page;
$params = http_build_query($params, '', '&');
$header = [
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($params)
];
$context = [
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $header),
'content' => $params
]
];
$analyticsUrl = 'http://www.google-analytics.com/collect';
file_get_contents($analyticsUrl, false, stream_context_create($context));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment