Monday, August 20, 2012

Google Analytics API with .NET
How to pull Browser statistics from Google Analytics Dashboard?

This article is next in series of our learning Google Analytics API with .NET. In our last article we learn about Dimension and Metrics and we pull Google Analytics profiles using Google Analytics API.

Today we will learn to pull Browser statistics reports from Google Analytics dashboard. When you log in to your Google Analytics accounts, on the left panel you see a lot of dimension. When you click on Technology->Browser & OS, Google Analytics dashboard will show what browser people are using to see your web page or web site.

To pull this data into .NET, we need to understand that Google Analytics Dashboard is all about Dimension and Metrics. Dimension in this case is Browser & OS which is represented as ga:browser and Metrics is represented as ga:pageviews when we are using Google Analytics API. To pull data from Dashboard Google Analytics provides DataQuery and DataFeed. In DataQuery we mention the Dimension and Metrics.

Let us see the example for this into .NET. We have following .NET interface.

We have two ListView control on the form to show profiles and Browser Statistics. We have two text boxes to supply Google Credentials. We have next two text boxes to supply data range for which we want to pull data from Google Analytics dashboard.

We have following code logic on button Show Allow Profiles to see pull all profiles information from Google Analytics.

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})));
 }

}

We have following code logic on button Show to pull Browser statistics information from Google Analytics dashboard.

private void button2_Click(object sender, EventArgs e)
{
 string browserName;
 listView2.Columns.Add("BrowserName");
 listView2.Columns.Add("NoOfVist");
 listView2.FullRowSelect = true;

 AnalyticsService GAService = new AnalyticsService("VikTest-GA-App");
 //Pass Google account credentials
 GAService.setUserCredentials(textBox1.Text, textBox2.Text);

 DataQuery query = new DataQuery("https://www.google.com/analytics/feeds/data");
 query.Ids = listView1.SelectedItems[0].SubItems[1].Text;
 query.Metrics = "ga:pageviews";
 query.Dimensions = "ga:browser";
 query.Sort = "ga:browser,ga:pageviews";
 query.GAStartDate = Convert.ToDateTime(textBox3.Text).ToString("yyyy-MM-dd"); 
 query.GAEndDate = Convert.ToDateTime(textBox4.Text).ToString("yyyy-MM-dd"); 

 DataFeed dataFeed = GAService.Query(query);
 foreach (DataEntry entry in dataFeed.Entries)
 {
 browserName = entry.Title.Text.ToString().Substring(11, (entry.Title.Text.Length - 11));
 listView2.Items.Add(new ListViewItem(new string[] { browserName, entry.Metrics[0].Value }));


 }
}

After designing you .NET interface and code logic, I connected to internet and run the form. I supplied my Google account credentials and click on Show All Profiles. The results were as expected.

I entered a start data and end date as I want to see data from 1st Aug 2012 to 20th Aug 2012 and clicked on button Show. The browser statistics was shown in ListView2.

So this is how we can pull the browser statistics reports from Google Analytics dashboard.

Thanks for reading this post.



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