Querying Google Analytics with .NET

Sunday, 1 November 2009

Google recently introduced an API for their Analytics service allowing you to query and download the statistics captured. Using a some C# .NET example code as a base, I created a fluent interface (using method chaining) to the API and a console application to call it from.

Calling the API to get the most popular pages for the last day is trivial:

/// 
/// The Google Analytics command line application.
/// 
static class Program
{
  static void Main(string[] args)
  {
    var results = new AnalyticQuery()
                        .WithCredentials(username, password)
                        .SelectTopContent()
                        .ForYesterday();

    Console.Write(new TextFormatter().Format(results));
  }
}

Using the text output formatter produces the following result:

Google Analytics for 02 October 09

Top Content for seogrep.com
===========================
 29 : /
 29 : /site/results
 28 : /site/loading
 10 : /site/domain-results/
  5 : /dead-links/results
  4 : /user/edit
  3 : /page/results/6Z3
  3 : /site/view/IK
  2 : /dead-links/results/bad
  2 : /dead-links/results/ok

Complex Queries and CSV Output

The query object uses object chaining enable you to easily build up a complex query using inteliisense:

/// 
/// The Google Analytics command line application.
/// 
static class Program
{
  static void Main(string[] args)
  {
    var results = new AnalyticQuery()
                        .WithCredentials(username, password)
                        .Select(Metric.Visitors)
                        .Having(Dimension.Date, Dimension.PageTitle)
                        .FromTheDate(DateTime.Now.AddMonths(-1))
                        .ToTheDate(DateTime.Now)
                        .ForDomain("seogrep.com")
                        .OrderDecending()
                        .Top(100);

    Console.Write(new CsvFormatter().Format(results));
  }
}

This would produce the following output (using the CSV formatter):

"www.seogrep.com","12","20091031","Simple SEO Statistics and Tracking - SEO grep"
"www.seogrep.com","8","20091030","Simple SEO Statistics and Tracking - SEO grep"
"www.seogrep.com","8","20091031","Analysing your site - SEO grep"
"www.seogrep.com","3","20091030","Analysis of letsgetdigitaltv.co.uk - SEO grep"
"www.seogrep.com","3","20091031","Dead Links - SEO grep"
"www.seogrep.com","2","20091030","Analysing your site - SEO grep"
"www.seogrep.com","2","20091030","Analysis of Site Root - SEO grep"
"www.seogrep.com","2","20091031","Analysis of Site Root - SEO grep"
"www.seogrep.com","2","20091031","Dead Link Analysis for Site Root - SEO grep"
"www.seogrep.com","2","20091031","Page Contains an H1 Tag Rule Analysis - SEO grep"
"www.seogrep.com","1","20091030","Analysis of nubilus.nl - SEO grep"
"www.seogrep.com","1","20091030","Analysis of www.letsgetdigitaltv.co.uk - SEO grep"
"www.seogrep.com","1","20091030","Analysis of x6grey/ - SEO grep"
"www.seogrep.com","1","20091030","Page URL Contains Underscores Rule Analysis - SEO grep"
"www.seogrep.com","1","20091031","Analysis of demusicalbende.nl - SEO grep"
"www.seogrep.com","1","20091031","Analysis of fashiondays.nl - SEO grep"
"www.seogrep.com","1","20091031","Analysis of index.php - SEO grep"
"www.seogrep.com","1","20091031","Analysis of lookbook.nu - SEO grep"
"www.seogrep.com","1","20091031","Analysis of musical.nl - SEO grep"

You can download the source code here or get a console application.

Building a Microprinter

Tuesday, 22 September 2009

Whilst reading about Papercamp I came across the concept of Microprinting. The idea is simple, take an old receipt printer and repurpose it to stream a live feed of your updates from the Internet. The idea was first mentioned by Matt Webb in his post about social letter boxes, and put into practice by Tom Taylor.

Inspired by this, I decided to make my own micro-printer. I've created a project page that lists the steps, materials, and general details required to make your own microprinter.

At 7:30 each morning I get a daily reports with information such as the tube status, weather and Google Analytics for various sites I maintain. Unlike an iPhone, I can just grab these reports off the printer and not worry about the battery running out or loosing reception on the tube. At the end of the day I shred the reports and put them out for recycling.

Weather Report on Flickr

Examples of the weather and tube report. I cycle to work most days, so the tube report is only printed if it's raining.

Tube Status on Flickr