Exemplo n.º 1
0
 private void AddToTempFile(ReportsTable data, string fileLocation)
 {
     try
     {
         using (FileStream aFile = new FileStream(fileLocation, FileMode.Append, FileAccess.Write))
             using (StreamWriter sw = new StreamWriter(aFile))
             {
                 foreach (var row in data.Data)
                 {
                     sw.WriteLine(String.Join(",", row));
                 }
                 data = null;
             }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message, ex.InnerException);
     }
 }
Exemplo n.º 2
0
        public void GetGoogleReport(string viewIds, string apiQuery, DateTime startDate, DateTime endDate, string tempFileLocation, string localFilePath)
        {
            try
            {
                if (svc == null)
                {
                    throw new Exception("No service available");
                }

                var headers = new List <string>();

                foreach (var viewId in viewIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    string pageToken  = "";
                    var    allResults = new List <Report>();
                    while (pageToken != null)
                    {
                        // pageToken = null;
                        var       request   = new ReportRequest();
                        var       firstDate = startDate.ToString("yyyy-MM-dd");
                        var       lastDate  = endDate.ToString("yyyy-MM-dd");
                        DateRange dateRange = new DateRange()
                        {
                            StartDate = firstDate, EndDate = lastDate
                        };
                        request.DateRanges = new List <DateRange>()
                        {
                            dateRange
                        };
                        request.PageSize      = 10000;
                        request.SamplingLevel = "LARGE";
                        request.ViewId        = "ga:" + viewId;
                        request.Metrics       = GetMetrics(apiQuery);
                        request.Dimensions    = GetDimensions(apiQuery);
                        request.PageToken     = pageToken == "" ? null : pageToken; // send null for 1st request
                                                                                    // get the report
                        var body = new GetReportsRequest();
                        body.ReportRequests = new List <ReportRequest>()
                        {
                            request
                        };

                        var reports = svc.Reports.BatchGet(body).Execute();
                        Thread.Sleep(1000);
                        if (reports?.Reports?[0]?.Data?.RowCount != null)
                        {
                            allResults.AddRange(reports.Reports);
                            pageToken = reports.Reports[0].NextPageToken;
                            reports   = null;
                        }
                        else
                        {
                            pageToken = null;
                        }
                    }
                    if (allResults.Count > 0)
                    {
                        var apiResults = new ReportsTable(allResults, viewId);
                        headers = apiResults.Headers;
                        // add the report to the file
                        AddToTempFile(apiResults, tempFileLocation);
                        allResults = null;
                        apiResults = null;
                    }
                }

                MergeTempFileToFile(localFilePath, tempFileLocation, headers);
            }
            catch (Exception ex)
            {
                // error ocurred
                errorCount++;
                // remove tempfile
                File.Delete(tempFileLocation);
                // try again
                if (errorCount < 10)
                {
                    Thread.Sleep(1000);
                    GetGoogleReport(viewIds, apiQuery, startDate, endDate, tempFileLocation, localFilePath);
                }
            }
            finally
            {
                svc.Dispose();
            }
        }