Using The Google Analytics API With Asp.Net MVC
by Jim Geurts on Feb.17, 2012, under Web Development
I’m a big fan of wordpress and really like the Google Analyticator plugin. It provides a dashboard widget that summarizes your analytics stats. I was working on a project that had a relatively sparse admin dashboard and figured I would provide a similar view of analytics stats. The main difference is that the project I was working on was an asp.net mvc3 project.
I searched for some analytics api examples for asp.net and didn’t really find much. So hopefully this will help someone who is looking to consume gdata apis from .net. All of the code is available on github for your viewing pleasure.
What does it look like?
Authenticating with Google Data Api
Authenticating with Google is pretty simple. Google supports a variety of authentication methods including OAuth2. I’m not entirely sure why, but I chose to go with their AuthSub method of authentication. Basically, you redirect the user to a specific Google url, they authorize your application and are returned to your application with a token. You can then use that token for one-time operation or convert the token to a long lasting token that can be used repeatedly.
The code I use to authenticate looks like:
I store the long lasting token in an embedded instance of RavenDb. It’s a quick & super easy way to store things like that.
Querying Analytics
Now that you have a token from the user, you can query analytics to get data for any site that the user has access to. Google sites have a unique id associated with them, that Google calls TableId. You need to specify this value with any analytic data query. In order to get a list of available sites, you’ll need to query the account feed:
With that snippet, I organize the sites based on their associated account.
After choosing a specific site to work with, you can query information like total page views, average amount of time spent on the site, etc. The data feed explorer is quite handy in exploring the type of data that Analytics will return. For example, the following query will get a number of general stats across all pages:
Similarly, you can add dimensions to your query. Dimensions allow you to group the metrics data, similar to sql group by. The following query will get a list of pages sorted by most page views:
The UI side of things
The graph is generated using the jquery sparkline plugin. While not as sexy as the official analytics graphs, it works for a quick view. Everything else on the page is styled with twitter bootstrap, with a few minor tweaks.
Conclusion
That’s about it. Have a look at the complete project to get a full understanding of how I used the analytics api to build an asp.net mvc widget that summarizes website analytics.

February 21st, 2012 on 11:14 am
Love it, this is exactly what I’ve been looking at for a project.
Thanks for making my life easier.
Bert
April 9th, 2012 on 3:26 pm
the project file cannot be loaded…
April 9th, 2012 on 3:37 pm
The project requires Visual Studio 2010 with SP1 and IIS Express. Also make sure that you have the latest version of nuget installed. Let me know if you continue to have troubles.
June 14th, 2012 on 2:05 am
Hello,
I have visual studio 2010. BUt i have basic version of windows 7. i am not able to intall IIS.
How can i do that ?
June 14th, 2012 on 6:47 am
For this example, you do not need IIS. You can just run IIS Express locally to get it running. Obviously if you plan on hosting it for others, you’ll need to do so with Windows Server.
July 14th, 2012 on 6:04 am
Hi.
Thanks a lot for your useful widget.
I have a problem with it. Every time I load index (Home/index) page, after getting data from google, it shows statics but it goes away and comes again in page, some kind of flashing, then it disappears.
September 27th, 2012 on 2:04 pm
I’m getting a 404 error on the Auth method for the url https://www.google.com/analytics/feeds/. Has anything changed with the google api?
September 27th, 2012 on 2:09 pm
I’m not aware that anything changed. A site that I have that makes use of this technique is still functioning properly