Search code examples
curlgoogle-analyticsphp-7guzzlemeasurement-protocol

google Measurement Protocol giving weird response


I am trying to set this up using php7 and guzzle for my rest API. when i send the request in curl using cli to the debug url i get a nice json response

{
  "hitParsingResult": [ {
    "valid": true,
    "parserMessage": [ ],
    "hit": "/debug/collect?v=1\u0026t=pageview\u0026tid=UA-1234567-1\u0026uid=UA-1234567-1\u0026dh=test.domain.com\u0026dp=/api/user/verify/flow"
  } ],
  "parserMessage": [ {
    "messageType": "INFO",
    "description": "Found 1 hit in the request."
  } ]
}

but if i send it to prod url i get weird response everytime

GIF89a▒▒▒▒▒,D;

does anyone know how to solve this?

this is what i run on cli

curl -X POST -d 'v=1&t=pageview&tid=UA-xxxxxxxxx-1&uid=UA-xxxxxxxxx-1&dh=test.domain.com&dp=%2Fiwsapi%2Fuser%2Fverify%2Fflow' https://www.google-analytics.com/debug/collect

for non debug mode i just remove the the debug

curl -X POST -d 'v=1&t=pageview&tid=UA-xxxxxxxxx-1&uid=UA-xxxxxxxxx-1&dh=test.domain.com&dp=%2Fiwsapi%2Fuser%2Fverify%2Fflow' https://www.google-analytics.com/collect

In guzzle i am building it like so

$promise = $guzzle->post("/debug/collect", [
                                'proxy' => [
                                    'http'  => 'http://proxy:8080',
                                    'https'  => 'http://proxy:8080', 
                                ],
                                'form_params'=>[
                                    'v'=>1,
                                    't'=>'pageview',
                                    'tid'=>GOOGLE_ID,
                                    'uid'=>$_SERVER['HTTP_CTVFNTUSERNAME'],
                                    'dh'=>WWWHOST,
                                    'dp'=>$_SERVER['REQUEST_URI'],
                                    //'dt'=>'homepage'
                                ]
                            ]);

Solution

  • Response Codes

    The Measurement Protocol will return a 2xx status code if the HTTP request was received. The Measurement Protocol does not return an error code if the payload data was malformed, or if the data in the payload was incorrect or was not processed by Google Analytics.

    If you do not get a 2xx status code, you should NOT retry the request. Instead, you should stop and correct any errors in your HTTP request.

    The measurement protocol doesn't actually validate your hits in anyway the only response you will ever get is an 1x1 pixel image that being GIF89a▒▒▒▒▒,D;

    So there is really nothing to solve its working as it should. The only one that will send you a proper response is the debug endpoint which you appear to be using