3

I'm using measurement protocol for my desktop application.

With this following URL I able send single request to Google Analytics(GA).

https://www.google-analytics.com/collect?v=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0

But I want to send multiple request to GA. According to the documentation, with /batch we can send multiple requests.

I have tried this URL,

https://www.google-analytics.com/batch?
v=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test1&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0
&v=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test2&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0
&v=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test3&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0

But in report only 3rd event getting recorded.

Please help me to fix this issue.

Divesh Oswal
  • 250
  • 3
  • 13

2 Answers2

7

You should send the payload/data in the body as raw text and on separate lines. Also, make sure you make a POST request. That worked for me. Here is an image showing how this looks in Postman:

Batch request to Google Analytics Measurement Protocol

From Postman you can then generate the code for the language you use. E.g for PHP Curl it looks like this.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.google-analytics.com/batch",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "v=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test1&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0\r\nv=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test2&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0\r\nv=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test3&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: text/html",
    "postman-token: de143f21-c12e-d268-32a0-9e5101541a07"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Søren Dam
  • 1,545
  • 1
  • 15
  • 17
1

Useful post from @Søren about Postman, which I've never used before. Unfortunately, it still took me some time to figure out why my call to https://www.google-analytics.com/batch wasn't working in Javascript, citing a CORS 403 error as the issue. In Postman it was working fine, but the JS output from Postman wasn't.

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://www.google-analytics.com/batch",
  "method": "POST",
  "headers": {
    "cache-control": "no-cache",
    "postman-token": "bec425da-11af-ec17-f702-fd7d01133ee4"
  },
  "data": "v=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test1&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0\r\nv=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test2&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0\r\nv=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test3&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0"
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

So using Fiddler and comparing the call Postman makes, the only real difference I could see in Raw view was Postman was using POST https://www.google-analytics.com/batch where as JS was using OPTIONS https://www.google-analytics.com/batch. By executing the raw script and changing it from OPTIONS to POST it worked fine. So why wasn't mine sending as POST? I then read something about the headers need to match otherwise it won't be executed as POST. So the solution? Remove the headers...

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://www.google-analytics.com/batch",
  "method": "POST",
  "data": "v=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test1&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0\r\nv=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test2&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0\r\nv=1&tid=UA-XXXXXX-1&cid=754654B98786B&t=event&ec=Test3&ea=click&cd=XYZ&an=XYZ&aid=123&av=3.0&aiid=1.0"
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

It took me a considerable amount if time to get this to work, for something so simple, and hope this will help someone else out.

Andi
  • 115
  • 4