0
votes

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

                }

                    }

                }
1
You cant add time to the startdate and EndDate in the request. developers.google.com/analytics/devguides/reporting/core/v3/… - DaImTo
Hmm okey. But i can write anny date i wan't in the reguest string. For example i could write StartDate 2014-03-18, 2013-01-12, 2011-07-23 and anny other date as long as there is some data to querry. So in theory i should be able to create a DateTime variable that accepts value (yyyymmdd) then converts it in to a string and pass it as StartDate in the request. Or did you mean it's impossible as i can't declare them before sending the request? - WhoAmI
Yes you can declare them before the request. - DaImTo
Yep this works: string A = "2014-01-24"; string B = "2014-02-28"; var r = _GoogleAnalyticsService.Data.Ga.Get("ga:59380223", A, B, "ga:visitors"); I really dont want to add time if i understand you correctly. I just wan't the user to be able to write wich dates he wants to querry. - WhoAmI

1 Answers

0
votes

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