我想访问Google分析数据,我从Google数据API SDK获取样本。但是这些编码不起作用并抛出异常
执行请求失败:https://www.google.com/analytics/feeds/accounts/default
所以我发现原因是Google将其更新为v3.0。我搜索了C#的更新编码,但我找不到解决方案。
我有同样的问题,但是用C#。 Exception thrown when using 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 data = service.Query(query2);
这一行。例外情况如下:
我正在使用以下DLL
Google.GData.Analytics.dll
Google.GData.Client.dll
Google.GData.Extensions.dll
我的问题:
如何更正此错误?
如何访问Google分析数据?它是否正确?或者这样做的方法是什么? 举个例子,我想获得ProfileId及其值。 (标题和页面浏览量)
答案 0 :(得分:8)
Google Analytics帐户:
我假设您已经拥有一个分析帐户,如果您不创建分析帐户,并在此处注册您的域名: 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不再那么灵活了,我在这里发布了另一篇文章来攻击它以获取配置文件ID: Getting an specific ProfileId from registered Accounts using GData .NET Analytics API 2.4如果您需要将代码转换为C#,可以使用Telerik转换器:http://converter.telerik.com/
我认为这足以使用2.4 API。如果您需要额外的帮助,请告诉我。