Google Analytics API in C# -Execution of request failed: https://www.google.com/analytics/feeds/accounts/default
Asked Answered
V

1

2

i want to access Google analytic data and i got samples from Google data API SDK. but these coding does not working and throws exception

Execution of request failed: https://www.google.com/analytics/feeds/accounts/default

so i found the reason for this is Google updated it's to v3.0. i searched updated coding for the C#, but i couldn't find solution for this.

i have same problem as this, but with C#. Exception thrown when using GData .NET Analytics API

i tried coding with doing changes as follows as it says in 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 exception in

DataFeed data = service.Query(query2);

this line. exception is as follows:

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

i'm using following DLL

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

My Questions :

  1. how can i correct this error?

  2. how can i access Google analytic data? is this correct? or else what is the way to doing it?? for a example i want to get available ProfileId and their values. (Title and Page views)

Vanessa answered 21/9, 2012 at 9:14 Comment(0)
H
8

Analytics Account:

I am assuming you have an analytics account already if you don't then create one, and sign up your domain here: http://www.google.com/intl/en/analytics/

To get your API Key do this:

Follow the instructions on https://developers.google.com/analytics/resources/articles/gdata-migration-guide (Create a Project in the Google APIs Console) to generate your key Once you have it set it as part of the querystring to request to Google Analytics service, in this case: YourAPIkEStringabcdefghijklmno

To get the profileId (Ids on the code) you should do this:

Log into your analytics account, select the desired domain on your list (blue link) click on the administrator button and on the profiles tab find the profile configuration subtab, right there you will find the profile id in this case the eight characters long id: 12345678

Here you have some C# code to help you getting the number of visits for that Id:

public string VisitsNumber() 
    {
        string visits = string.Empty;
        string username = "[email protected]";
        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 the 2.4 API is not so flexible anymore, I have another post here hacking it to get the profile Id: Getting an specific ProfileId from registered Accounts using GData .NET Analytics API 2.4 if you need to convert the code to C# you can use the Telerik converter: http://converter.telerik.com/

I think this suffice to use the 2.4 API. If you need extra help let me know.

Haplo answered 24/9, 2012 at 22:35 Comment(14)
do u mean for the gkey, we need our APIkEY into 4 times. is this correct?Vanessa
do we need to create oAuth 2.0 client ID and api key is getting from google api's Simple API Access value, am i right??? but still give me same exception.Vanessa
Keep the OAuth off while working with the 2.4 API. 1) Log in api console: code.google.com/apis/console Sign in, and go to the services tab, there Enable the analytics API toggling to ON 2) Go to API Access tab and copy the value thats show on the API Key field, use it on the code. 3) Follow the instructions on the answer above to the get the profile ID value for which you want to get the visits from and use it also on the code. If you don't get it to work you can email me your login email, password, profile id, and API key values to test it here, my code is working here.Haplo
And no, you dont need to repeat your API key 4 times, just put it once, I wanted to point out the number of characters of it repeating it, thats it.Haplo
this is my API key. AIzaSyD_ds6fLlHM1Sskg0KdS5Sow6AnG8Qcf-8. do i need to put -8 also?Vanessa
as an inner exception i got The remote server returned an error: (403) Forbidden.Vanessa
Email me the info I asked you above to test the code here. My mail is on my SOF profile.Haplo
I am stuck with the same error The remote server returned an error: (403) Forbidden. How did you get around to this?Piano
also what happens to code written with the older dll Google.GData.Analytics.dll ? will that stop working ?Piano
Hi user20358, I think the only way to get around this is using the NEW OAuth 2.0 Gdata .Net library, since the old feed has been shutdown. I haven't tried the code on this post in a while. Here are a couple of references for you: #12105212 code.google.com/p/google-gdata/source/browse/trunk/clients/cs/… Let me know how it went. Hope it helps.Haplo
There is this class called OAuth2Parameters in the code sample on the second link. Not able to find that anywhere..Piano
Check: #10156697Haplo
Hi @CoderRoller, Great answer.. Helped me out immensely! I can now fetch the number of visits for a month by setting up dates, but can I get number of visit for each day within this month's period?Cambyses
I've obtained the page visit for each day by looping through the dates and adding each day's count in an array, but in this case a request is sent everytime the date increases.. :/ which is not an optimal solution. Is there any property of dll that could meet my requirement in optimal way?Cambyses

© 2022 - 2024 — McMap. All rights reserved.