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

时间:2014-03-07 08:03:57

标签: c# asp.net-mvc google-analytics google-analytics-api google-api-client

我从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次访问
  • .....等等

日期安排在酒店" Rows"在GaData班。但它是一个字符串列表,属性如下所示:

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

这里是GaDataRows属性:

enter image description here

这是我在调试时得到的,日期显示&#34;。看看这个:

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

THX

1 个答案:

答案 0 :(得分:0)

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

通过列标题循环:

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

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