1

I'm working on a Google Analytics Client for an MVC application that collects data from Analytics API and I'm trying display the data in a view. The ultimate goal is to display the Google Analytics Data with Google Charts.

However I'm not sure how to properly structure the Data. Each metric is a dictionary with a KeyValuePair where metric name is key and value is the actual value. For example ga:visitors, 3000. I need to organize each metric with its KeyValuePair in to a dictionary that I can return to the view.

For example, first I wrote a console application that returned 3 metrics:

  • ga:visitors
  • ga:newVisits
  • ga:percentNewVisits

Note: each of this metrics is a separate dictionary with KeyValuePair. Ex: [ga:visitors, 3000]

When displaying the data in the console, the code looks like this:

Console.WriteLine("VisitorStatistics" + "  " + 
                d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "\r\n" +
                "------------------------------------------" + "\r\n" +
             "Visitors:" + " " + d.TotalsForAllResults["ga:visitors"] + "\r\n" +
             "New Visitors:" + " " + d.TotalsForAllResults["ga:newVisits"] + "\r\n" +
             "Percent New Visitors:" + " " + d.TotalsForAllResults["ga:percentNewVisits"] +"%");

I need to display this data in my MVC 4 /asp.net application but I'm not sure how to achieve this. All code is located here in my controller:

public void GAnalyticsService()
        {
         var serviceAccountEmail = "xxxxx.gserviceaccount.com";
         var certificate = new x509Certificate2(@"C:\Users\User\Desktop\MyApp\Presentation\Nop.Web\key.p12", "notasecret", X509KeyStorageFlags.Exportable);

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

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

        var request = GoogleAnalyticsService.Data.Ga.Get("ProfileID", "2010-02-24", "2014-02-24", "ga:visitors,ga:newVisits,ga:percentNewVisits");

        //Specify some addition query parameters
        request.Dimensions = "ga:visitCount";
        request.Sort = "-ga:visitors";
        request.MaxResults = 10000;
        //Execute and fetch the results of our query
        Google.Apis.Analytics.v3.Data.GaData d = request.Execute();
        }

public ActionResult GAStatistics()  {
     //GAnalyticsService();
     return View(new GAStatisticsListModel());
    }





    }

Here's the data i'm recieving from Google Analytics API:

Total results with 3 metrics (each a dictionary with KeyValuePair) enter image description here

The KeyValuePair: enter image description here

I just need to organise this data and display it in the view, just plain data (text) is fine. How do i do this?

Any suggestions would be of great help.

WhoAmI
  • 1,188
  • 6
  • 17
  • 47

1 Answers1

0

Did a temporary fix just to display the data. It serves it's purpose for the time beeing.

Changed GAnalyticsService from a void to a string method.

Returned this:

return "Besöksstatistik" + "  " +
                    d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "<br/>" +
                    "------------------------------------------" + "<br/>" +
                 "Antal besökare:" + " " + d.TotalsForAllResults["ga:visitors"] + "<br/>" +
                 "Antal nya besökare:" + " " + d.TotalsForAllResults["ga:newVisits"] + "<br/>" +
                 "Procent nya besökare:" + " " + d.TotalsForAllResults["ga:percentNewVisits"] + "%".ToString();

Got the following output in the view:

Besöksstatistik 2010-02-24 - 2014-02-24
------------------------------------------
Antal besökare: 343272
Antal nya besökare: 147693
Procent nya besökare: 42.54700702044485%
WhoAmI
  • 1,188
  • 6
  • 17
  • 47