title: Organize and work with Google Analytics data in .NET/C#?
body: <p>I'm extracting data from Google Analytics API and i want to extract total visitors per day and organize them in a list ordered by dates. </p>
  
  <p>For example:</p>
  
  <p>Property "Rows" contains all the dates within the timeframe I specified.</p>
  
  <p>Each row contains:</p>
  
  <pre><code>Date - 2014-02-24
  Visitors - 3000
  newVisits - 2400
  PageViews - 10000
  PercentNewVisits - 38,001302
  </code></pre>
  
  <p>I need to organize and structure this data so i can display it properly in my C# / .NET application but how??</p>
  
  <p>For example:</p>
  
  <p>If I choose start and endate "2014-01-24 - 2014-02-28" I want to see all visits between those days.</p>
  
  <ul>
  <li>2014-01-24 = 100 visits</li>
  <li>2014-01-25 = 200 visits</li>
  <li>..... and so on</li>
  </ul>
  
  <p>Dates are organised in the property "Rows" in <code>GaData</code> class. However it's a list of strings and the property looks like this:</p>
  
  <pre><code>public virtual IList&lt;IList&lt;string&gt;&gt; Rows { get; set; }
  </code></pre>
  
  <p>Here's the <code>GaData</code> class with <code>Rows</code> property:</p>
  
  <p><img src="https://i.stack.imgur.com/ogp90.png" alt="enter image description here"></p>
  
  <p>This is what i get when debugging, the dates are displayed". Check this out:</p>
  
  <p><img src="https://i.stack.imgur.com/EetDJ.png" alt="enter image description here"></p>
  
  <p>Each row contains visitor data for each day within the query StartDate and EndDate!</p>
  
  <p>Here's the nested class with metric, dimensions etc:</p>
  
  <p><img src="https://i.stack.imgur.com/SCxV8.png" alt="enter image description here"></p>
  
  <p>This is my current query:</p>
  
  <pre><code>var r = gas.Data.Ga.Get("ProfileID", "2014-01-24", "2014-02-28", "ga:pageviews,ga:newVisits,ga:visitors,ga:percentNewVisits");
  
  r.Dimensions = "ga:date";
  r.Sort = "-ga:date";
  r.MaxResults = 10000;
  
  Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
  
  Console.WriteLine("VisitorStats" + "  " + 
                    d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "\r\n" +
                      "------------------------------------------" + "\r\n" +
                   "Visitors:" + " " + d.TotalsForAllResults["ga:visitors"] + "\r\n" +
                   "NewVisits:" + " " + d.TotalsForAllResults["ga:newVisits"] + "\r\n" +
                   "PageViews:" + " " + d.TotalsForAllResults["ga:pageviews"] + "\r\n" +
                   "PercentNewVisits:" + " " + d.TotalsForAllResults["ga:percentNewVisits"] +"%");
  </code></pre>
  
  <p>And this is the output I get:</p>
  
  <pre><code> VisitorStats 2010-02-24 - 2014-02-24
          ------------------------------------------
          NewVisits: 343272
          NewVisits: 147693
          PageViews: 255000
          PersentNewVisits: 42.54700702044485%
  </code></pre>
  
  <p>I'm getting pretty close now. What I'm trying to archive here is this:</p>
  
  <p><img src="https://i.stack.imgur.com/cBbGW.png" alt="enter image description here"></p>
  
  <p>My code looks like this:</p>
  
  <p><code>ControllerClass</code>:</p>
  
  <pre><code>public class GAStatisticsController : Controller
  {
          // GET: /ShopStatistics/
          public string GAnalyticsService()
          {
              //Users Google Service Account
              string serviceAccountEmail = "xxx@developer.gserviceaccount.com";
  
              //Public key for authorisation
              X509Certificate2 certificate = new X509Certificate2(@"C:\Users\xxx\NopCommerce\Presentation\Nop.Web\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
  
              //Account credentials of authorisation
              ServiceAccountCredential credential = new ServiceAccountCredential(
              new ServiceAccountCredential.Initializer(serviceAccountEmail)
              {
                  Scopes = new[] { AnalyticsService.Scope.Analytics }
              }.FromCertificate(certificate));
  
              // Create the service.
              //Twistandtango
              AnalyticsService GoogleAnalyticsService = new AnalyticsService(new BaseClientService.Initializer()
              {
                  HttpClientInitializer = credential,
                  ApplicationName = "Twist",
              });
  
              var r = gas.Data.Ga.Get("ProfileID", "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 = request.Execute();
  
              return "Visitor statistics" + "  " +
                      d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "&lt;br/&gt;" +
                      "------------------------------------------" + "&lt;br/&gt;" +
                   "Total visitors:" + " " + d.TotalsForAllResults["ga:visitors"].ToString();
          }
  
          public ActionResult GAStatistics()
          {
              GAnalyticsService();
  
              return View(new GAStatisticsListModel());
          }
      }
  }
  </code></pre>
  
  <p><code>ListModel</code> so customer can select what to query. StartDate, EndDate, Visitors, Sales, ConversionRate etc (this model is not doing anything atm):</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  using System.Web.Mvc;
  using Nop.Web.Framework;
  using Nop.Web.Framework.Mvc;
  
  namespace Nop.Admin.Models.GAStatistics
  {
      public class GAStatisticsListModel : BaseNopModel
      {
          public GAStatisticsListModel()
          {
              AvailableGAStatistics = new List&lt;SelectListItem&gt;();
  
              Visitors = new List&lt;SelectListItem&gt;();
              ConversionRate = new List&lt;SelectListItem&gt;();
              Sales = new List&lt;SelectListItem&gt;();
          }
  
          [NopResourceDisplayName("Admin.ShopStatistics.List.StartDate")]
          [UIHint("DateNullable")]
          public DateTime? StartDate { get; set; }
  
          [NopResourceDisplayName("Admin.ShopStatistics.List.EndDate")]
          [UIHint("DateNullable")]
          public DateTime? EndDate { get; set; }
           [NopResourceDisplayName("Admin.GAStatistics.GAStatistics.AvailableGAStatistics")]
          public int GAStatisticsId { get; set; }
  
          public List&lt;SelectListItem&gt; AvailableGAStatistics { get; set; }
          public IList&lt;SelectListItem&gt; Sales { get; set; }
          public IList&lt;SelectListItem&gt; ConversionRate { get; set; }
          public IList&lt;SelectListItem&gt; Visitors { get; set; }
      }
  }
  </code></pre>
  
  <p>Any ideas??</p>
  
  <p>This post is related and I've managed to create a temporary fix:
  <a href="https://stackoverflow.com/questions/22224753/display-google-analytics-data-in-view">Display Google Analytics Data in View</a></p>
  
  <p>Thx</p>