Google Analytics API in C # - Request Execution Error: https://www.google.com/analytics/feeds/accounts/default

I want to access Google analytic data, and I got samples from the Google API data SDK. but this encoding does not work and throws an exception

Request Failed: https://www.google.com/analytics/feeds/accounts/default

so I found the reason for this - Google updated it to version 3.0. I was looking for updated coding for C #, but I could not find a solution for this.

I have the same problem as this one, but with C #. Exception thrown while using GData.NET Analytics APIs

I tried the coding with the changes as follows: it says in a Google developer - https://developers.google.com/analytics/resources/articles/gdata-migration-guide#appendix_a

string userName = this.Username.Text; string passWord = this.Password.Text; AnalyticsService service = new AnalyticsService("AnalyticsSampleApp"); service.setUserCredentials(userName, passWord); string googleAccountWebId = "AIXXXXXXXXXXXXXXXXXXXXXXXXXXX"; string profileFeedUrl = "https://www.googleapis.com/analytics/v2.4/data?key=" + googleAccountWebId; DataQuery query2 = new DataQuery(profileFeedUrl); query2.Ids = "12345678"; query2.Metrics = "ga:visits"; query2.Sort = "ga:visits"; query2.GAStartDate = DateTime.Now.AddMonths(-1).AddDays(-2).ToString("2011-08-01"); query2.GAEndDate = DateTime.Now.ToString("2013-09-01"); query2.StartIndex = 1; DataFeed data = service.Query(query2); foreach (DataEntry entry in data.Entries) { string st=entry.Metrics[0].Value; } 

but even I change this, it throws an exception in

Data DataFeed = service.Query (query2);

this line. The exception is as follows:

The request failed: https://www.googleapis.com/analytics/v2.4/data?key=AIXXXXXXXXXXXXXXXXXXXXXXX-8&start-index=1&end-date=2013-09-01&ids=12345678&metrics=ga:visits&sort=ga :: -date = 2011-08-01

I am using the following dll

 Google.GData.Analytics.dll Google.GData.Client.dll Google.GData.Extensions.dll 

My questions:

  • How can I fix this error?

  • How can I access Google analytics data? It's right? or how to do it? For an example I want to receive ProfileId and their values. (Types of headings and pages)

+2
c # google-analytics gdata gdata-api
Sep 21 '12 at 9:14
source share
1 answer

Google Analytics Account:

I assume that you already have an analytic account, if you do not create one, and register your domain here: http://www.google.com/intl/en/analytics/

To get your API key, follow these steps:

Follow the instructions https://developers.google.com/analytics/resources/articles/gdata-migration-guide (create a project in the Google API console) to generate your key. When you install it as part of a request that requests Google Analytics, in this case: YourAPIkEStringabcdefghijklmno

To get the profileId (code ids), you must do the following:

Log in to your analytics account, select the domain you need in the list (blue link), click the "Admin" button and on the "Profile" tab find the config subtab profile, there you will find the profile identifier in this case eight characters long: 12345678

Here you have the C # code that will help you get the number of visits for this Id:

 public string VisitsNumber() { string visits = string.Empty; string username = "youremailuser@domain.com"; string pass = "yourpassword"; string gkey = "?key=YourAPIkEYYourAPIkEYYourAPIkEYYourAPIkE"; string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey; string accountFeedUrl = "https://www.googleapis.com/analytics/v2.4/management/accounts" + gkey; AnalyticsService service = new AnalyticsService("WebApp"); service.setUserCredentials(username, pass); DataQuery query1 = new DataQuery(dataFeedUrl); query1.Ids = "ga:12345678"; query1.Metrics = "ga:visits"; query1.Sort = "ga:visits"; //You were setting 2013-09-01 and thats an invalid date because it hasn't been reached yet, be sure you set valid dates //For start date is better to place an aprox date when you registered the domain on Google Analytics for example January 2nd 2012, for an end date the actual date is enough, no need to go further query1.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd"); query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd"); query1.StartIndex = 1; DataFeed dataFeedVisits = service.Query(query1); foreach (DataEntry entry in dataFeedVisits.Entries) { string st = entry.Title.Text; string ss = entry.Metrics[0].Value; visits = ss; } return visits; } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Response.Write("Visits:" + this.VisitsNumber()); } } 

Since API 2.4 is not so flexible anymore, I have another entry that hacks it to get the profile ID: Getting a specific profile from registered accounts using GData.NET Analytics API 2.4 , if you need to convert the code to C #, you can use Telerik converter: http://converter.telerik.com/

I think this is enough to use API 2.4. If you need more help let me know.

+8
Sep 24
source



All Articles