2

我想访问谷歌分析数据,我从谷歌数据 API SDK 中获得了样本。但这些编码不起作用并引发异常

请求执行失败:https ://www.google.com/analytics/feeds/accounts/default

所以我发现这是因为谷歌将它更新到了 v3.0。我搜索了 C# 的更新编码,但我找不到解决方案。

我有同样的问题,但使用 C#。 使用 GData .NET Analytics API 时引发异常

我尝试按照 Google 开发人员中的说明进行如下更改编码 - 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;       
}

但即使我改变了它,它也会抛出异常

DataFeed 数据 = service.Query(query2);

这条线。例外情况如下:

请求执行失败: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 -日期=2011-08-01

我正在使用以下 DLL

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

我的问题:

  1. 我该如何纠正这个错误?

  2. 如何访问 Google 分析数据?它是否正确?否则有什么办法呢?例如,我想获取可用的 ProfileId 及其值。(标题和页面视图)

4

1 回答 1

8

分析帐户:

我假设你已经有一个分析帐户,如果你不创建一个,然后在这里注册你的域: http ://www.google.com/intl/en/analytics/

要获取您的 API 密钥,请执行以下操作:

按照https://developers.google.com/analytics/resources/articles/gdata-migration-guide上的说明(在 Google API 控制台中创建项目)生成您的密钥一旦您将其设置为查询字符串的一部分向 Google Analytics 服务请求,在这种情况下: YourAPIkEStringabcdefghijklmno

要获取 profileId(代码上的 ID),您应该这样做:

登录到您的分析帐户,在列表中选择所需的域(蓝色链接),单击管理员按钮,然后在配置文件选项卡上找到配置文件配置子选项卡,在这种情况下,您将找到配置文件 ID,其中包含八个字符长的 ID: 12345678

这里有一些 C# 代码可以帮助您获取该 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());
    }
}

由于 2.4 API 不再那么灵活,我在这里有另一篇文章 hacking it to get profile Id: Getting an specific ProfileId from registered Accounts using GData .NET Analytics API 2.4如果您需要将代码转换为 C#,您可以使用Telerik 转换器:http ://converter.telerik.com/

我认为这足以使用 2.4 API。如果您需要额外的帮助,请告诉我。

于 2012-09-24T22:35:48.923 回答