Dimension and Metrics in Google Analytics API using ASP.NET MVC


In this article, I’m explaining the concept of dimension and metrics in google analytics api using asp.net mvc and get the data.


Dimension and Metrics are the building block of the google analytics. In the google work by dimension and metrics but what is difference and what is meaning of dimension and metrics. Let’s know what is dimension and metrics.

Dimension (Describe Data)


In the google analytics, dimension are the labels in the rows of your reports. Think of a dimension as describing the “what,” as in “what keyword did they use” or “what city is the visitor from” or “what pages were viewed.” The Dimension is actually a descriptive data that always in the words. It also create the category that means if you see the as metrics visits when you add the dimension as country, or anything then it will categorized and show country vise visits.

The given below figure to understand the dimensions like this in the google analytics:


Metrics (Measured Data)


In the google analytics, metrics are elements about a dimension that can be measured. Think of a metric as answering “how many” or “how long,” as in “how many visits” or “how long a visitor was on the site.” The metrics contain the numbers or percentage as ‘number of visits”, “number of users”, or “session duration”, etc. Metrics are individual element that contain the elements of dimension.

The given below figure to understand the metric as like this in the google analytics:


Relation between Dimension and Metrics in Google Analytics


In the google analytics, dimension and metrics can work standalone. The metric are the required field and the dimension are optional. It usually used the in the google analytics or any program with together. The relationship between the dimension and metrics create the category of data that means if your metric ‘no of visits’ and ‘bounce rate’ it show the total bounce rate and number of visits but if add the dimension as country then create group as like this:


No. of Visits

Bounce rate










United States




In the google analytics, reports are based on the combination of dimensions and metrics. Some dimensions and metrics appear by default when you open a report. And when you want select according own get result then select the dimension and metrics according to need and show the result desired.


Here, I’m creating an example of metrics and dimension in the program that will help to understand developers.

Let’s create an ASP.NET MVC or c# Example:

Here, I’m create a simple example to understand the dimensions and metrics how work.


Step 1 Basic Steps to Connect


There are following steps to connect the google analytics:

1.       First of all set up the tracking if you are not set up the tracking code. If you want to show how set up the tracking code see this article: Set up tracking code in Website.

2.       After that create service account in Google Developers Console and add permission in google analytics if your account already exist then it does need otherwise you will show the my previous article Google analytics in asp.net mvc.


Step 2 Add this code


Before code add your library of google analytics.

Add the Code to connect and understand the metrics:


string scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();

            string ServiceAccountUser = "nnnnnnnnnnnnnnn-nnnnnnnnnnnnnnnnnnn.com";

            string keyFile = @"D:\nnnnnnnnnnnnnnnnnnnnnnnn.p12";

            string keyPassword = "notasecret";

            AssertionFlowClient client = new AssertionFlowClient(

                GoogleAuthenticationServer.Description, new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable))


                Scope = scope,

                ServiceAccountId = ServiceAccountUser


            var authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

            var service = new AnalyticsService(new BaseClientService.Initializer()


                Authenticator = authenticator


            string profileId = "ga:nnnnnnnn";

            string startDate = "2014-01-10";

            string endDate = "2014-10-30";

            string metrics = "ga:visits,ga:pageViews";

            DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);

            request.Dimensions = "ga:country";

            GaData data = request.Execute();

In this code segment the highlighted lines metrics and dimension. It give the output as like this:

It returns country name visits and the page view. If the dimension remove from the program watch the output as like this:

Show this figure it only return the total visits and total page views.

If add more dimension as like add city, operating system, and browser, etc. then replace the dimension with this code:

 request.Dimensions = "ga:country,ga:city,ga:operatingSystem,ga:browser";

And look the output:


It show it create category that metrics individual show data of dimension. When metrics are alone not with the dimension then it will show only visits and page views that only one row which total number of the visits and page views. When one dimension then it number countries and show its visits and page views. Here number four country then four records that means if many dimension and available all dimension number of values then it create rows according its.


I hope that this article is helpful for you. Thanks!


Leave Comment