We are in the process of migrating this forum. A new space will be available soon. We are sorry for the inconvenience.

Speeding up calls to google analytics


ctype_alnum
24-07-2014, 10:17
Thats probably a load balancer IP and google have a habit of changing things. Its never a good idea to hardcode IP addresses for that reason, you will go from it being slow to not working at all.

Kode
23-07-2014, 21:29
Looks like a large part of it is looking up the google domain

replacing

PHP Code:
$url 'www.google-analytics.com'
$fp fsockopen($url80$errno$errstr5); 
with

PHP Code:
$fp=fsockopen('149.3.176.53'80); 
significantly sped up the requests, only issue is google might change the ip, I could save the ip in memcache though and have it expire every hour or something, that should still signifcantly speed things up.

Kode
23-07-2014, 20:48
This is interesting, I have been unit testing on a cheap VPS I have, and on that particular VPS with tracking turned on it averages about 13 requests a second, with it turned off it averages about 1,000 requests a second... that's a significat difference. And that is regardless of using register_shutdown_function or doing it directly

Kode
23-07-2014, 14:15
found the issue, lol, I'm missing an extra \r\n before outputting the fields

Kode
23-07-2014, 14:05
I tried register_shutdown_function with the curl version and it made no difference, maybe the opposite in fact it seemed like throughput went down

Kode
23-07-2014, 13:54
interesting, the other interesting thing is they are using that with sockets, so it seems like socket calls should work, but i just cant get them showing up

mike_
23-07-2014, 10:44
I was just looking at this http://code.google.com/p/php-ga/ and can see they use register_shutdown_function to send calls to GA after the script has run. It won't make the calls any faster, but it would get your pages/API results to the client faster as PHP won't block waiting for the GA call to finish.

Kode
22-07-2014, 22:48
I tried changing the curl request to analytics to a socket call, it doesn't seem to have worked though, if I run the curl version I can see it immediately in analytics real-time, but it doesn't seem to show up when using the socket version, anyone have an ideas?

Curl
PHP Code:
        private function track() {
            
//set POST variables
            
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
                
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
            }    
            
$url 'http://www.google-analytics.com/collect';
            
$fields = array(
                
'v' => '1',
                
'tid' => $this->GA_ID,
                
'cid' => $this->gaParseCookie(),
                
't' => 'pageview',
                
'dh' => 'webservice.fanart.tv',
                
'dp' => $this->ttype.' - '.$_GET["api_key"].' - '.$this->project,
                
'dt' => $this->tid,
                
'uip' => $_SERVER['REMOTE_ADDR']
            );
            
$fields_string http_build_query($fields);
            
//die($fields_string);
            //open connection
            
$ch curl_init();

            
//set the url, number of POST vars, POST data
            
curl_setopt($ch,CURLOPT_URL$url);
            
curl_setopt($ch,CURLOPT_POSTtrue);
            
curl_setopt($ch,CURLOPT_POSTFIELDS$fields_string);
            
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
//execute post
            
$result curl_exec($ch);

            
//close connection
            
curl_close($ch);
        } 
Socket
PHP Code:
        private function track() {
            
//set POST variables
            
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
                
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
            }    
            
$url 'www.google-analytics.com';
            
$page '/collect';
            
$fields = array(
                
'v' => '1',
                
'tid' => $this->GA_ID,
                
'cid' => $this->gaParseCookie(),
                
't' => 'pageview',
                
'dh' => 'webservice.fanart.tv',
                
'dp' => $this->ttype.' - '.$_GET["api_key"].' - '.$this->project,
                
'dt' => $this->tid,
                
'uip' => $_SERVER['REMOTE_ADDR']
            );
            
$fields_string http_build_query($fields);


            
$fp fsockopen($url80$errno$errstr5);

            
$output "POST $page HTTP/1.1\r\n";
            
$output .= "Host: $url\r\n";
            
$output .= "Content-Type: application/x-www-form-urlencoded\r\n";
            
$output .= "Content-Length: ".strlen($fields_string)."\r\n";
            
$output .= "Connection: close\r\n";

            
$output .= $fields_string;
            
//die($output);
            
fwrite($fp$output);
            
            
fclose($fp);

        }