public static AnalyticsReport GetReport(AnalyticsProfile profile, IAuthenticator authenticator) { AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = authenticator }); var data = service.Data.Ga.Get("ga:" + profile.id, profile.startDate.ToString("yyyy-MM-dd"), profile.endDate.ToString("yyyy-MM-dd"), "ga:visits,ga:bounces,ga:pageviews"); data.Dimensions = "ga:source,ga:keyword,ga:pagePath,ga:city,ga:date,ga:landingPagePath,ga:visitorType"; // data.MaxResults = 500; if (data.Fetch() == null) { try { //reauth if auth fail IAuthenticator auth = Utils.getCredsnew(AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()); data =service.Data.Ga.Get("ga:" + profile.id, profile.startDate.ToString("yyyy-MM-dd"), profile.endDate.ToString("yyyy-MM-dd"), "ga:visits,ga:bounces,ga:pageviews"); data.Dimensions = "ga:source,ga:keyword,ga:pagePath,ga:city,ga:date,ga:landingPagePath,ga:visitorType"; } catch (Exception ex) { } } GaData garesults = data.Fetch(); AnalyticsReport report = new AnalyticsReport(); report.id = profile.id; report.JSON = garesults.Rows; var test = garesults.TotalsForAllResults; report.TotalVisits = Convert.ToInt32(test["ga:visits"]); report.TotalPageViews = Convert.ToInt32(test["ga:pageviews"]); var bounces = Convert.ToInt32(test["ga:bounces"]); var visits = Convert.ToInt32(test["ga:visits"]); report.BounceRate = ( visits / 2); //Referral List List<string> reflinks = new List<string>(); List<string> keyWordList = new List<string>(); List<string> landingPages = new List<string>(); List<string> cityList = new List<string>(); List<string> pagePathList = new List<string>(); List<string> visitorType = new List<string>(); List<int> dayList = new List<int>(); List<int> totalVisitors = new List<int>(); List<int> newVisitors = new List<int>(); List<int> returningVisitors = new List<int>(); int maxReturningVisitors = 0; int maxNewVisitors = 0; if (garesults.Rows != null){ foreach (var a in garesults.Rows) { string visType = a[6]; var day = Convert.ToInt32(a[4]); if (dayList.Count() == 0) { dayList.Add(day); } else { var lastday = dayList.Last(); if (day != lastday) { dayList.Add(day); filltoSameSize(newVisitors, returningVisitors); } } int numVisits = Convert.ToInt32(a[7]); if (visType == "New Visitor") { newVisitors.Add(numVisits); report.TotalNewVisitors = (report.TotalNewVisitors + numVisits); maxNewVisitors = Math.Max(maxNewVisitors, numVisits); totalVisitors.Add(numVisits); } else { totalVisitors.Add(numVisits); returningVisitors.Add(numVisits); report.TotalReturningVisitors = (report.TotalReturningVisitors + numVisits); maxReturningVisitors = Math.Max(maxReturningVisitors, numVisits); } reflinks.Add(a[0]); keyWordList.Add(a[1]); pagePathList.Add(a[2]); cityList.Add(a[3]); dayList.Add(day); landingPages.Add(a[5]); visitorType.Add(a[6]); } filltoSameSize(newVisitors, returningVisitors); report.totalVisitors = totalVisitors; report.maxNewVisitors = maxNewVisitors; report.maxReturningVisitors = maxReturningVisitors; report.newVisitors = newVisitors; report.retVisitors = returningVisitors; report.landingpagelist = landingPages; report.keylist = keyWordList; report.reflist = reflinks; report.keylist = keyWordList; report.citylist = cityList; report.pagelist = pagePathList; report.daylist = dayList; //KeyWord Entrances // report.newVisitors = (from pages in arrPage group pages by pages into p where p.Key != null && !p.Key.Contains("Error") orderby p.Count() descending select new { Key = p.Key, Count = p.Count() }).Take(7); return report; } else { return null; } }
public void getVisitChart(AnalyticsReport report) { var entries = report.JSON; string[] returningVisitors; string[] newVisitors; string[] days; int maxReturn = 0; int maxNewVisitor = 0; foreach (var a in entries) { } //var retVisitors; }
public static AnalyticsReport GetProfileReport(string id, IAuthenticator authenticator) { AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = authenticator }); var data = service.Data.Ga.Get("ga:" + id, "2012-10-01", "2012-11-01", "ga:visits,ga:bounces,ga:pageviews"); data.Dimensions = "ga:source,ga:keyword,ga:pagePath,ga:city,ga:date,ga:landingPagePath,ga:visitorType"; // data.MaxResults = 500; GaData garesults = data.Fetch(); AnalyticsReport report = new AnalyticsReport(); report.id = id; report.JSON = garesults.Rows; var test = garesults.TotalsForAllResults; report.TotalVisits = Convert.ToInt32(test["ga:visits"]); report.TotalPageViews = Convert.ToInt32(test["ga:pageviews"]); var bounces = Convert.ToInt32(test["ga:bounces"]); var visits = Convert.ToInt32(test["ga:visits"]); report.BounceRate = (bounces / visits); //Referral List List<string> reflinks = new List<string>(); List<string> keyWordList = new List<string>(); List<string> landingPages = new List<string>(); List<string> pagePathList = new List<string>(); List<string> cityList = new List<string>(); List<string> visitorType = new List<string>(); List<int> dayList = new List<int>(); List<int> newVisitors = new List<int>(); List<int> returningVisitors = new List<int>(); int maxReturningVisitors = 0; int maxNewVisitors = 0; foreach (var a in garesults.Rows) { string visType = a[6]; var day = Convert.ToInt32(a[4]); if (dayList.Count() == 0) { dayList.Add(day); } else { var lastday = dayList.Last(); if (day != lastday) { dayList.Add(day); filltoSameSize(newVisitors, returningVisitors); } } int numVisits = Convert.ToInt32(a[7]); if(visType == "New Visitor"){ newVisitors.Add(numVisits); report.TotalNewVisitors = (report.TotalNewVisitors + numVisits); maxNewVisitors = Math.Max(maxNewVisitors, numVisits); } else { returningVisitors.Add(numVisits); report.TotalReturningVisitors = (report.TotalReturningVisitors + numVisits); maxReturningVisitors = Math.Max(maxReturningVisitors, numVisits); } reflinks.Add(a[0]); keyWordList.Add(a[1]); pagePathList.Add(a[2]); cityList.Add(a[3]); dayList.Add(day); landingPages.Add(a[5]); visitorType.Add(a[6]); } filltoSameSize(newVisitors, returningVisitors); report.maxNewVisitors = maxNewVisitors; report.maxReturningVisitors = maxReturningVisitors; report.newVisitors = newVisitors; report.retVisitors = returningVisitors; report.keylist = keyWordList; report.reflist = reflinks; report.citylist = cityList; report.landingpagelist = pagePathList; report.daylist = dayList; //KeyWord Entrances // report.newVisitors = (from pages in arrPage group pages by pages into p where p.Key != null && !p.Key.Contains("Error") orderby p.Count() descending select new { Key = p.Key, Count = p.Count() }).Take(7); return report; }