Google Analytics Screen Widths with PHP (Firefox)Google Analytics Screen Widths with PHP (Firefox)

Posted December 17th, 2008 in PHP

Google Analytics has screen resolution information but groups them by both width and height. I was wanting to know the percentage of visitors by width so created a PHP script to parse text copied from the Analytics page and sum up the percentage by screen width.

The screenshot below shows the first 8 entries in the screen resolutions for one of my sites. You can see that in this 8 alone 1280 wide appears 3 times (1280x800, 1280x1024x 1280x768) and several widths appeared multiple times.

google analytics screen resolution data

I didn't really feel like getting out the calculator and having to work it out for all of them so decided to create a PHP script to parse the data and that way I can use it again and again.

Note that this PHP script simply parses the data saved to a text file from a copy and paste from the web page in your browser. It doesn't log into Analytics and do it all for you.

If you select all the text in the screen resolution table in Firefox and save it to a text file you'll end up with some data like this:

1.    
1024x768
    7,805     38.41%    
2.    
1280x800
    4,047     19.92%    
3.    
1280x1024
    2,907     14.31%    
4.    
1440x900
    1,358     6.68%    

So the PHP script needs to read three lines at a time, extract the resolution width and percentage, sum them up and then display them. The commented script for Firefox is below. Note that this was done with Firefox 3 on Windows Vista.

$rows = explode("\r\n", file_get_contents('/path/to/file.txt'));
$data = array();

// every three rows contains
// 0 => the line number
// 1 => the resolution e.g 1024x768 (which is width X height
// 2 => the number and percentage e.g. 7,805 38.41%
// so we loop through every three rows at a time

for($i = 0; $i < count($rows); $i+=3) {
  // by (int)ing the resolution we end up with the width part only
  $res = (int)$rows[$i+1];
  // get the percentage using a regular expression
  preg_match('/([0-9.]+)%/', $rows[$i+2], $matches);
  // and add to the data array
  // note that the percentage can be 0.00% so don't count those ones
  if($matches[1] > 0) {
    if(isset($data[$res])) {
      $data[$res] += $matches[1];
    }
    else {
      $data[$res] = $matches[1];
    }
  }
}

// sort in reverse order, maintaing the key field
arsort($data);
// and display
print_r($data);

This will output something along the lines of this:

Array
(
    [1024] => 38.72
    [1280] => 37.55
    [1440] => 6.71
    [1680] => 5.46
    [800] => 5.1
    [1152] => 2.76
    ....
)

So that's how to do it in Firefox. Internet Explorer will copy and paste the data slightly differently so this time next week there will be a followup post showing how to do this for Internet Explorer.

Related posts:

Share or Bookmark

Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.

Subscribe or Follow

Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

Comments

blog comments powered by Disqus