Google Analytics can return gziped data


Please Share

Google Analytics

Get gZip Google Analytics Data

As part of my recent Google Analytics documentation digging, I ran across something I hadn’t seen before.     The Core Reporting API can return data zipped.    

Using gzip

An easy and convenient way to reduce the bandwidth needed for each request is to enable gzip compression. Although this requires additional CPU time to uncompress the results, the tradeoff with network costs usually makes it very worthwhile. In order to receive a gzip-encoded response you must do two things: Set an Accept-Encoding header, and modify your user agent to contain the string gzip. Here is an example of properly formed HTTP headers for enabling gzip compression:

Accept-Encoding: gzip
User-Agent: my program (gzip)

 

If you make a request that returns a million rows and you can only get them in 10k row batches that’s a lot of requests to run against the API.    If the API is capable of  returning the whole thing in a gzip file this has the potential of being very useful.    We will still have to unzip the file but at least we wont have to deal with the extra hits to the daily quota limit using the nextlinks.

I have spent the night digging around the Google Dot.net Client lib,  now my question is.    Is this supported in the Client library?  or am I going to have to send this request the hard way?

I suspect it is supported by the client lib,  because CreateRequest seams to be over ride-able to create a gzip.     But when i run the execute on the code it doesn’t seam to do anything.   It must return some kind of download stream like the Google Drive API when you download a file.     Maybe i should dig around in the Documentation for Google Drive .net see if i can find anything there that will lead me down the correct path to gziped Google Analytics data. 

Code as it stands now

string _profileid =”ga:78110423″;
string _startDate = “2013-08-01”;
String _endDate = “2014-08-01″;
String _metrics=”ga:entrances,ga:pageviews,ga:exits,ga:bounces,ga:timeOnPage”;
string _dimensions = “ga:date,ga:hour”;
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(_profileid, _startDate, _endDate, _metrics);
request.SamplingLevel = DataResource.GaResource.GetRequest.SamplingLevelEnum.HIGHERPRECISION;
request.Dimensions = _dimensions;
request.CreateRequest(true);
var t = request.Execute();

 

To be continued …

 

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog www.daimto.com. This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.

Leave a comment

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