Last active
May 9, 2017 11:39
-
-
Save akase244/5915b8cf2841dee4b3c8287d21ebfd9f to your computer and use it in GitHub Desktop.
helpers.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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