Google Analytics API in C#

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.


  • Kingen

    Keep up the good work!

  • http://www.o7thwebdesign.com Kevin Pirnie

    Great work on this.  I wonder if you might be able to help me out though.

    I am currently testing it with my google account.  I have 3 sites setup under it, but, when trying to pull GetAccounts, #1 I am getting 3 records returned, but they all are equal to the first record.  #2 I am wondering how hard it would be to put in the node for this…  it would be real handy to be able to supply a drop-down with the profile names…

    • Alexander Nyquist

      Kevin,

      to be honest, i don’t like the syntax and usage of this library anymore. I’m going to improve it and give it some more love. In the meantime, let me try to investigate that and come back to you.

      Alex

      • http://www.o7thwebdesign.com Kevin Pirnie

        :)
        Honeslty…I don’t like it either ;)   It’s in c#  i’m a vb.net guy LMAO

        I got #2 cured.  I added another value to the Enum ElementLevel and added a check for it in GetXmlValue, and added another method:
                private string GetXmlTopLevelValue(XDocument doc, string path)
                {
                    if (doc.Root == null) return string.Empty;

                    var ns = doc.Root.GetDefaultNamespace();

                    var node = doc.Descendants(ns + path);
                    return node.Any() == false ? string.Empty : doc.Descendants(ns + “entry”).Descendants(ns + path).First().Value;
                }

        #1 is the really the issue now.  thanks man!

      • http://www.o7thwebdesign.com Kevin Pirnie

        p.s.   Thanks for this.  So far it’s the only thing I’ve seen that actually works.

  • Andrey Druz

    Hi,
    How can I use GetTopTrafficSources?
    Thanks

    • Alexander Nyquist

      Hi Andrey,

      I’m sorry, it’s not implemented yet. As I mentioned above, i’m not happy with the current API. When I get time, i’ll spike out a new one.

      Hope this helps,

      Alexa