2014-03-29 102 views
9

我試圖用谷歌分析來追蹤一些更多的自定義數據。所以我想我使用這些事件。以下是我試過的代碼,我取代了uuiduser 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); 
?> 

我缺少的東西?

+0

你爲什麼覺得'我錯過了什麼'? –

+0

,因爲它似乎不起作用。或者至少我沒有在谷歌分析中記錄任何事件。 – user2693017

+1

你可以添加一些'$ data'的最終值的例子嗎?最後,當您處理測量協議 – Eduardo

回答

0

你可以試試這個

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

我想這可能工作

3

我測試你的代碼和它的作品。

但是,爲了幫助您調試這一點,改變/調試/收集路徑/收集和谷歌將驗證你的命中,嘗試:

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

而且,一定要到實時報告,以檢查如果你的命中正在處理。

+0

這很好/調試/收集謝謝你。 – dsum27

+0

難道人們無法使用它添加數據給其他人嗎?如果你知道他們的UA。 –

+1

當然Gary!定期檢查您的主機名稱報告和/或實施一些命中過濾器以確保您的官方查看/報告僅包含所需的數據收集非常重要。 –