/// <summary> /// Corrects the report region names. /// </summary> /// <param name="report">The report.</param> private static void CorrectReportRegionNames(VisitPerRegionReport report) { // Google will return Hong Kong as (city: Hong Kong, region: (not set), country: Hong Kong) VisitPerRegionReportRecord hongKongRecord = report.Records.FirstOrDefault( r => r.Country == "Hong Kong"); if (hongKongRecord != null) { hongKongRecord.City = "Hong Kong"; hongKongRecord.Region = "Hong Kong"; hongKongRecord.Country = "China"; } // Google will return Macau as (city: Macau, region: (not set), country: Macau) VisitPerRegionReportRecord macauRecord = report.Records.FirstOrDefault( r => r.Country == "Macau"); if (macauRecord != null) { macauRecord.City = "Macau"; macauRecord.Region = "Macau"; macauRecord.Country = "China"; } // Google will return Taiwan as (city: [city name], region: [region name], country: Taiwan) VisitPerRegionReportRecord taiwanRecord = report.Records.FirstOrDefault( r => r.Country == "Taiwan"); if (taiwanRecord != null) { taiwanRecord.Country = "China"; } }
/// <summary> /// Renders the specified report. /// </summary> /// <param name="report">The report.</param> /// <returns></returns> public string Render(VisitPerRegionReport report) { StringBuilder builder = new StringBuilder(); RenderHeader(builder, report); RenderBody(builder, report); RenderFooter(builder, report); return(builder.ToString()); }
/// <summary> /// Renders the specified report. /// </summary> /// <param name="report">The report.</param> /// <returns></returns> public string Render(VisitPerRegionReport report) { StringBuilder builder = new StringBuilder(); RenderHeader(builder, report); RenderBody(builder, report); RenderFooter(builder, report); return builder.ToString(); }
/// <summary> /// Renders the body. /// </summary> /// <param name="builder">The builder.</param> /// <param name="report">The report.</param> private void RenderBody(StringBuilder builder, VisitPerRegionReport report) { builder.Append( @"<table class='report'> <tr> <td>City</td> <td>Region</td> <td>Country</td> <td>Visits</td> <td>Pageviews ▼</td> <td>% Pageviews</td> </tr>"); foreach (var record in report.Records) { builder.AppendFormat( @"<tr> <td class='leftJustify'>{0}</td> <td class='leftJustify'>{1}</td> <td class='leftJustify'>{2}</td> <td>{3}</td> <td>{4}</td> <td>{5:P}</td> </tr>", record.City, record.Region, record.Country, record.Visits, record.Pageviews, Convert.ToDecimal(record.Pageviews) / Convert.ToDecimal(report.TotalPageviews)); } builder.AppendFormat( @"<tr> <td></td> <td></td> <td></td> <td>{0}</td> <td>{1}</td> <td>{2:P}</td> </tr>", report.TotalVisits, report.TotalPageviews, 1); builder.Append(@"</table>"); }
/// <summary> /// Builds the visit per region report. /// </summary> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <returns></returns> public VisitPerRegionReport BuildVisitPerRegionReport(DateTime start, DateTime end) { DataFeed feed = GetVisitPerCityDataFeed(start, end); if (feed.Entries.Count == 0) { throw new Exception("No entries found"); } else { DataSource dataSource = feed.DataSource; VisitPerRegionReport report = new VisitPerRegionReport( dataSource.TableName.TrimEnd('/'), start, end); foreach (DataEntry entry in feed.Entries) { string city = string.Empty; string region = string.Empty; string country = string.Empty; int visits = -1; int pageViews = -1; Dimension cityDimension = entry.Dimensions.FirstOrDefault( d => d.Name == Constants.Dimensions.City); if (cityDimension != null) { city = cityDimension.Value; } Dimension regionDimension = entry.Dimensions.FirstOrDefault( d => d.Name == Constants.Dimensions.Region); if (regionDimension != null) { region = regionDimension.Value; } Dimension countryDimension = entry.Dimensions.FirstOrDefault( d => d.Name == Constants.Dimensions.Country); if (countryDimension != null) { country = countryDimension.Value; } Metric visitsMetric = entry.Metrics.FirstOrDefault( m => m.Name == Constants.Metrics.Visits); if (visitsMetric != null) { visits = Convert.ToInt32(visitsMetric.Value); } Metric pageViewsMetric = entry.Metrics.FirstOrDefault( m => m.Name == Constants.Metrics.Pageviews); if (pageViewsMetric != null) { pageViews = Convert.ToInt32(pageViewsMetric.Value); } report.AddRecord(city, region, country, visits, pageViews); } CorrectReportRegionNames(report); return report; } }
/// <summary> /// Renders the header. /// </summary> /// <param name="builder">The builder.</param> /// <param name="report">The report.</param> private void RenderHeader(StringBuilder builder, VisitPerRegionReport report) { builder.AppendFormat( "<span class='sectionHeader'>{0} (by visit region)</span><br /><br />", report.WebsiteName); }
/// <summary> /// Renders the footer. /// </summary> /// <param name="builder">The builder.</param> /// <param name="report">The report.</param> private void RenderFooter(StringBuilder builder, VisitPerRegionReport report) { builder.Append("<br />"); }
public static void Main(String[] args) { try { DateTime start; if (string.IsNullOrEmpty(startDateString) || !DateTime.TryParse(startDateString, out start)) { start = DateTime.Now.AddDays(-7); // assuming this app runs on Mondays } DateTime end; if (string.IsNullOrEmpty(endDateString) || !DateTime.TryParse(endDateString, out end)) { end = DateTime.Now.AddDays(-1); // assuming this app runs on Mondays } string outputFileName = string.Format(@"{0}\VistorReport_{1:ddMMyyyy}.html", outputFolderPath, DateTime.Now); Trace.WriteLine(string.Format( "[{0}] Generating Site 1 & Site 2 visitor reports from {1:MM/dd/yyyy} to {2:MM/dd/yyyy}", DateTime.Now, start, end)); StringBuilder htmlBody = new StringBuilder(); VisitPerDateReportHtmlRenderer visitPerDateReportRenderer = new VisitPerDateReportHtmlRenderer(); VisitPerRegionHtmlReportRenderer visitPerRegionReportRenderer = new VisitPerRegionHtmlReportRenderer(); Trace.Indent(); // Site 1 visit by date report Trace.WriteLine("Creating Site 1 report builder..."); IReportBuilder site1ReportBuilder = new GoogleAnalyticsReportBuilder( userName, password, site1TableId); Trace.WriteLine("Generating Site 1 visit by date report..."); VisitPerDateReport site1VisitPerDateReport = site1ReportBuilder.BuildVisitPerDateReport( start, end); Trace.WriteLine("Rendering Site 1 visit by date report..."); htmlBody.Append(visitPerDateReportRenderer.Render(site1VisitPerDateReport)); // Site 1 visit by region report Trace.WriteLine("Generating Site 1 visit by region report..."); VisitPerRegionReport site1VisitPerRegionReport = site1ReportBuilder.BuildVisitPerRegionReport( start, end); Trace.WriteLine("Rendering Site 1 visit by region report..."); htmlBody.Append(visitPerRegionReportRenderer.Render(site1VisitPerRegionReport)); // Site 2 visit by date report Trace.WriteLine("Creating Site 2 report builder..."); IReportBuilder site2ReportBuilder = new GoogleAnalyticsReportBuilder( userName, password, site2TableId); Trace.WriteLine("Generating Site 2 visit by date report..."); VisitPerDateReport site2VisitPerDateReport = site2ReportBuilder.BuildVisitPerDateReport( start, end); Trace.WriteLine("Rendering Site 2 visit by date report..."); htmlBody.Append(visitPerDateReportRenderer.Render(site2VisitPerDateReport)); // Site 1 visit by region report Trace.WriteLine("Generating Site 2 visit by region report..."); VisitPerRegionReport site2VisitPerRegionReport = site2ReportBuilder.BuildVisitPerRegionReport( start, end); Trace.WriteLine("Rendering Site 2 visit by region report..."); htmlBody.Append(visitPerRegionReportRenderer.Render(site2VisitPerRegionReport)); Trace.WriteLine("Adding styles to final output..."); string html = HtmlReportsHelper.WrapStyles(htmlBody.ToString(), styleSheetPath); Trace.WriteLine(string.Format("Wrting output to file {0}", outputFileName)); File.WriteAllText(outputFileName, html); Trace.Unindent(); Trace.WriteLine(string.Format("[{0}] Report generation completed.", DateTime.Now)); Trace.WriteLine(string.Empty); } catch (Exception ex) { Trace.WriteLine("Exception Caught!"); Trace.WriteLine(string.Format("[{0}] {1}", DateTime.Now, ex.Message)); } }
/// <summary> /// Builds the visit per region report. /// </summary> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <returns></returns> public VisitPerRegionReport BuildVisitPerRegionReport(DateTime start, DateTime end) { DataFeed feed = GetVisitPerCityDataFeed(start, end); if (feed.Entries.Count == 0) { throw new Exception("No entries found"); } else { DataSource dataSource = feed.DataSource; VisitPerRegionReport report = new VisitPerRegionReport( dataSource.TableName.TrimEnd('/'), start, end); foreach (DataEntry entry in feed.Entries) { string city = string.Empty; string region = string.Empty; string country = string.Empty; int visits = -1; int pageViews = -1; Dimension cityDimension = entry.Dimensions.FirstOrDefault( d => d.Name == Constants.Dimensions.City); if (cityDimension != null) { city = cityDimension.Value; } Dimension regionDimension = entry.Dimensions.FirstOrDefault( d => d.Name == Constants.Dimensions.Region); if (regionDimension != null) { region = regionDimension.Value; } Dimension countryDimension = entry.Dimensions.FirstOrDefault( d => d.Name == Constants.Dimensions.Country); if (countryDimension != null) { country = countryDimension.Value; } Metric visitsMetric = entry.Metrics.FirstOrDefault( m => m.Name == Constants.Metrics.Visits); if (visitsMetric != null) { visits = Convert.ToInt32(visitsMetric.Value); } Metric pageViewsMetric = entry.Metrics.FirstOrDefault( m => m.Name == Constants.Metrics.Pageviews); if (pageViewsMetric != null) { pageViews = Convert.ToInt32(pageViewsMetric.Value); } report.AddRecord(city, region, country, visits, pageViews); } CorrectReportRegionNames(report); return(report); } }