添加自定义变量查询-谷歌分析

本文关键字:谷歌 查询 自定义 变量 添加 | 更新日期: 2023-09-27 17:50:41

这是一个谷歌分析客户端按日期返回访问者从谷歌分析帐户的工作示例。为了获得数据,我必须发送一个请求与查询参数(开始日期,结束日期,指标,尺寸)到谷歌分析API使用核心报告API。

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

我想为开始日期和结束日期添加DateTime变量,用户可以在其中写入任何开始日期和结束日期。在发送请求之前,用户可以在控制台中输入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();
                }
                    }
                }

添加自定义变量查询-谷歌分析

这个问题在这里的另一个线程中得到了回答。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);