articles

Home / DeveloperSection / Articles / Data Reports example in Google Analytics API using ASP.NET MVC

Data Reports example in Google Analytics API using ASP.NET MVC

Anchal Kesharwani12296 08-Oct-2014

In this article, I’m creating an example in asp.net mvc that will help to understand data reports parameter values. 

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.

Let’s create the simple google example in asp.net mvc:

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

Step 2 Add this Code 

To understand the data to need to necessary fetch the data from google analytics so connect from the google analytics as following code:

This code store the output in the GaData object. Here no dimension values but so it simple show the dimension. The profile id is the unique Id that is necessary in the data feed request. And start date, end and metrics is also required field.

Now we need to implement the data reports parameter. 

Step 3 Add Data Report and Test 

The data report parameter add and how to work let’s check that:

Let’s add the metrics and dimension change the starting data and end date:

 

string profileId = "ga:1234567";

string startDate = "2014-09-11";

string endDate = "2014-09-17";

string metrics = "ga:visits";

// Add the dimension

request.Dimensions = "ga:day";

This show the visits according to day between starting date to end date from profile id of google analytics. Here this output as like this: 

If you need to sort the by descending according to visits then add this code and show that:

 request.Sort = "-ga:visits";

This sort property accept the dimension or metrics and sort property by default sort ascending order but when include the –(minus) sign with metrics or dimension then it sort in descending order. Let’s see the output:

Note: The data sorted by the ga:visits in descending order. All Output sorted according to visits. 

If I add the dimension value as country then it show this value as:

 

request.Dimensions  = "ga:day,ga:country";

These red lines indicate the four different country and these countries show visits according to data vise. But if I want to filter that means if need only India data then it need to filter. Let’s add the filter code and let’s see:

 

request.Filters  = "ga:country==India";

The result shown only country of India. The Filters property get only the dimension and metrics value and filter the result according to expression.

If I add the metrics and dimensions value according to need. Next I want to test the segment parameter. So It necessary to show the data for segment:

Add this code or replace this:

// Add the metrics  as users for show users details
string metrics = "ga:visits,ga:users";
// Add the dimension as browser that show the which browser used.
request.Dimensions = "ga:day,ga:country,ga:browser";

Let’s see the following according to query: 

In this data many users and many browser I’m only want user segment that open in the Chrome browser let’s see how to do it:

Let’s add this code:

request.Segment  = "users::condition::ga:browser==Chrome"; 

See this output for segment result: 

The segment get the type and then add the condition to filter the data.

Now add the code start index that select the beginning index in the above output get the 5 records but when set the start index then show this result:

request.StartIndex = 3;

Then it show the only three record because starting index is 3. Let’s see the output: 

You also the maximum show. For check it add property MaxResults in the program. It set to two that show only two records.

 request.MaxResults = 2;

Let see the result that contain only two record.

I hope this article is helpful for you. Thanks!


Updated 17-Jul-2019

Leave Comment

Comments

Liked By