【发布时间】:2014-03-07 08:03:56
【问题描述】:
我正在从 Google Analytics API 中提取数据,我想提取每天的访问者总数,并将它们组织在按日期排序的列表中。
例如:
属性“行”包含我指定的时间范围内的所有日期。
每一行包含:
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; }
这是带有Rows 属性的GaData 类:
这是我在调试时得到的,日期显示“。检查一下:
每一行都包含查询 StartDate 和 EndDate 中每一天的访问者数据!
这是包含度量、维度等的嵌套类:
这是我当前的查询:
var r = gas.Data.Ga.Get("ProfileID", "2014-01-24", "2014-02-28", "ga:pageviews,ga:newVisits,ga:visitors,ga:percentNewVisits");
r.Dimensions = "ga:date";
r.Sort = "-ga:date";
r.MaxResults = 10000;
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
Console.WriteLine("VisitorStats" + " " +
d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "\r\n" +
"------------------------------------------" + "\r\n" +
"Visitors:" + " " + d.TotalsForAllResults["ga:visitors"] + "\r\n" +
"NewVisits:" + " " + d.TotalsForAllResults["ga:newVisits"] + "\r\n" +
"PageViews:" + " " + d.TotalsForAllResults["ga:pageviews"] + "\r\n" +
"PercentNewVisits:" + " " + d.TotalsForAllResults["ga:percentNewVisits"] +"%");
这是我得到的输出:
VisitorStats 2010-02-24 - 2014-02-24
------------------------------------------
NewVisits: 343272
NewVisits: 147693
PageViews: 255000
PersentNewVisits: 42.54700702044485%
我现在已经很接近了。我要在这里存档的是:
我的代码如下所示:
ControllerClass:
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());
}
}
}
ListModel 所以客户可以选择要查询的内容。 StartDate、EndDate、Visitors、Sales、ConversionRate 等(这个模型没有做任何事情):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Nop.Web.Framework;
using Nop.Web.Framework.Mvc;
namespace Nop.Admin.Models.GAStatistics
{
public class GAStatisticsListModel : BaseNopModel
{
public GAStatisticsListModel()
{
AvailableGAStatistics = new List<SelectListItem>();
Visitors = new List<SelectListItem>();
ConversionRate = new List<SelectListItem>();
Sales = new List<SelectListItem>();
}
[NopResourceDisplayName("Admin.ShopStatistics.List.StartDate")]
[UIHint("DateNullable")]
public DateTime? StartDate { get; set; }
[NopResourceDisplayName("Admin.ShopStatistics.List.EndDate")]
[UIHint("DateNullable")]
public DateTime? EndDate { get; set; }
[NopResourceDisplayName("Admin.GAStatistics.GAStatistics.AvailableGAStatistics")]
public int GAStatisticsId { get; set; }
public List<SelectListItem> AvailableGAStatistics { get; set; }
public IList<SelectListItem> Sales { get; set; }
public IList<SelectListItem> ConversionRate { get; set; }
public IList<SelectListItem> Visitors { get; set; }
}
}
有什么想法吗??
这篇文章是相关的,我设法创建了一个临时修复: Display Google Analytics Data in View
谢谢
【问题讨论】:
-
API 不会为您总结。您必须提取访问次数,然后自己添加。
-
好的。那可能吗?所以我必须为我不想提取访问者的每一天进行查询?我的意思是如果我使用 demention "r.Sort = "-ga:date";"我不应该在我指定的日期之间的所有日子里都有访客吗?调试器中不应该显示所有日期吗?必须可以从 api 中获得每天的访问者数量,否则它如何使用 Analytics Dashboard 中的折线图?
-
你有没有先在这里测试你的请求? ga-dev-tools.appspot.com/explorer
-
如果您不需要 ga:visitCount 只需删除维度,它将为您提供该时间范围内的访问总数。使用其中的维度,您将获得每个不同访问次数的访问者。
-
更新:我写错了日期。我从 2010 年到 2014 年写的,当然数字错了,哈哈。我现在在“行”中获取指定时间跨度内的所有日期。下一个问题是我如何从数据中制作对象或至少将它们组织到列表中等等..
标签: c# asp.net-mvc google-analytics google-analytics-api google-api-client