Google Analytics API PHP Class - Q&A May 21st 2009
Posted May 21st, 2009 in PHP
I've had some more questions about my Google Analytics API PHP Class by email so am posting the information relating to these questions here to help other people out who may have the same issues.
Badly formatted request to the Google Analytics API
When making a call to the ->data() function, if any of the parameters are badly formatting the warning "Badly formatted request to the Google Analytics API; check your profile id is in the format ga:12345, dates are correctly formatted and the dimensions and metrics are correct" will be triggered.
The person emailing a question about this error message was confused about the warning, only looking at the profile id, seeing that was correct and wondering why they were getting an error.
The Google Analytics API will return a 400 error code (which triggers the above error in my code) in any of the following cases, but does not provide any further information:
- The profile id is badly formatted (it should be in the format ga:12345)
- One of the metrics or dimensions used may not exist. Check the documentation to ensure they are spelt correctly and do exist.
- The combination of a metric or dimension may be invalid. Check the documentation to ensure the combinations are valid. Not all of them are.
- The date range is invalid or invalid dates are being passed. They must be in the format YYYY-MM-DD with leading zeroes e.g. 2009-05-01
The warning message in my code tries to cover all bases so make sure you read all the hints :)
Default dates
If the dates are not supplied to the ->data() call they default to yesterday and and 1 month ago, using the strtotime() function like so:
if(!$start) $start = date('Y-m-d', strtotime('1 month ago'));
if(!$end) $end = date('Y-m-d', strtotime('yesterday'));
I had someone email me this morning saying that there is "a small mistake in default dates. They should be "1 month ago -1 day" up to "yesterday" in order to go by the default dates that google analytics sets"
Unfortunately months are not a very good measure of time because they're so in-exact unless you are measuring from the start of one month to the next. I'm fairly certain when I first wrote and tested the class the above code matched the dates in the Google Analytics interface but today when looking they don't. It probably has something to do with the number of days in the month currrently.
However, if I subtract an extra day from the start date as proposed by the person who emailed me, then on June 1st the default month range would be April 30th to May 31st, which is not really ideal because it's not a calendar month. The following code illustrates this:
$x = date('Y-m-d', strtotime('June 1st - 1 month')); // = 2009-05-01
$x = date('Y-m-d', strtotime('June 1st - 1 month - 1 day')); // = 2009-04-30
So I'll be sticking with the way I set the default dates at the moment. I'm not really sure of the relevence of making sure the defaults match Google's defaults for the current day anyway.
A second instance of the API is unauthorized
If you create an instance of the API, login, get some data and then create a second instance of the API just calling the ->data() method you'll get the "Unauthorized request to the Google Analytics API" error. For example:
$api = new analytics_api(); $api->login( ... ); $api->data( ... ); $api2 = new analytics_api(); $api2->data( ... );
This is because the second instance hasn't logged in. However, getting the second one to log in as well will add another second or two of time overhead and is actually unecessary.
The authorization code is a protected property of the class so it can't be read or set outside the class or its descendents. I am going to change this, which would mean you could do this:
$api = new analytics_api(); $api->login( ... ); $api->data( ... ); $api2 = new analytics_api(); $api2->auth = $api->auth; $api2->data( ... );
It also means you could store the auth key in a database (or similar) and re-use it for subsequent requests. Once authorised, the key is valid for approximately 14 days so can be reused without having to log in again each time.
Currently if you run the above code example you'll get an error; to fix this change "protected $auth;" to "public $auth;" and it should work just fine.
Related posts:
- The Google Analytics API and PHP: A series (Tuesday, April 28th 2009)

Comments
blog comments powered by Disqus