Thursday, August 23, 2012

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

This article is next in series of our learning Google Analytics API with .NET. In our last articles we learn how to pull browser statistics report from Google Analytics dashboard using Google Analytics API.
Today we will learn to pull Visitor Type statistics reports from Google Analytics dashboard. Google Analytics dashboard reports show you new and returning visitors to your website. When you click on Behavior->New vs Returning, Google Analytics dashboard will show how people are behaving to see your web page or web site. Are you getting the new users or returning users to your web asset.
To pull this information into .NET using Google Analytics API, we will use the following .NET interface. In ListView2 we will display new and returning visitor’s statistics.
We have following code logic on Show All Profiles button, which pull all the profiles information from Google Analytics account.
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 Show button. To pull visitor type statistics we have to supply ga:VisitorType as Dimension and ga:Visitors as Metrics. Google Analytics understands the dimension and metrics. By specifying the dimension and metrics you are telling to Google Analytics what part of data you want to retrieve from Google Analytics dashboard.
private void button2_Click(object sender, EventArgs e)
{
  string VisitorTypeName;
  listView2.Columns.Add("VisitorType");
  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:visitors";
  query.Dimensions = "ga:visitorType";
  
  
  query.Sort = "ga:visitorType,ga:visitors";
  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)
  {
   VisitorTypeName = entry.Title.Text.ToString().Substring(4, (entry.Title.Text.Length - 4));
   listView2.Items.Add(new ListViewItem(new string[] { VisitorTypeName, entry.Metrics[0].Value }));


  }
}
After setting up the code logic and interface we need to connect to internet and has to supply our google credentials. We have to enter data range for which we want to retrive
data and click on Show. The results were as expected. The code logic pulls the data from Google Analytics report about visitor type statistics.


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