How to use Google Analytics API Client Library in C# application

How tu use Google Analytics

The creators and owners of websites are aware of the importance of statistics on the behavior of people using their services. There are tools that allow monitoring of web applications. They allow for collecting information about visitors to the site in real-time, for example where they are from or what kind of web browser they are using. One of such tools is mentioned in the title Google Analytics. I was so interested in this topic that I wrote my engineering thesis and created a project that used the aforementioned tool.

Some introductory thoughts to Google Analytics

Google Analytics is the most popular tool for tracking user behavior on websites, created of course by Google. In order to start using this tool, you must register your website with it. How to do it? It’s really easy! In the beginning, you have to register in Analytics. Google has provided instruction on how to get through the registration process, which I recommend. After successful registration, you can proceed to register your website.
The tool will generate a tracking code for you, which should be placed in the views of your website. From now on, you can view statistics and traffic from your website on Google Analytics, which are displayed on a very nice and user-friendly GUI. Let’s assume, however, that this interface offered by Google Analytics is not enough for you or you just want to collect and display this data in your application. No problem, just use Google Analytics API.

Google Analytics API Client Library

This is the Application Programming Interface that provides advanced access to data in your application. The API is available in many programming languages, but this article will show an example in C#. The latest version is v4, which gives you possibilities to build your own dashboards to display data in the application or integrate Google Analytics with your other application, which is crucial in this case. It’s time to finish the stories and go to the code.

[mailpoet_form id=”3″]

Communication of our application with Google Analytics

The implementation should start with the code that will allow our application to connect with Google Analytics. I created GACommunication.cs class for this purpose. At the beginning of the class, you can see the Google Analytics service declaration, which is necessary for implementation.
The next step is to create a constructor with two arguments. The first is the path to the key file, which must be downloaded from Google Analytics in advance. It is unique for each service created to track a certain website. The second argument is the account email address, which is also created when registering the service in Google Analytics. I will write about going through all these steps of creating and registering a service in Google Analytics in the next article in a week.
Inside the constructor, credentials are created that are necessary for our application to access data from Google Analytics. We can say that this is just an authorization. The next stage is the initialization of our website object, which is visible at the bottom of the listing.

Creating a request to Google Analytics

Once we have made a connection to Google Analytics, we can now move on to writing the method responsible for sending requests to get specific data. The arguments, in this case, are, in turn, the Id of service, which you will find in the information about it on Google Analytics and the range of dates from which the data are to come.
The method itself, as you can see in the attached listings, uses the method included in the service provided by Google Analytics. Our task is limited to providing a range of dates, as well as metrics and dimensions. In this case, it will be the number of people visiting the website depending on the country.

Show me some results!

Ok, ok, keep calm! The listing shows a real example of sending a request to Google Analytics and displaying it in the console. As you can see, Google Analytics API is a very nice tool to download statistics from our website to another application. If something is not clear to you and you have some doubts. Let me know! Anyway, I hope the article has been interesting and useful for you.


If you haven’t been in contact with Google Analytics before and you don’t quite know what I’ve written about here, I recommend you take a look at this tool. I think that if you have your website, it is extremely useful to use it. However, in this article, I wanted to focus more on its API.
I have shown the majority of the implementation part here to illustrate to you that it’s easy and enjoyable. The next article will focus on the configuration of the service and the whole Google Analytics settings.

Leave a Reply

Your email address will not be published.

Scroll to top