Measurement protocol docs给我这些发送批量请求的指示:
POST /batch HTTP/1.1
Host: www.google-analytics.com
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact
我对如何构建我的网址感到困惑?
我尝试了什么:
$guzzle = new \GuzzleHttp\Client();
$guzzle->request('POST','www.google-analytics.com/batch',[
'query' => "v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event"
]);
这不起作用,ga中应该有5个新事件,但我只收到1个(第一个)。
如何向测量协议发送批量请求?
答案 0 :(得分:2)
如果您使用GuzzleHttp
,则应使用body
代替query
:
$guzzle = new \GuzzleHttp\Client();
$guzzle->request('POST','www.google-analytics.com/batch', [
'body' => implode("\n", array(
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
)]);
答案 1 :(得分:1)
也许网址需要在数据部分,而不是在请求的标题部分?
所以使用laracurl的第二个post参数(来自Laracurl README):
$response = Laracurl::post($url, ['post' => $data]);
with:
$data = join("\n",array(
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome",
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout",
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact"));
答案 2 :(得分:0)
希望帮助某人: Analytics Management API v3
创建新目标
(PHP Google_Client,Google_Service_Analytics)
/*
Assuming there is a $goals array
*/
$client = new Google_Client();
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->addScope([Google_Service_Analytics::ANALYTICS_READONLY,
Google_Service_Analytics::ANALYTICS_EDIT]);
$client->setDeveloperKey($API_KEY);
$client->setSubject($EMAIL);
$client->refreshToken($REFRESH_TOKEN);
$client->setUseBatch(true);
$analytics = new Google_Service_Analytics($client);
$batch = $analytics->createBatch();
foreach($goals as $goal){
$req1 = $analytics->management_goals->insert(XXXXXX, UA-XXXXXX-1, XXXXXX, $goal);
$batch->add($req1);
}
try {
$batchResponse = $batch->execute();
/* Handling Response */
foreach ($batchResponse as $key => $value) {
if(!($value instanceof Google_Service_Exception)){
echo $value->getId()."\n";
continue;
}
print_r($value->getErrors());
}
} catch (Google_Service_Exception $e) {
/*
handling exception
*/
}