从API发送详细信息到google分析的最快方式是什么?


What is the fastest way to send details to google analytics from an API

我的API目前通过cURL请求发送分析跟踪,然而,这确实降低了API可以处理的请求数量,所以我尝试通过套接字发送它并忽略输出,但是分析似乎没有跟踪它。

我在下面附上了两组代码,当使用curl时,我可以看到它立即显示在实时分析中,当使用socket时,实时分析似乎并没有真正改变。

我不确定是否有一个错误的代码套接字一个或如果谷歌分析只是不喜欢它的某种原因,任何想法?

套接字代码:

private function track() {
    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($url, 80, $errno, $errstr, 5);
    $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;
    fwrite($fp, $output);
    fclose($fp);
}

旋度代码:

private function track() {
    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);
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
}

不出所料,这最终是一个简单的错误。

$output .= "Connection: close'r'n";
$output .= $fields_string;

必须是

$output .= "Connection: close'r'n'r'n";
$output .= $fields_string;

之后,跟踪开始工作

另外,我发现在一些主机上查找主机名导致它变慢,在memcache中保存IP后,我正在进行单元测试的廉价VPS从平均每秒15个请求到每秒约1000个请求。

更新后的工作代码如下:

public function track() {
    $url = 'www.google-analytics.com';
    $page = '/collect';
    $googleip = $this->memcacheget('googleip');
    if(empty($googleip)) {
        $googleip = gethostbyname($url);
        $this->memcacheset('googleip', $googleip, 3600);
    }

    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
        $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
    }   
    $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'],
        'ua' => $_SERVER['HTTP_USER_AGENT']
    );
    $fields_string = http_build_query($fields);
    $fp=fsockopen($googleip, 80, $errno, $errstr, 5);
    stream_set_blocking($fp, 0);
    stream_set_timeout($fp, 5);
    $output = "POST http://".$url.$page." HTTP/1.1'r'n";
    $output .= "Host: $url'r'n";
    $output .= "Content-Length: ".strlen($fields_string)."'r'n";
    $output .= "Connection: close'r'n'r'n";
    $output .= $fields_string;
    $sentData = 0;
    $toBeSentData = strlen($output);
    while($sentData < $toBeSentData) {
        $sentData += fwrite($fp, $output);
    }
    fclose($fp);
}
public function memcacheget($key){
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211);
    $result = $memcache->get($key);
    return $result;
}
public function memcacheset($key,$value,$timeout=86400){
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211);
    //$memcache->flush();
    $result = $memcache->get($key);
    if(empty($result)){  //store in memcache
        $memcache->set($key,$value,MEMCACHE_COMPRESSED,$timeout);
    } else {
        $memcache->replace($key,$value,MEMCACHE_COMPRESSED,$timeout);
    }
    return $result;
}