Calculating a page’s bounce rate with the Google Analytics API

I had an email a couple of days ago from someone asking me how to calculate the bounce rate for a page using my PHP Class for the Google Analytics API.

The formula supplied from Google from the “Common Calculations” section of the Analytics API documentation says the bounce rate for a particular page should be caculated like this:

ga:bounces/ga:uniquePageviews,ga:pagePath
 

To work this out using my PHP Class you would do something like the following example. I’ve multiplied the caculation up by 100 to give a percentage in this example.

$data = $api->data($id, 'ga:pagePath', 'ga:bounces,ga:uniquePageviews');
 foreach($data as $page_path => $row) {
 $bounce_rate = round(100 * $row['ga:bounces'] / $row['ga:uniquePageviews'], 2);
 echo "$page_path $bounce_rate%n";
 }
 

The round(…, 2) rounds the number to 2 decimal places (see my rounding numbers with PHP post for more details).

The above example would output the following from my own data as at the time of writing this post.

/jquery-get-set-form-values/ 87.17%
 /article/apache/restart-apache/ 94.31%
 /article/mysql/delete-all-data-mysql/ 91.23%
 /php-http-referer-variable/ 92.74%
 /check-uncheck-checkbox-jquery/ 86.73%
 /article/mysql/cross-table-update/ 88.99%
 /article/networking/open-firewall-msn-icq/ 94.37%
 /article/linux-unix-bsd/howto-check-md5-file/ 94.75%
 /upper-lower-case-strings-mysql/ 95.69%
 /install-yum-with-rpm-on-centos/ 87.36%
 

The defaults for the ->data() method will get data for the month to yesterday and for the first 10 records only. This can be modified to a different date range and/or number of records. For example, to get the same data for 1000 records for the same date range you could do this:

$data = $api->data($id, 'ga:pagePath', 'ga:bounces,ga:uniquePageviews', '', '', '', 1000);
 

I’ve realised in writing this I don’t actually have a post dedicated to the parameters for the data call so will write one and post it tomorrow.

Categories PHP

var_dump or print_r inside a Smarty template

While trying to debug why something wasn’t working, I needed to see what data had been assigned to a Smarty template but from within the template itself (I didn’t have time to try and work out where in the PHP code the stuff was assigned and debug from there). This post shows how to do a var_dump or print_r from within the Smarty template itself.

Categories PHP

PHP parse_url error for https requests from “Test Certificate Info”

The PHP websites I manage email me all notices, warnings and errors and I occasionally get warnings resulting from bots requesting bad URLs. This post looks at a PHP parse_url error caused by an incorrect piece of code that was published which has since been corrected, but there are clearly still scripts out there using the old code and making bad requests.

Categories PHP

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

MAMP PHP cURL and SSL

I currently develop PHP based websites on a Mac using MAMP. When testing the Interspire Email Marketer API on a local version of a site posting data through to an https:// URL with cURL, I discovered that the version of cURL on MAMP has some issues with certs and SSL.

Categories PHP