Php 测量协议错误位置

Php 测量协议错误位置,php,google-analytics,traffic-measurement,measurement-protocol,Php,Google Analytics,Traffic Measurement,Measurement Protocol,我编写了以下类来通过GoogleAnalytics测量协议测量服务器端流量 问题是,所有活动访问者都来自意大利,我的服务器当前在意大利-我假设问题是由fsockopen请求IP引起的,那么有没有办法发送用户真实的IP地址 <?php class GoogleAnalytics { public function sendHit($method = null, $info = null) { if ($method && $i

我编写了以下类来通过GoogleAnalytics测量协议测量服务器端流量

问题是,所有活动访问者都来自意大利,我的服务器当前在意大利-我假设问题是由fsockopen请求IP引起的,那么有没有办法发送用户真实的IP地址

<?php
    class GoogleAnalytics {

        public function sendHit($method = null, $info = null) {
            if ($method && $info) {
                // Standard params
                $v   = 1;
                $tid = "UA-xxxxxx-xx";
                $cid = $this->gaParseCookie();
                // Register a PAGEVIEW
                if ($method === 'pageview') {
                    // Send PageView hit
                    $data = array(
                        'v' => $v,
                        'tid' => $tid,
                        'cid' => $cid,
                        't' => 'pageview',
                        'dh' => 'xyz.com',
                        'dt' => $info['title'],
                        'dp' => $info['path']
                    );
                    // Send the data
                    $this->gaFireHit($data);
                }
            }
        }

        // Handle the parsing of the _ga cookie or setting it to a unique identifier
        private function gaParseCookie() {
            if (isset($_COOKIE['_ga'])) {
                list($version, $domainDepth, $cid1, $cid2) = split('[\.]', $_COOKIE["_ga"], 4);

                $contents = array(
                    'version' => $version,
                    'domainDepth' => $domainDepth,
                    'cid' => $cid1 . '.' . $cid2
                );

                $cid      = $contents['cid'];
            } else {
                $cid = $this->gaGenUUID();
            }

            return $cid;
        }

        // Generate UUID v4 function - needed to generate a CID when one isn't available
        private function gaGenUUID() {
            return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 
                // 32 bits for "time_low"
                mt_rand(0, 0xffff), mt_rand(0, 0xffff), 
                // 16 bits for "time_mid"
                mt_rand(0, 0xffff), 
                // 16 bits for "time_hi_and_version",
                // four most significant bits holds version number 4
                mt_rand(0, 0x0fff) | 0x4000, 
                // 16 bits, 8 bits for "clk_seq_hi_res",
                // 8 bits for "clk_seq_low",
                // two most significant bits holds zero and one for variant DCE1.1
                mt_rand(0, 0x3fff) | 0x8000, 
                // 48 bits for "node"
                mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
            );
        }

        // See https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
        private function gaFireHit($data = null) {
            if ($data)
                return $this->socketPost('http://www.google-analytics.com/collect', $data);

            return false;
        }

        private function socketPost($url, $params = array(), $ua = null) {
            // create POST string
            $post_params = array();

            foreach ($params as $key => &$val) {
                $post_params[] = $key . '=' . urlencode($val);
            }

            $post_string = implode('&', $post_params);

            // get URL segments
            $parts = parse_url($url);

            // workout port and open socket
            $port = isset($parts['port']) ? $parts['port'] : 80;
            $success = $fp = @fsockopen($parts['host'], $port, $errno, $errstr, 30);

            if ($fp) {
                // create output string
                $output = "POST " . $parts['path'] . " HTTP/1.1\r\n";
                if (is_string($ua)) 
                    $output .= "User-Agent: " . $ua . "\r\n";
                $output .= "Host: " . $parts['host'] . "\r\n";
                $output .= "Content-Type: application/x-www-form-urlencoded\r\n";
                $output .= "Content-Length: " . strlen($post_string) . "\r\n";
                $output .= "Connection: Close\r\n\r\n";
                $output .= isset($post_string) ? $post_string : '';
                // send output to $url handle
                $success = fwrite($fp, $output);

                fclose($fp);
            }

            return $success ? true : false;
        }
    }
?>

如果您有任何建议,我们将不胜感激。

最后,谷歌将此功能添加到测量协议中,也可以覆盖用户代理

IP Override
parameter: &uip 
Should be a valid IP address. This will always be anonymized just as though &aip (anonymize IP) had been used.
example: &uip=1.2.3.4

User Agent Override
parameter: &ua
Should be a User Agent reported by the browser (don't forget to URL encode the value!). Note: We have libraries to identify real user agents. Hand crafting your own agent could break at any time.
example: &ua=Opera%2F9.80%20(Windows%20NT%206.0)%20Presto%2F2.12.388%20Version%2F12.14

最后谷歌将此功能添加到测量协议中,也可以覆盖用户代理

IP Override
parameter: &uip 
Should be a valid IP address. This will always be anonymized just as though &aip (anonymize IP) had been used.
example: &uip=1.2.3.4

User Agent Override
parameter: &ua
Should be a User Agent reported by the browser (don't forget to URL encode the value!). Note: We have libraries to identify real user agents. Hand crafting your own agent could break at any time.
example: &ua=Opera%2F9.80%20(Windows%20NT%206.0)%20Presto%2F2.12.388%20Version%2F12.14

你能检查一下ClientID是否被正确检索吗?@PetrHavlík ClientID是正确的,不幸的是,我必须找到一种方法来传递用户IP而不是服务器IP。我很确定没有这样的参数:-@PetrHavlík我很抱歉,但由于条款的限制,我们不能在评论/帖子中发送主题URL-无论如何,我对定期访问没有任何问题。我想,及时:-太棒了!!!你能检查一下ClientID是否被正确检索吗?@PetrHavlík ClientID是正确的,不幸的是,我必须找到一种方法来传递用户IP而不是服务器IP。我很确定没有这样的参数:-@PetrHavlík我很抱歉,但由于条款的限制,我们不能在评论/帖子中发送主题URL-无论如何,我对定期访问没有任何问题。我想,及时:-太棒了!!!这真是个好消息!以这种方式设置的IP是否与通过GA过滤器的IP相同?在我看来,我有一个IP排除过滤器,它与“uip”值匹配,但不知何故,命中率仍然可以通过。这确实是个好消息!以这种方式设置的IP是否与通过GA过滤器的IP相同?我的视图中有一个IP排除过滤器,它与“uip”值匹配,但不知何故,命中率仍然可以通过。