首页 文章

在.NET / C#中组织和使用Google Analytics数据?

提问于
浏览
3

我正在从Google AnalyticsAPI中提取数据,我希望每天提取总访问者,并将其按日期排序列表进行整理 .

例如:

属性“行”包含我指定的时间范围内的所有日期 .

每行包含:

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 类的"Rows" property 中进行组织 . 但是它是一个字符串列表,属性如下所示:

public virtual IList<IList<string>> Rows { get; set; }

这是带有 Rows 属性的 GaData 类:

enter image description here

这是我在调试时得到的,日期显示“ . 检查出来:

enter image description here

每行包含查询StartDate和EndDate中每天的访问者数据!

这是包含度量,维度等的嵌套类:

enter image description here

这是我目前的查询:

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%

我现在已经很近了 . 我想在这里存档的是:

enter image description here

我的代码看起来像这样:

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 + "
" + "------------------------------------------" + "
" + "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

谢谢

1 回答

  • 0

    数据已在列表中返回给您:

    通过列 Headers 循环:

    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);
                }
            }
    

    我还想补充一点,我认为这个问题超出了原始问题的范围 . 此外,赏金问题听起来不像原始问题,对我来说听起来更像是你希望有人为你做你的工作 .

相关问题