Edit: I’m not satisfied at all by the API below. Hopefully i will find time to give it some love, perhaps using dynamics.
I’m not that fond of the official GData library and especially not the Analytics wrapper. Since i only needed a small subset of the function of all available features, i’ve developed my own library.
Let me introduce AnalyticsApi.
AnalyticsApi is still in a very early stage but it’s used in production in one of our projects at work. My goal is to create a easy to use, strongly typed library. It should be both unit- and integration testable.
It has a fluent, easy to use, interface:
var dashboard = new AnalyticsService(new AnalyticsDataProvider(HttpWrapper.Standard)) .Username("username@gmail.com") .Password("password") .Logon() .Profile(1312231) .GetDashboard("2011-01-01", "2011-01-30"); Console.WriteLine("PageViews: {0}", dashboard.PageViews); Console.WriteLine("Bouncrate: {0}", dashboard.Bouncrate); Console.WriteLine("Visits: {0}", dashboard.Visits);
The AnalyticsService has a dependency on a AnalyticsDataProvider which allows you to mock the results and test it in isolation.
Each request is defined as a request map, similar to FluentNHibernate. A request for dashboard data would be defined as the following:
class DashboardApiMap : ApiMap<DashboardRequest> { public DashboardApiMap() { Map(x => x.Visits, "ga:visits"); Map(x => x.PageViews, "ga:pageviews"); Map(x => x.Bounces, "ga:bounces"); Map(x => x.Entrances, "ga:entrances"); Map(x => x.TimeOnSite, "ga:timeonsite"); Map(x => x.NewVisits, "ga:newvisits"); Map(x => x.StartDate, "startDate", ElementLevel.FeedLevel); Map(x => x.EndDate, "endDate", ElementLevel.FeedLevel); } }
DashboardRequest is just a simple POCO with some methods for calculation for example calculating the bounce rate.
One thing i’m aming to fix pretty soon is rate limit checks and make it even easier to define new requests. I’m thinking about using extension methods for this. The AnalyticsService should also implicitly use the “live” AnalyticsDataProvider if nothing else is specified.
This library is far from finished, but i’m going to give it some love pretty soon.
Check out AnalyticsApi at GitHub.