Monday, August 20, 2012

Google Analytics API with .NET
How to pull profile information from Google analytics?

This post is in continuation of our learning Google Analytics API. In the last post we learn what is Google Analytics and we setup our profile with Google Analytics. We also learn how to setup Google analytics tracking code in our web site.

Once you put the Google analytics tracking code in your web page, Google Analytics start tracking your page statistics. Every time you login to Google Analytics to see your web statistics, you are greeted with the the following screen. You can select the profile for which you want to see web statisics. With your Google Analytics account, you can setup more than one profile.You can track statistics for one web page or complete web site or your multiple web sites by creating differet profiles. So you need to pick which profile you want to see from the below screen.

Once you click the profile name, you are taken to Standard Reporting dashboard. You can see the visitor’s overview to the right side of the panel.

From the left side of panel you can select the section for which you want to see the reports. If you want to see what browser people are using to see your web page, you can select Browser and OS from the left panel and Google Analytics will show you statistics about that. Is not that great you can design your web page to best suited browser that people are using to see your web content.

The sections on the left side are called Dimension. Dimensions are segments or sections of report against which you want to see the count. Metrics are the count that you see on the report. So in case you selected Browser and OS and you see the counts against it, you just interact with one Dimension and Metrics of Google Analytics report.

As you play around with Google Analytics Dashboard you will be more familiarized with the reports.

Now coming back to our objective to learn Google Analytics API, as discussed in the last post if you have installed the .NET client library, you can pull the statistics that is shown on the Google Analytics Dashboard to your .NET program.

Google Data API provides a namespace Google.GData.Analytics that you can use to read your web statistics. Let us try to pull all profile information from Google Analytics using our .NET program.

To start with I have design following .NET form. It has two label and two text boxes where you will supply your Google account credentials. There is a button on the form Show All Profiles. When you click on the button, the code logic written on the form will interact with Google Analytics feed and return the profile information.

I am using following namespaces that come with the Google Data API that I installed.

using Google.GData.Analytics;
using Google.GData.Extensions;

I have following code logic on the button control. We will be pulling Profile name and profile id from the Google Analytics feed. The coding logics is self-explanatory. We are creating an instance of Analyticsservice and have given our instance a name. We are creating an AccountQuery because we want to query account information. We are passing the account query to Account feed which interacts with Google Analytics and pull the feed entries. Each feed entry represents a profile.

private void button1_Click(object sender, EventArgs e)
{
 //Add LisView Column headings

 listView1.Columns.Add("ProfileName");
 listView1.Columns.Add("ProfileID");
 listView1.FullRowSelect = true;

 AnalyticsService GAService = new AnalyticsService("VikTest-GA-App");
 AccountQuery GAQuery = new AccountQuery();

 //Pass Google account credentials
 GAService.setUserCredentials(textBox1.Text, textBox2.Text);

 //Create Google Account Feed
 AccountFeed GAFeed = GAService.Query(GAQuery);
 foreach (AccountEntry entry in GAFeed.Entries)
 {
  
  listView1.Items.Add((new ListViewItem(new string[] { entry.Title.Text, entry.ProfileId.Value})));
 }

}

Once you are done with design part, you need to check you are connected with the internet. Since you are interacting with an online source, you need to be on internet to run your .NET Form. I run the form and supplied my Google credentials and clicked on Show All Profiles button. The code logic work as expected and pulled the profile information and show it on the listview.



Please note this article was created using Google Analytics API version 2.3. As per Google Analytics Notification this feed has been shutdown.


Popular Posts

Real Time Web Analytics