Have you been trying to connect your website or application to the Google Analytics API? Would you like to be able to show your users Google Analytics data for your website? If you are you trying to work with the Google Analytics API in C# .net I might be able to help. In this tutorial series, we will be looking into how to connect to Google Analytics API using OAuth2, as well as a service account. I will show you how to get a list of the users Accounts to display to them from the Google Analytics Management API. Using the Meta-data API you will be able to get a full up to date list of the current available metrics and dimensions to display to your users. Finally, we will look at getting data back from Google Analytics by using either the Real-time API or the Core reporting API.
- 1 Google Analytics API – Seven part Tutorial Series
- 2 Project Setup
- 3 Authenticating Google Analytics API With C#
- 4 Types of Authentication
- 5 Google Analytics Service
- 6 Conclusion
Google Analytics API – Seven part Tutorial Series
- Google Analytics API Introduction
- Google Analytics API Authentication with C# OAuth2 vs Service Account
- Google Analytics Management API with C#Accounts, Web Properties and views(Profiles)
- Google Analytics Management API with C# – Advanced
- Google Analytics Management API with C# – Upload
- Google Analytics Meta-Data API with C# – Showing current dimensions and metrics
- Google Analytics Real-Time API with C# – Whats happening now!
- Google Analytics Core Reporting API with C# – Its all about the data baby!
Make sure you create your Microsoft Visual Studio project and have it set to .NET Framework .net 4.0 or .NET Framework 4.5.
Add the following NuGet Package
– .NET Framework 4 and 4.5
– Windows Store apps
– Windows Phone 8 and 8.1
– Portable Class Libraries
To install Google.Apis.Analytics.v3 Client Library, run the following command in the Package Manager Console
PM> Install-Package Google.Apis.Analytics.v3
Microsoft Visual Studio project will probably need most of these using’s
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Google.Apis.Analytics.v3; using Google.Apis.Auth.OAuth2; using System.Threading; using Google.Apis.Util.Store; using Google.Apis.Services; using System.Security.Cryptography.X509Certificates; using System.IO;
Authenticating Google Analytics API With C#
Authentication is the key to everything here. If you want to be able to access data on Google Analytics from Microsoft Visual Studio using C# in most cases you will need to be authenticated.
The two types of Authentication you can use are:
- Open Authentication or OAuth2 – Which is designed to allow you to access other peoples data. For Example: A user installs your application that will allow them to view their Google Analtyics Data, before your application can view their data the user will be asked to authenticate your application giving your application permission to access their data.
- Service Account Authentication – Which is designed to allow you to access your own data. For example: You would like to display a count of the number of page views a page on your website has had. For this there is no reason to ask permission because you already own this data.
If you are unsure which type of authentication you need to use you can check out my authentication page which gives more detailed explanation into the different types. I have created a separate tutorial that explains how to create the different credentials. Decide which type of authentication you need and please read though that tutorial and come back to continue this one.
Types of Authentication
There are two types of Authentication OAuth2 which will allow you to access another users data, and a service account which can be set up to access your own data. The code for them is slightly different.
If you want to be able to access Google Analytics though the API owned by someone else you will need to be authenticated. The code for authentication simply displays a webpage for your user asking them to give you permission to access there data. If the user gives you permission a file is then stored on the machine in the %AppData% directory containing all the information you will need in the future to access there data. For my testing purpose I use Environment. UserName as the name of the user, but if you are doing this web based you could use it as a session var of some kind.
That’s just fine but how does Google know what kind of permission you want to ask the user for? That’s where scopes come in, with scopes you define what permissions you need.
The code below will basically ask for everything, you should only include the scopes you NEED.
Assuming the user clicks Accept and gives you permission you will then have a valid user credential you can build upon. The main problem you may have with this is that it is useing FileDataStore which stores the data in the users %AppData% directory while this is probably fine for an installed application this is probably not the optimal solution for a web application. If you want to store the data in the database you need to create your own implementation of IDataStore. You are in luck I have a working version of that on GitHub.
Service Account Authentication
If you are only accessing your own Google Analytics Account you own this data already, so there is no reason to ask a user for permission to access it. In this instance you should use a Service account. A service account is just like any other user who has been granted access to your Google Analytics data with one exception, its automatic. There will be no prompt for access.
Service Account Setup
Take the service account email address from the Google Developer console, we need to give this email address access to our Google Analytics Data. We do that by adding it as we would any other user in the Admin Section of the Google Analytics Website. Add the service account email address as a user at the account level it must be the account level. This will enable your service account access to the Google Analytics account in question. Did I mention it must be at the ACCOUNT Level?
Service Account Authentication
Now the keyfile that you downloaded from the Google Developer console contains all the information needed by your code to access google. We use the key file to set up our authentication.
Google Analytics Service
Now that you are authenticated its only a matter of passing your credentials to create a new AnalyticsService. The service is where all of the magic is. It is though the service that all of the calls will be made to the Google Analytics API.
Notice how we pass our credential to it.
Public API key
To my knowledge there is only one Public API associated with the Google Analytics APIs that is the MetaData API. The data in the MetaData API is public its free for anyone to access, because of that we don’t need to use OAuth2 or a service account you can create a public API key and create the analyticservice using that
You should now understand how to access the Google Analytics API with an authenticated user. Join me for the next tutorial where we will look at how to get a list of accounts – web properties and profiles for an authenticated Analytics Service.
If you had any problems with this tutorial you can check the a sample project for working with Google Analytics API on GitHub as part of the Google-Dotnet-Samples project. With in that project you will find a helper class for Authentication with Google Analytics. DaimtoAnalyticsAuthenticationHelper.cs