0

测量协议文档为我提供了发送批处理请求的这些指示:

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 个(第一个)。

如何向测量协议发送批处理请求?

4

4 回答 4

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',
        )]);
于 2015-12-16T19:29:09.593 回答
1

也许 urls 需要在数据部分,而不是在请求的标题部分?

所以使用 laracurl 的第二个后置参数(来自Laracurl README):

 $response = Laracurl::post($url, ['post' => $data]);

和 :

 $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"));
于 2015-11-30T17:20:51.047 回答
-1

希望对某人有所帮助: 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  
    */
}
于 2018-03-14T21:23:20.287 回答
-1

Google会阻止带有 guzzle 标头的请求。要成功发送请求,您需要替换标头

'headers' => [
   'User-Agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101',
],
于 2021-08-26T10:55:11.037 回答