Paging through result data from the Google Analytics API PHP Class

I had a query the other day about paging through Google Analytics API data using my PHP Class; Google allows a maximum of 1000 records to be returned at a time per request so you need to loop until there’s no more data, requesting 1000 records at a time starting from offset 1001, 2001 etc.

Read more about my Google Analytics API PHP class and download the class and example files.

data method parameters

The ->data() method takes a number of parameters which are as follows (I may modify the class (or rewrite) at a later date to make these class properties instead of having so many parameters passed to a function call):

  • $id – the profile id to query data for
  • $dimension – the dimension(s) to retrieve data for
  • $metric – the metric(s) to retrive data for
  • $sort – the sort order for data
  • $start – the start date in YYYY-MM-DD format
  • $end – the end date inYYYY-MM-DD format
  • $max_results – the maximum number of results to return
  • $start_index – the index to start from, if $max_results is 1000 then the second “page” of data would have a start index of 1001
  • $filters – information to filter the data
  • $debug – if true will echo out the URL that is called when asking Google for data

Looping until the end of data

The following data call will get the page path and number of pageviews for all data in a specified date range (last month as of the writing of this post). It then loops through the data returned from Google until there is no more.

$data = true;
 $index = 1;
 $increment = 1000;
 while($data) {
 $data = $api->data($id, "ga:pagePath", "ga:pageviews", "", 
 "2009-05-01", "2009-05-31", $increment, $index);
 // do something with the data here
 $index += $increment;
 }
 

$data is set to true before the loop so it will loop at least once. You could equally do a do { } while() loop and not set $data before the loop but I’m not a fan of do … while.

$index is the index to get data from. It’s incremented at the end of each loop by the $increment amount.

$increment is the number of rows to get each time and also the offset to add to $index at the end of each loop.

When no more data comes back from the GA API while($data) will evaluate to false and the loop will end. Of course, if you have a lot of data for the selected dimension(s) and metric(s) it may take a long time for this to run. You may want to limit the number of queries instead of going for all data in this instance.

Categories PHP

CSS3 rounded corner input

The CSS3 properties border-radius and box-shadow allow a designer to easily create rounded input boxes with pure HTML and CSS without having to resort to images. This post shows how it can be done and deals with vendor prefixes and other cross browser issues to ensure the input boxes work across all browsers.

Connect a USB device with VMWare Server 2

I recently replaced my Windows Vista desktop/host machine with Windows 7 Release Candidate and switched from VMWare Server version 1 to VMWare Server version 2. Version 2 uses a web based management interface and each virtual machine can be accessed in a separate remote console.

I needed to connect a USB printer to one of the virtual machines and was surprised to not see any USB devices shown in the “Devices” drop down box in the remote console. This is shown in the first screenshot below where you can see the virtual CD/DVD devices and network adapter but no USB devices:

show devices with vmware server console

For some reason these don’t show up under the devices drop down box so you have to go into the web management console and connect the USB device there. This is shown in the screenshot below where the red arrow (added for the screenshot) shows the USB device drop down box.

usb device option in vmware infrastructure web access

Click the USB button and it shows the devices that can be connected. This isn’t the most convenient way of connecting a USB device (it would be easier if the USB devices were shown in the device drop down in the remote console) but at least it can be done.

Get the current page path and filename with Javascript

A couple of days ago, I needed to get the current page’s path and script filename with Javascript, excluding the protocol prefix, domain name and any parameters or hashtags which might be present. So here’s another page for future self reference and so I will remember it in the future.