添加自定义变量以进行查询 - Google Analytics

时间:2021-08-20 15:14:19

Here is a working example of a Google Analytics Client the returns Visitors by date from a Google Analytics Account. In order to get the data i must send a request with query parameters (StartDate, EndDate, Metrics, Dimensions) to the Google Analytics API using Core Report API.

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

As you can see below this line - var request = _GoogleAnalyticsService.Data.Ga.Get("ga:xxxxxxxx", "2014-01-24", "2014-02-28", "ga:visitors"); - is used as parameeters to query data, all of these are string-value-properties from the Core Reporting API which are used to send the query to Google Analytics API

如下所示,此行 - var request = _GoogleAnalyticsService.Data.Ga.Get(“ga:xxxxxxxx”,“2014-01-24”,“2014-02-28”,“ga:visitor”); - 用作查询数据的参数,所有这些都是来自Core Reporting API的字符串值属性,用于将查询发送到Google AnalyticsAPI

I would like to add DateTime variables for StartDate and EndDate in which the user can write any StartDate and EndDate. The user would be able to enter a StartDate and an EndDate in the Console before the request are being sent. I would have to parse these variables to string ("yyyymmdd") in order for the request to work though.

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

I'm thinking something like this:

我在想这样的事情:

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

I've tried this but it will not work of course as I can't declare these variables before I create the request:

我试过这个,但它当然不会起作用,因为我在创建请求之前无法声明这些变量:

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

However I can do this (The user are able to write any dates to query):

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

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

Any idea?

Client:

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

                }

                    }

                }

1 个解决方案

#1


0  

This question was answered in another thread here Convert DateTime to string "yyyy-mm-dd" Answer provided by Lloyd.

这个问题在另一个线程中得到了回答,将Date DateTime转换为字符串“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);

#1


0  

This question was answered in another thread here Convert DateTime to string "yyyy-mm-dd" Answer provided by Lloyd.

这个问题在另一个线程中得到了回答,将Date DateTime转换为字符串“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);