Batching features in the Google Analytics APIs

jsnGoogle Analytics0 Comments

Sending HTTP requests in a batch – a small hidden feature of the Google Analytics APIs that does not get the attention it deserves. In this blog, I’ll walk you through the feature and showcase how to make your code more efficient.

Google Analytics provides a set of really useful APIs that allow you to programmatically interact with the platform and scale and automate various tasks using the Core Reporting API, the Management API, the User Deletion API and others.

For any operation that you are executing using the APIs, there is a resulting API call to the Google Analytics’ servers and an establishment of a HTTP connection. The more operations you are executing, the more overhead that is generated at the server side to process the requests. A typical HTTP session consists of the following steps:

  1. The client establishes a connection to the server
  2. The client sends its request
  3. The server processes the request, providing back a response and a status code with some data

Now, what the Batch request feature provide is the option to bundle all of the requests you are sending into one and establish a connection and wait for a response only once, instead of doing that for every single request.

In other words, if you want to buy 4 items from the shop, would you make 4 separate journeys or you would go once and buy all of the items together? That is what the Batch Requests feature does – it makes your shopping experience in the world of the Google Analytics APIs more efficient!

Use cases

Let’s look at a few examples of when the Batch Requests feature is handy.

  • Creating a set of custom dimensions or custom metrics across multiple GA Properties via the Management API. In this case, instead of making a single request per custom dimension/metric and per property, you can bundle them into only one HTTP request listing all of the required operations.
  • Updating the permissions of multiple users with the Management API. Apart from simplifying the request, using the Batch Request feature here also has additional performance and quota benefits. Moreover, Every 30 batched user permission change API requests counts as only a single write operation and you can bundle up to 300 requests within a single batch.
  • Sending multiple offline hits when using the Measurement Protocol. Similarly, when you are sending hits to GA from an offline environment, you can make them more efficient using the Batch Request feature. There is a limit of 20 requests within the batch, and it the total size of the payload should not go over the 16k bytes limit, but nevertheless a great way to optimize your usage.
  • Generating multiple reports on a regular basis using the Reporting API. Performance gains can also be found here when you have a setup that regularly queries Google Analytics to pull numbers for multiple reports. You can bundle your request to the servers allowing for a quicker response and less overhead generated to the servers.

The list can go on and on and there are a dozen of use cases for each of the available APIs. The key takeaway here is that the Batch Requests feature exists for a reason and optimizing the APIs usage is a must, especially when scaling up your operations.

Limitations

Batching saves up on the overhead of constructing multiple HTTP requests, however, each Google Analytics API request within the batch request still counts against the daily project quota. By default, a project can make up to 50,000 requests per day and batching does not change that.

There is a limit of up to 1000 calls in a single batch request. If more than 1000 calls are required, split them into multiple batch requests.

Summary

The Batch Requests feature of the Google Analytics APIs (when considering the standard usage limits) has multiple benefits and provides a method to optimize your code and API usage. It can be applied in various scenarios and is fairly simple to use.

Leave a Reply

Your email address will not be published. Required fields are marked *