This article is in continuation towards our learning Google Analytics API with .NET. In our last articles we learn how to pull browser statistics and visitor type statistics report from Google Analytics dashboard using Google Analytics API.
In this post we will learn to pull search engine statistics report for your web site. Does your web site features in the search result list of search engines? Do the visitors actually go to your web sites when they searching using various Search engines such as Google, Bing, Yahoo etc.? Google Analytics Traffic Source Dimension shows you this repot when you click on Search and Overview.
You can pull this information into .NET using Google Analytics API. We will use the following .NET interface. In ListView2 we will display how many visitors you received from Search engines.
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 Search engine statistics we have to supply ga:source as Dimension and ga:organicsSearches 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 SearchEngineName; listView2.Columns.Add("SearchEngine"); listView2.Columns.Add("NoOfSearches"); 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:organicSearches"; query.Dimensions = "ga:source"; query.Sort = "ga:organicSearches,ga:source"; 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) { SearchEngineName = entry.Title.Text.ToString().Substring(10, (entry.Title.Text.Length - 10)); if (entry.Metrics[0].Value !="0") { listView2.Items.Add(new ListViewItem(new string[] { SearchEngineName, 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 search engine statistics.
- How to get started with Google Analytics API
- How to pull profile information from Google analytics?
- How to pull Browser statistics from Google Analytics Dashboard?
- How to pull Visitor Type statistics from Google Analytics Dashboard?
- How to pull Search Engines statistics report for your web site from Google Analytics Dashboard?
- How to pull your web site speed statistics from Google Analytics?