Friday, 7 August 2015

Using the Google Analytics API in .NET

An old application which talks to Google Analytics (GA) in order to produce some custom reports started throwing an error the other day. It turns out Google has discontinued the old way in which you connect to their GA API. They also seem to have been through a few iterations of how you connect to them since the application was originally written.

Unfortunately, details on language specific implementation examples (C# / .NET in my case) don't seem to be easily available. The official documentation appears particularly confused - it's been updated so many times you're never sure whether you're looking at the latest and greatest or some deprecated version.

After some searching about I found the following guide really useful:

In essence it seems to boil down to the following:

1) You used to connect to the Google Analytics API with a straight-forward username and password combination. There is a token returned after successful credentials being supplied, which apparently didn't need to be used, at least in the code I was looking at.

2) Connecting now is done by registering your application in the Google Developer Console and then generating a public/private key pair, of which you'll use the private key in your application.

3) This application will have an email associated with it which looks like You'll need to make sure this account is authorised to read the Google Analytics accounts you want to pull info from.

4) At the time of writing, in order to utilise the asymmetric key combo you need to nuget in the GoogleAnalytics .net library: You use the private key to create an X509Certificate2 which you pass to the a new instance of the AnalyticsService.

It's a shame that by far the best documentation and examples are by third-parties! Of which I do not count myself one.

No comments:

Post a Comment