【问题标题】:Adding custom variables for querying - Google Analytics添加用于查询的自定义变量 - Google Analytics
【发布时间】:2014-03-18 09:31:39
【问题描述】:

这是一个 Google Analytics 客户的工作示例,它按日期从 Google Analytics 帐户返回访问者。为了获取数据,我必须使用 Core Report API 向 Google Analytics API 发送带有查询参数(StartDate、EndDate、Metrics、Dimensions)的请求。

正如您在下面看到的这一行 - var request = _GoogleAnalyticsService.Data.Ga.Get("ga:xxxxxxxx", "2014-01-24", "2014-02-28", "ga:visitors"); - 用作查询数据的参数,所有这些都是来自 Core Reporting API 的字符串值属性,用于将查询发送到 Google Analytics API

我想为 StartDate 和 EndDate 添加 DateTime 变量,用户可以在其中编写任何 StartDate 和 EndDate。在发送请求之前,用户将能够在控制台中输入 StartDate 和 EndDate。我必须将这些变量解析为字符串(“yyyymmdd”)才能使请求正常工作。

我在想这样的事情:

("ga:xxxxxxxx", CustomStartDate, CustomEndDate, "ga:visitors");

我已经尝试过了,但它当然行不通,因为我无法在创建请求之前声明这些变量:

    DateTime StartDate = DateTime.ParseExact(request.StartDate, "ddMMyyyy", 
                                      CultureInfo.InvariantCulture);
                                      StartDate.ToString("yyyyMMdd");

    DateTime EndDate = DateTime.ParseExact(request.EndDate, "ddMMyyyy", 
                                      CultureInfo.InvariantCulture);
                                      EndDate.ToString("yyyyMMdd");

var request = _GoogleAnalyticsService.Data.Ga.Get("ga:xxxxxxxx", StartDate, EndDate, "ga:visitors");

但是我可以这样做(用户可以写任何日期来查询):

        Console.WriteLine("Enter StartDate! (yyyy-mm-dd)");
        string A = Console.ReadLine();
        Console.WriteLine("Enter EndDate! (yyyy-mm-dd)");
        string B = Console.ReadLine();

        var r = _GoogleAnalyticsService.Data.Ga.Get("ga:59380223", A, B, "ga:visitors");

有什么想法吗?

客户:

public class Program
{
                public static void Main(string[] args)
                {
                    //Google Service Account Credentials
                    var serviceAccountEmail = "xxxxxx@developer.gserviceaccount.com";
                    var certificate = new X509Certificate2(@"C:\Users\MyApp\key.p12", "notasecret", X509KeyStorageFlags.Exportable);

                    var credential = new ServiceAccountCredential(
                          new ServiceAccountCredential.Initializer(serviceAccountEmail)
                          {
                              Scopes = new[] { AnalyticsService.Scope.Analytics }
                          }.FromCertificate(certificate));

                    // Create the service.
                    //MyApp
                    var _GoogleAnalyticsService = new AnalyticsService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "TestGoogleAnalytics",
                    });





                    var request = _GoogleAnalyticsService.Data.Ga.Get("ga:xxxxxxxx", "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 = r.Execute();


                       List<GAStatistics> ListGaVisitors = new List<GAStatistics>();

                Console.WriteLine("StartDate:" + " " + r.StartDate + " " + "-" + " " + "EndDate:" + " " + r.EndDate + "\r\n" + 
                                  "----------------------------------------------------------");

                foreach (var row in d.Rows)
                {

                    GAStatistics GaVisits = new GAStatistics(row[0], row[1]);
                    ListGaVisitors.Add(GaVisits);

                    Console.Write("Date:" + " " + GaVisits.TotalDate + " " + "-" + " " + "Visitors:" + " " + GaVisits.TotalVisitors);
                    Console.ReadLine();

                }

                    }

                }

【问题讨论】:

  • 您不能在请求中为 startdate 和 EndDate 添加时间。 developers.google.com/analytics/devguides/reporting/core/v3/…
  • 嗯,好吧。但我可以在请求字符串中写下我不想写的任何日期。例如,只要有一些数据要查询,我就可以写 StartDate 2014-03-18、2013-01-12、2011-07-23 和任何其他日期。所以理论上我应该能够创建一个接受值(yyyymmdd)的 DateTime 变量,然后将其转换为字符串并在请求中将其作为 StartDate 传递。还是您的意思是不可能,因为我无法在发送请求之前声明它们?
  • 是的,您可以在请求之前声明它们。
  • 是的,这行得通:string A = "2014-01-24";字符串 B = "2014-02-28"; var r = _GoogleAnalyticsService.Data.Ga.Get("ga:59380223", A, B, "ga:visitors");如果我理解正确,我真的不想增加时间。我只是不希望用户能够写出他想要查询的日期。

标签: c# .net parsing google-analytics-api google-api-dotnet-client


【解决方案1】:

此问题已在此处Convert DateTime to string "yyyy-mm-dd" 的另一个线程中得到回答。Lloyd 提供的答案。

  Console.WriteLine("Enter StartDate! (yyyy-MM-dd)");
            var S = Console.ReadLine();
            var StartDate = DateTime.ParseExact(S, "yyyy-MM-dd", CultureInfo.InvariantCulture);


            Console.WriteLine("Enter EndDate! (yyyy-MM-dd)");
            var E = Console.ReadLine();
            var EndDate = DateTime.ParseExact(E, "yyyy-MM-dd", CultureInfo.InvariantCulture);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多