【问题标题】:What is the fastest way to send details to google analytics from an API从 API 向谷歌分析发送详细信息的最快方法是什么
【发布时间】:2014-09-14 09:41:46
【问题描述】:

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

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

我不确定套接字 1 的代码是否有错误,或者谷歌分析是否出于某种原因不喜欢它,有什么想法吗?

套接字代码:

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);
}

【问题讨论】:

    标签: php sockets curl google-analytics


    【解决方案1】:

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

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

    需要

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

    之后跟踪开始工作

    此外,我发现在某些主机上查找主机名导致其速度变慢,在将 IP 保存在 memcache 中后,我对其进行单元测试的廉价 VPS 从平均每秒 15 个请求变为每秒大约 1,000 个请求。

    更新后的工作代码如下:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      相关资源
      最近更新 更多