0

Measurement protocol docs give me these directions to send a batch request:

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

Im a little confused on how i would build my url for this?

What i have tried:

$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"
            ]);

This does not work, there should 5 new events in ga, but i only receive 1 (the first one).

How can i send a batch request to the measurement protocol?

Anthony Raimondo
  • 1,621
  • 2
  • 24
  • 40
  • What you want, is the `curl_multi_*` family. Which seems not to be wrapped by Laracurl, at first sight. – eyecatchUp Dec 01 '15 at 13:19
  • im having the same issue, im confused because the docs kinda state that you should send the same keys consecutively. So order would matter? Usually the way to do this would be `v[0]=1&t[0]=event...v[1]=1&t[1]=pageview` but the batch protocol just line-separates the post data? – Flame Dec 02 '15 at 15:17
  • @eyecatchUp please post an answer if you know a way – Anthony Raimondo Dec 03 '15 at 18:19
  • First try things with https://ga-dev-tools.appspot.com/hit-builder/ – Vineet1982 Dec 15 '15 at 06:11
  • @Vineet1982 i have it does not allow for batch request testing from what i can see. I can send single request just fine, but batch does not seem to work, i cant understand what google docs is asking for in there request. – Anthony Raimondo Dec 15 '15 at 15:55

3 Answers3

3

If you use GuzzleHttp, you should use body instead of 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',
        )]);
Gennadiy Litvinyuk
  • 1,536
  • 12
  • 21
  • For some reason, I had to specify a `null` User-Agent in `headers`. (for reference, Guzzle was setting `User-Agent: GuzzleHttp/6.5.1 curl/7.29.0 PHP/7.4.3`) – jerkan Jul 31 '20 at 09:04
1

Maybe urls need to be in the data part, not in the header part of request ?

So use the second post parameter of laracurl (from 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"));
Alice Oualouest
  • 836
  • 12
  • 20
-1

Hoping help someone: Analytics Management API v3

Creating a new goal

(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  
    */
}
Angie
  • 255
  • 3
  • 7