10

我尝试使用谷歌分析来跟踪更多自定义数据。所以我想我使用事件。以下是我尝试替换uuidand的代码user agent

<?php
function gen_uuid() {
return 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 )
);
}

$data = array(
'v' => 1,
'tid' => 'UA-********-**',
'cid' => gen_uuid(),
't' => 'event'
);


$data['ec'] = "category";
$data['ea'] = "product";
$data['el'] = "element";
$data['ev'] = "34";


$url = 'http://www.google-analytics.com/collect';
$content = http_build_query($data);
$content = utf8_encode($content);
$user_agent = 'Example/1.0 (http://example.com/)';


$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_exec($ch);
curl_close($ch);
?>

我错过了什么吗?

4

2 回答 2

4

我已经测试了你的代码并且它有效。

但为了帮助您调试,请将 /collect 路径更改为 /debug/collect,Google 将验证您的命中,请尝试:

<?php
function gen_uuid() {
return 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 )
);
}

$data = array(
'v' => 1,
'tid' => 'UA-1111111-1',
'cid' => gen_uuid(),
't' => 'event'
);


$data['ec'] = "category";
$data['ea'] = "product";
$data['el'] = "element";
$data['ev'] = "34";


$url = 'https://www.google-analytics.com/debug/collect';
$content = http_build_query($data);
$content = utf8_encode($content);
$user_agent = 'Example/1.0 (http://example.com/)';


$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_exec($ch);
curl_close($ch);
?>

并且,请务必转到实时报告以检查您的点击是否正在处理中。

于 2015-12-06T23:22:37.703 回答
1

你可以试试这个

        <?php
        function gen_uuid() {
        return 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 )
        );
        }

        $data = array(
        'v' => 1,
        'tid' => 'UA-********-**',
        'cid' => gen_uuid(),
        't' => 'event'
        );


        $data['ec'] = "category";
        $data['ea'] = "product";
        $data['el'] = "element";
        $data['ev'] = "34";


        $url = 'http://www.google-analytics.com/collect';
        $content = http_build_query($data);
        $content = utf8_encode($content);
        $user_agent = 'Example/1.0 (http://example.com/)';


        $ch = curl_init();
        curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
        curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
        curl_setopt($ch,CURLOPT_POST, TRUE);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
        curl_exec($ch);
        curl_close($ch);
        ?>

我认为这可能有效

于 2015-04-12T07:40:50.170 回答