Using The Google Analytics API With Asp.Net MVC

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.

14 thoughts on “Using The Google Analytics API With Asp.Net MVC

    1. 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.

    1. 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.

  1. 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.

  2. vry nice article!

    The Google Analytics is the feature that provide the facility to get the statics of the website. In the google analytics statics get by the help of reports. The report show in the account show very easily but when show the data in the program then it creates problem. So, let’s know about data in google analytics. Reports are generally organize into these categories: users, traffic sources, content, internal site search, goals, and ecommerce.

    Generally, the data report is based on the dimensions and metrics. Analytics reports use a combination of metrics and dimensions to describe key types of user activity to your website, such as which search engine users used to reach your site in the Search Engines report, or which pages on your site received the most traffic in the Top Content report. Similarly, the Core Reporting API groups both dimensions and metrics into several categories of report data. By choosing your own combinations of dimensions and metrics, you can create a customized report tailored to your specifications.

    Follow Basic steps:
    There are following basic steps to need to follow before work the google analytics data in the asp.net mvc or c# program:

    First of set up tracking code and service if you not do:

    1. Set up the tracking code in the web.
    2. First Create service account in the Google Developers Console.
    3. Add permission for this account in the Google analytics.
    4. Use this account.

    Second install the necessary api’s that connect the google analytics.

    It may help you understand this using sample :
    Data Reports example in Google Analytics API using ASP.NET MVC

    Google Analytics API integration with ASP.Net

    Integrate an ASP.NET website with Google Analytics

  3. I was so happy to find this example, only to see that google has deprecated your code. Any thoughts on updating the example with Google’s new libraries so it works?

    1. I doubt I’ll get around to updating it anytime soon. I’ve moved on from asp.net. That said, I will accept a pull request :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>