public JsonResult ViewPDF(AnalyticsProfile profile)
        {
            IAuthenticator authenticator = Session["authenticator"] as IAuthenticator;
            var garesults = Utils.GetReport(profile, authenticator);

                garesults.profile = profile;
                var html = RenderViewToString("Report", garesults, this.ControllerContext);
               var output = Printer.GeneratePDF(html, profile.id);
            //    Response.BinaryWrite(output.GetBuffer());
            //    Response.Buffer = true;
            //    //Response.Clear();

            //    Response.ContentType = "application/pdf";
            //    Response.AddHeader("content-length", output.Length.ToString());
            //    Response.AddHeader("content-disposition", "attachment; filename=Test.pdf");

            ////Response.Flush();
             //  return File(result.Attachment.Data, MimeExtensionHelper.GetMimeType(result.Attachment.FileName), result.Attachment.FileName);
            //FileContentResult result = new FileContentResult(output.GetBuffer(), "application/octet-stream")
            //    {
            //        FileDownloadName = "Test.pdf"
            //    };

            return Json(output);
            // return FileContentResult(output, "application/pdf", Server.UrlEncode("Test.pdf"));
        }
Exemple #2
0
        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 ActionResult Report(AnalyticsProfile profile)
 {
     IAuthenticator authenticator = Session["authenticator"] as IAuthenticator;
        var garesults = Utils.GetReport(profile, authenticator);
     if(garesults != null){
     garesults.profile = profile;
     var html = RenderViewToString("Report", garesults, this.ControllerContext);
     Printer.GeneratePDF(html, profile.id);
     return View(garesults);
     }
     else
     {
         return View("Index");
     }
 }