To continue our learning towards Google Analytics API with.NET, in this post we will learn how we can pull our web site speed statistics from Google Analytics dashboard. Google tracking code that we setup in our website or webpage tracks the Page load time, Domain lookup time, Server response time, Server connection time etc. When we login into Google Analytics account and see the dashboard
In .NET we have following windows interface. We have two text boxes to supply Google credentials. Show All Profiles button contains the code logic to pull all the profiles that we have setup with our Google Analytics account. We have another two text boxes to enter date range for which we want to pull data. Show button contains the code logic to connect with Google Analytics feed and pull the web site speed statistics.
The .NET code logic on Show All Profiles button is following:
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}))); } }
The code logic on Show button is following. We are supplying ga:pageLoadTime into Metrics as we want to pull page load time statistics.
private void button2_Click(object sender, EventArgs e) { double SpeedTimeinSec; listView2.Columns.Add("PageSpeedType"); listView2.Columns.Add("Speed in Seconds"); listView2.FullRowSelect = true; AnalyticsService GAService = new AnalyticsService("VikTest-GA-App"); //Pass Google account credentials GAService.setUserCredentials(textBox1.Text, textBox2.Text); //PageLoadTime DataQuery query = new DataQuery("https://www.google.com/analytics/feeds/data"); query.Ids = listView1.SelectedItems[0].SubItems[1].Text; query.Metrics = "ga:pageLoadTime"; 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) { SpeedTimeinSec=Convert.ToInt32(entry.Metrics[0].Value) * 0.0010; listView2.Items.Add(new ListViewItem(new string[] {"PageLoadTime",SpeedTimeinSec.ToString()})); } }
After setting up the code logic, we need to connect to internet. We need to supply our identity to Google and click on Show All Profiles. Next we need to supply data range and click on Show button. The code logic works as expected and it pull down the Page Load time for this website (www.singhvikash.blogspot.com).
You can supply following Metrics values if you want to pull information such as Domain Lookup time, Server response time etc.
ga:domainLookupTime
ga:pageDownloadTime
ga:redirectionTime
ga:serverConnectionTime
ga:serverResponseTime
For a complete list of Dimension and metrics values for website speed you can visit following Google link. https://developers.google.com/analytics/devguides/reporting/core/dimsmets/sitespeed
- 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?