Skip to content

The Analytics API in PHP without specifying size

Warning: this article was written over 12 years ago, some information may be out of date.

Exploiting the Analytics API is not complex, and many tools are available.
However, the guides are not always self-explanatory, and finding the right resources is not always easy.

In the case of Analytics, the API has changed a lot over time, but the Google Analytics API PHP Interface has not followed all the changes that have been made.
In particular, the API now allows the size not to be specified, whereas, for the library, it is mandatory.
I, therefore, made a slight change to the library so that it would also accept the null dimension.

In this example, the unique visitors for today are retrieved:

require_once 'gapi.class.mod.php';

define('ga_email','my_email');
define('ga_password','my_password');
define('ga_profile_id','my_id');

$gy = new gapi(ga_email,ga_password);
$gy->requestReportData(ga_profile_id,null,array('visitors','visits'),array('-visitors'),null,'2011-06-04','2011-06-04');

echo $gy->getVisitors();

In the example above, the visits metric is also retrieved.
To understand how to retrieve other metrics and dimensions, there is the excellent Data Feed Query Explorer.

Previous article

Next article

Thread

Leave a comment

Your email address will not be published.