【问题标题】:Organize and work with Google Analytics data in .NET/C#?在 .NET/C# 中组织和使用 Google Analytics 数据?
【发布时间】: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


【解决方案1】:

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

遍历列标题:

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

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

【讨论】:

  • 你好。当然,我不希望任何人为我做这项工作,这不是重点。关键是能够在列表或数组中分离数据。例如,从 GaData.Rows 中提取所有日期并制作日期列表。我宁愿希望有一些有用的建议。如果赏金问题给人留下了这样的印象,我很抱歉。
  • 如果您不知道如何在 C# 中制作列表,我建议您发布一个关于此的问题。它与 GA api 无关。
  • 好吧,我在编程方面还很新,但我确实知道如何制作清单。我只是无法从谷歌获取我不想要的值并将它们放入我自己的列表中。 Annywas,想出了如何提取我想要的东西,这很好用: foreach (var row in d.Rows) { Console.WriteLine("Date:" + " " + row[0] + " " + "Visitors:" + " " + 行[1]); Console.ReadLine();这将两个值按预期分开。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 2018-03-16
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多