在.NET/C#中组织和使用谷歌分析数据?
我正在从Google Analytics API中提取数据,我想提取每天的总访客数,并按日期将其组织在一个列表中 例如: 属性“Rows”包含我指定的时间范围内的所有日期 每行包含:在.NET/C#中组织和使用谷歌分析数据?,c#,asp.net-mvc,google-analytics,google-analytics-api,google-api-client,C#,Asp.net Mvc,Google Analytics,Google Analytics Api,Google Api Client,我正在从Google Analytics API中提取数据,我想提取每天的总访客数,并按日期将其组织在一个列表中 例如: 属性“Rows”包含我指定的时间范围内的所有日期 每行包含: Date - 2014-02-24 Visitors - 3000 newVisits - 2400 PageViews - 10000 PercentNewVisits - 38,001302 我需要组织和构造这些数据,以便在我的C#/.NET应用程序中正确显示它,但是如何呢 例如: 如果我选择开始和结束日期“
Date - 2014-02-24
Visitors - 3000
newVisits - 2400
PageViews - 10000
PercentNewVisits - 38,001302
我需要组织和构造这些数据,以便在我的C#/.NET应用程序中正确显示它,但是如何呢
例如:
如果我选择开始和结束日期“2014-01-24-2014-02-28”,我希望看到这两天之间的所有访问
- 2014-01-24=100次访问
- 2014-01-25=200次访问
- 。。。。。等等
GaData
类的属性“行”中。但是,它是一个字符串列表,属性如下所示:
public virtual IList<IList<string>> Rows { get; set; }
这是我得到的输出:
VisitorStats 2010-02-24 - 2014-02-24
------------------------------------------
NewVisits: 343272
NewVisits: 147693
PageViews: 255000
PersentNewVisits: 42.54700702044485%
我现在很接近了。我想在这里归档的是:
我的代码如下所示:
public virtual IList<IList<string>> Rows { get; set; }
控制器类
:
public class GAStatisticsController : Controller
{
// GET: /ShopStatistics/
public string GAnalyticsService()
{
//Users Google Service Account
string serviceAccountEmail = "xxx@developer.gserviceaccount.com";
//Public key for authorisation
X509Certificate2 certificate = new X509Certificate2(@"C:\Users\xxx\NopCommerce\Presentation\Nop.Web\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
//Account credentials of authorisation
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
// Create the service.
//Twistandtango
AnalyticsService GoogleAnalyticsService = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Twist",
});
var r = gas.Data.Ga.Get("ProfileID", "2014-01-24", "2014-02-28", "ga:visitors");
r.Dimensions = "ga:date";
r.Sort = "-ga:date";
r.MaxResults = 10000;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = request.Execute();
return "Visitor statistics" + " " +
d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "<br/>" +
"------------------------------------------" + "<br/>" +
"Total visitors:" + " " + d.TotalsForAllResults["ga:visitors"].ToString();
}
public ActionResult GAStatistics()
{
GAnalyticsService();
return View(new GAStatisticsListModel());
}
}
}
有什么想法吗
这篇文章是相关的,我已经设法创建了一个临时修复程序:
Thx数据已在列表中返回给您: 在列标题中循环:
foreach (GaData.ColumnHeadersData header in d.ColumnHeaders)
{
Console.Write(header.Name);
}
如果您希望能够对它们进行汇总,我建议您在此时注意nr列,如果是ColumnType=“METRIC”,则可以通过创建某种计数器。你不能把维度加起来
var r = gas.Data.Ga.Get("ga:7811042x", "2014-01-24", "2014-02-28", "ga:visitors");
r.Dimensions = "ga:date";
r.Sort = "-ga:date";
r.MaxResults = 10000;
循环通过每行:
foreach (var row in d.Rows)
{
foreach (var Column in row)
{
// here is where I would check the counter if its Colum nr x
// add it to some counter.
Console.Write(Column);
}
}
我还要补充一点,我认为这个问题超出了原来问题的范围。另外,悬赏问题听起来不像原来的问题,对我来说,听起来更像是你希望有人为你做工作 API不会为您总结它。你必须提取访问量,然后自己加起来。好的。可能吗?所以我每天都要问一个问题来吸引访客?我的意思是,如果我使用demention“r.Sort=“-ga:date”;“我不应该在我指定的日期之间的所有日子都有访客吗?调试器中不应该显示所有日期吗?必须能够从api中获得每天一定数量的访问者,否则它如何在Analytics Dashboard中使用折线图?您是否已经在这里首先测试了您的请求?如果您不需要ga:visitCount,只需删除维度,它将为您提供该时间段内的访问总数。有了这个维度,您将获得每个不同访问计数的访问者。更新:我写错了日期。我写了2010-2014年,当然数字错了,哈哈。我现在把我指定时间范围内的所有日期都记在“行”中。下一个问题是如何从数据中生成对象,或者至少将它们组织到列表中,等等..您好。当然,我不想让任何人为我做这项工作,这不是重点。关键是能够在列表或数组中分离数据。例如,从GaData.行中提取所有日期,并列出日期列表。我很希望能得到一些有用的建议。如果悬赏问题给人留下这样的印象,我很抱歉。如果你不知道如何用C#列出一个清单,我建议你发布一个关于这个问题的问题。它与GA api无关。我对编程相当陌生,但我知道如何制作一个列表。我只是无法从谷歌那里获取我不想要的价值,并将它们放在我自己的列表中。Annywas,弄明白了如何提取我想要的内容,这非常有用:foreach(d.Rows中的var row){Console.WriteLine(“日期:“+”+行[0]+“+”+”访问者:“+”+行[1]);Console.ReadLine();}这将两个值像预期的那样分开。谢谢
foreach (var row in d.Rows)
{
foreach (var Column in row)
{
// here is where I would check the counter if its Colum nr x
// add it to some counter.
Console.Write(Column);
}
}