WhoAmI
WhoAmI

Reputation: 1236

Adding custom variables for querying - Google Analytics

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.

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

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.

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 = "[email protected]";
                    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();

                }

                    }

                }

Upvotes: 0

Views: 602

Answers (1)

WhoAmI
WhoAmI

Reputation: 1236

This question was answered in another thread here Convert DateTime to string "yyyy-mm-dd" Answer provided by 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);

Upvotes: 0

Related Questions