Google Analytics Screen Widths with PHP (Internet Explorer)
Posted December 24th, 2008 in PHP
A week ago I looked at copying and pasting the screen resolution data from Google Analytics and parsing it to get just the screen widths and percentages with a PHP script using Firefox. The copy-paste format is different for Internet Explorer so this followup post modifies the script for IE.
Recap
The following paragraphs are repeated from the previous post to look at the issue.
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.

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.
The IE solution
If you select all the text in the screen resolution table in Internet Explorer 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% 5. 1680x1050 1,109 5.46% 6. 800x600 1,018 5.01% 7. 1152x864 520 2.56% 8. 1280x768 394 1.94%
So the PHP script needs to extract the width part from the screen resolution (e.g. the 1024 part of 1024x768 in the first line) and the percentage, sum them up and then display them. The commented script for Firefox is below. Note that this was done with Internet Explorer 7 on Windows Vista.
$rows = explode("\r\n", file_get_contents('/path/to/file.txt'));
$data = array();
// a line looks like this: 1. 1024x768 7,805 38.41%
for($i = 0; $i < count($rows); $i++) {
preg_match('/([0-9]+)x.+([0-9.]+)%/U', $rows[$i], $matches);
// resolution width is in $matches[1] and percentage in $matches[2]
// add to the data array, note that the percentage can be 0.00% so don't count those ones
if($matches[2] > 0) {
if(isset($data[$matches[1]])) {
$data[$matches[1]] += $matches[2];
}
else {
$data[$matches[1]] = $matches[2];
}
}
}
// 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 Internet Explorer. Firefox will copies and pastes the data slightly differently so if you use Firefox then be sure to check out the other post here.
Related posts:
- Google Analytics Screen Widths with PHP (Firefox) (Wednesday, December 17th 2008)
- Validating numbers with PHP (Tuesday, November 25th 2008)
- Intialise an array in PHP with default values (Tuesday, October 7th 2008)
- Return information from PHP print_r instead of displaying it (Tuesday, September 30th 2008)

Comments
blog comments powered by Disqus