/// <summary> /// Save an order to PDF /// </summary> /// <param name="order">Order</param> /// <param name="languageId">Language identifier; 0 to use a language used when placing an order</param> /// <param name="vendorId">Vendor ident</param> /// <returns>A download ident</returns> public virtual async Task <string> SaveOrderToBinary(Order order, string languageId, string vendorId = "") { if (order == null) { throw new ArgumentNullException("order"); } string fileName = string.Format("order_{0}_{1}", order.OrderGuid, CommonHelper.GenerateRandomDigitCode(4)); string downloadId = string.Empty; using (MemoryStream ms = new MemoryStream()) { var orders = new List <Order> { order }; await PrintOrdersToPdf(ms, orders, languageId, vendorId); var download = new Core.Domain.Media.Download { Filename = fileName, Extension = ".pdf", DownloadBinary = ms.ToArray(), ContentType = "application/pdf", }; await _downloadService.InsertDownload(download); downloadId = download.Id; } return(downloadId); }
private GoogleAnaliticsStatisticModel GetAnaliticsDataMethod(string scope, string clientId, string keyPassword, Core.Domain.Media.Download download, DateTime startDate, DateTime endDate) { var model = new GoogleAnaliticsStatisticModel(); AuthorizationServerDescription desc = GoogleAuthenticationServer.Description; //Create a certificate object to use when authenticating X509Certificate2 key = new X509Certificate2(download.DownloadBinary, keyPassword, X509KeyStorageFlags.Exportable); //Now, we will log in and authenticate, passing in the description //and key from above, then setting the accountId and scope AssertionFlowClient client = new AssertionFlowClient(desc, key) { ServiceAccountId = clientId, Scope = scope, }; //Finally, complete the authentication process //NOTE: This is the first change from the update above OAuth2Authenticator <AssertionFlowClient> auth = new OAuth2Authenticator <AssertionFlowClient>(client, AssertionFlowClient.GetState); //First, create a new service object //NOTE: this is the second change from the update //above. Thanks to James for pointing this out AnalyticsService gas = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = auth }); //Create our query //The Data.Ga.Get needs the parameters: //Analytics account id, starting with ga: //Start date in format YYYY-MM-DD //End date in format YYYY-MM-DD //A string specifying the metrics DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:" + _googleAnliticsSettings.AccountId, startDate.ToString("yyy-MM-dd"), endDate.ToString("yyy-MM-dd"), "ga:visitors,ga:uniquePageviews,ga:AvgTimeOnSite,ga:exitRate"); //Specify some addition query parameters //r.Sort = "-ga:visitors"; //r.Sort = "-ga:AvgTimeOnSite"; //r.Sort = "-ga:uniquePageviews"; //r.Sort = "-ga:exitRate"; r.MaxResults = 50; //Execute and fetch the results of our query GaData d = r.Fetch(); //Write the column headers //Write the data foreach (var row in d.Rows) { model.Visitors = row[0]; model.UniquePageViews = row[1]; model.AverageTimeOnSite = row[2]; if (row[3].Length > 5) { model.ExitRate = row[3].Substring(0, 5); } else { model.ExitRate = row[3]; } } r = gas.Data.Ga.Get("ga:" + _googleAnliticsSettings.AccountId, startDate.ToString("yyy-MM-dd"), endDate.ToString("yyy-MM-dd"), "ga:visitors"); r.Dimensions = "ga:visitorType"; r.MaxResults = 50; //Execute and fetch the results of our query d = r.Fetch(); if (d.Rows.Count == 1) { if (d.Rows[0][0] != "Returning Visitor") { model.NewToOldVisitorsRate = "100%"; } else { model.NewToOldVisitorsRate = "0%"; } } else { model.NewToOldVisitorsRate = (((double)(int.Parse(d.Rows[0][1])) / (int.Parse(d.Rows[1][1]))) * 100).ToString() + "%"; } if (model.NewToOldVisitorsRate.Length > 5) { model.NewToOldVisitorsRate = model.NewToOldVisitorsRate.Substring(0, 5); } if (model.AverageTimeOnSite.Length - model.AverageTimeOnSite.IndexOf(".") > 3) { model.AverageTimeOnSite = model.AverageTimeOnSite.Substring(0, model.AverageTimeOnSite.IndexOf(".") + 3); } return(model); }