private static List <AnalyticsReportResponse> MakeReport(GetReportsResponse response)
        {
            var reportResponse = new List <AnalyticsReportResponse>();

            foreach (var report in response.Reports)
            {
                var          rows             = report.Data.Rows;
                ColumnHeader header           = report.ColumnHeader;
                var          dimensionHeaders = header.Dimensions;
                var          metricHeaders    = header.MetricHeader.MetricHeaderEntries;
                if (rows == null || !rows.Any())
                {
                    return(reportResponse);
                }
                else
                {
                    foreach (var row in rows)
                    {
                        var dimensions = row.Dimensions;
                        var metrics    = row.Metrics;

                        reportResponse.Add(new AnalyticsReportResponse(dimensions, metrics));
                    }
                }
            }
            return(reportResponse);
        }
Esempio n. 2
0
        public static void WriteResponseToFile(GetReportsResponse response, string[] optoins)
        {
            if (!Directory.Exists(optoins[0]))
            {
                Directory.CreateDirectory(optoins[0]);
            }

            var file = $"{optoins[0]}\\GoogleAnalyticsExport{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}.csv";

            Console.WriteLine($"File writting to {file}");
            using (var writer = System.IO.File.AppendText(file))
            {
                foreach (var responseReport in response.Reports)
                {
                    foreach (var reportRow in responseReport.Data.Rows)
                    {
                        var dimensions = reportRow.Dimensions.Aggregate((a, y) => a + ", " + y);
                        foreach (var reportRowMetric in reportRow.Metrics)
                        {
                            writer.WriteLine(
                                $"{dimensions}, {reportRowMetric.Values.Aggregate((a, y) => a + ", " + y)}");
                        }
                    }
                }
            }
        }
        public void SaveReportToDisk(GetReportsResponse reportsResponse, string viewId)
        {
            try
            {
                if (reportsResponse != null && reportsResponse.Reports.Any(report => report.Data.Rows != null))
                {
                    Logger.Info("Generating extract file...");

                    var outputDirectory = ConfigurationManager.AppSettings["OutputDirectory"];
                    Directory.CreateDirectory(outputDirectory); //Create directory if it doesn't exist

                    var reportName = GetReportNameByMetric(reportsResponse.Reports[0].ColumnHeader.MetricHeader.MetricHeaderEntries[0].Name);
                    var fileName   = string.Format("{0}_{1}_{2}.json", reportName, viewId.Trim(), DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture));

                    var outputList = new List <object>();
                    foreach (var row in reportsResponse.Reports.SelectMany(r => r.Data.Rows))
                    {
                        outputList.Add(new { Dimensions = row.Dimensions, Metrics = row.Metrics.SelectMany(m => m.Values) });
                    }

                    File.WriteAllText(string.Format(@"{0}\{1}", outputDirectory, fileName), JsonConvert.SerializeObject(outputList));
                    Logger.Info("Finished geneating extract file...");
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error in geneating extract file: " + ex);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Get a collection of document series pages whit their numebr of views
        /// </summary>
        /// <param name="startDate">the date whence the count starts </param>
        /// <param name="viewId">VIEW_ID specified in the Analytics console</param>
        /// <returns>A dictionary containing how many views every document series had</returns>
        public List <KeyValuePair <int, int> > GetSeriesViews(string startDate,
                                                              string viewId)
        {
            int        i, documentId;
            UriBuilder uriBuilder;
            string     queryString;
            var        dimensions = new List <Dimension> {
                new Dimension {
                    Name = "ga:pagePath"
                }
            };
            var metrics = new List <Metric> {
                new Metric {
                    Expression = "ga:pageviews"
                }
            };

            DateRange dateRange = new DateRange
            {
                StartDate = startDate,
                EndDate   = DateTime.Now.ToString("yyyy-MM-dd")
            };

            List <DateRange> dateRanges = new List <DateRange> {
                dateRange
            };
            GetReportsResponse response = SendReportRequest(dateRanges, dimensions, metrics, viewId);
            List <KeyValuePair <int, int> > _pageVisitCount = new List <KeyValuePair <int, int> >();

            KeyValuePair <int, int> tmpModel;
            List <ReportRow>        rows;

            foreach (var report in response.Reports)
            {
                rows = (List <ReportRow>)report.Data.Rows;
                foreach (ReportRow row in rows)
                {
                    for (i = 0; i < row.Metrics.Count(); i++)
                    {
                        if (row.Dimensions[i].Contains("/Series.aspx?idSeries"))
                        {
                            uriBuilder  = new UriBuilder(HttpContext.Current.Request.Url.Host, row.Dimensions[i]);
                            queryString = uriBuilder.Uri.Query.Substring(0, uriBuilder.Uri.Query.LastIndexOf('/'));
                            var queryDictionary = HttpUtility.ParseQueryString(queryString);
                            if (queryDictionary.GetValues("idSeries") != null)
                            {
                                bool success = int.TryParse(queryDictionary.GetValues("idSeries")[0], out documentId);
                                if (success)
                                {
                                    tmpModel = new KeyValuePair <int, int>(documentId, int.Parse(row.Metrics[i].Values[0]));
                                    _pageVisitCount.Add(tmpModel);
                                }
                            }
                        }
                    }
                }
            }
            return(_pageVisitCount);
        }
Esempio n. 5
0
        public async Task GetReportsTest()
        {
            OpenvasApi         api     = new OpenvasApi(Address);
            GetReportsResponse reports = await api.GetReports(new GetReportsRequest());// {ReportId = "4e1adb68-a2f7-409a-8393-926e9c8c7821" });

            Assert.IsNotNull(reports.Results);
            string j = JsonConvert.SerializeObject(reports);
        }
        public IActionResult GetAnalyticsData(string startDate = null, string endDate = null)
        {
            string email = "*****@*****.**";

            AnalyticsReportingService service = AnalyticsHelper.AuthenticateServiceAccount(email);
            // Create the DateRange object.
            DateRange dateRange = new DateRange()
            {
                StartDate = (string.IsNullOrEmpty(startDate))?
                            DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd") : startDate,
                EndDate = (string.IsNullOrEmpty(endDate)? DateTime.Now.ToString("yyyy-MM-dd") : endDate)
            };

            // Create the Metrics object.
            //Metric sessions = new Metric { Expression = "ga:sessions", Alias = "Sessions" };
            Metric pageViews = new Metric {
                Expression = "ga:pageviews", Alias = "PageViews"
            };

            //Create the Dimensions object.
            Dimension pagePath = new Dimension {
                Name = "ga:pagePath"
            };

            // Create the ReportRequest object.
            // Create the ReportRequest object.
            ReportRequest reportRequest = new ReportRequest
            {
                ViewId     = "183068054",
                DateRanges = new List <DateRange>()
                {
                    dateRange
                },
                Dimensions = new List <Dimension>()
                {
                    pagePath
                },
                Metrics = new List <Metric>()
                {
                    pageViews
                }
            };

            List <ReportRequest> requests = new List <ReportRequest>();

            requests.Add(reportRequest);

            // Create the GetReportsRequest object.
            GetReportsRequest getReport = new GetReportsRequest()
            {
                ReportRequests = requests
            };

            // Call the batchGet method.
            GetReportsResponse response = service.Reports.BatchGet(getReport).Execute();

            return(Ok(response));
        }
 public void GenerateReport()
 {
     try
     {
         //code that gets all the data. This all works in a console app I made. My challenge is making it work in MVC.
         var response = batchRequest.Execute();
         googleRpt = response;
     }
 }
Esempio n. 8
0
        public HttpResponseMessage GetReportByType(byte reportType)
        {
            GetReportsResponse response = reportService.GetReportByType(new GetReportsRequest()
            {
                AccountId  = this.AccountId,
                ReportType = (Reports)reportType
            });

            return(Request.BuildResponse(response));
        }
Esempio n. 9
0
        /// <summary>
        /// Send a generic request based on provided Dimensions and Metrics lists, calculated from configuration startDate
        /// </summary>
        /// <param name="dateRanges">Range of dates for calculate views</param>
        /// <param name="dimensions">See https://developers.google.com/analytics/devguides/reporting/core/dimsmets#cats=session</param>
        /// <param name="metrics">See https://developers.google.com/analytics/devguides/reporting/core/dimsmets#cats=session</param>
        /// <param name="viewId">VIEW_ID specified in the Analytics console</param>
        /// /// <returns>A list of rows filled with response data</returns>
        public List <ReportRow> GetStatisticsByMetrics(List <DateRange> dateRanges,
                                                       List <Dimension> dimensions,
                                                       List <Metric> metrics,
                                                       string viewId)
        {
            GetReportsResponse    response     = SendReportRequest(dateRanges, dimensions, metrics, viewId);
            List <List <string> > responseList = new List <List <string> >();
            Report           report            = response.Reports[0];
            List <ReportRow> rows = (List <ReportRow>)report.Data.Rows;

            return(rows);
        }
Esempio n. 10
0
 public static void WriteResponseToConsole(GetReportsResponse response, string[] optoins)
 {
     foreach (var responseReport in response.Reports)
     {
         foreach (var reportRow in responseReport.Data.Rows)
         {
             var dimensions = reportRow.Dimensions.Aggregate((a, y) => a + ", " + y);
             foreach (var reportRowMetric in reportRow.Metrics)
             {
                 Console.WriteLine($"{dimensions}, {reportRowMetric.Values.Aggregate((a, y) => a + ", " + y)}");
             }
         }
     }
 }
Esempio n. 11
0
        public async void LoadAllReports()
        {
            GetReportsResponse response = null;


            response = await ApiHelper.Instance.GetReportsAsync(_reportsCurrentPageNumber, pageSize : (int)pageSizeMalfunctionsNumericUpDown.Value, searchMachineNameMalfunctionTextBox.Text, notScheduledCheckBox.Checked, scheduledCheckBox.Checked, notFixedCheckBox.Checked, fixedCheckBox.Checked, overdueCheckBox.Checked);


            _reportsPagesCount               = response.Pages;
            _reportsCurrentPageNumber        = response.PageNumber;
            totalPagesMalfunctionsLabel.Text = response.Pages.ToString();
            pageNumberMalfunctionsLabel.Text = response.PageNumber.ToString();
            LoadReportsTable(response.Data);
        }
Esempio n. 12
0
        public async void LoadTechnicianReports()
        {
            GetReportsResponse response = null;


            response = await ApiHelper.Instance.GetReportsAsync(_reportsTechCurrentPageNumber, pageSize : (int)pageSizePlannedFixesNumericUpDown.Value, searchMachineNamePlannedFixesTextBox.Text);


            _reportsTechPagesCount           = response.Pages;
            _reportsTechCurrentPageNumber    = response.PageNumber;
            totalPagesPlannedFixesLabel.Text = response.Pages.ToString();
            pageNumberPlannedFixesLabel.Text = response.PageNumber.ToString();
            LoadTechnicianReportsTable(response.Data);
        }
        private GoogleAnalyticsResult ParseResponse(GetReportsResponse response, DateTime startDate, DateTime endDate)
        {
            var result = new GoogleAnalyticsResult();

            result.StartDate = startDate.ToString();
            result.EndDate   = endDate.ToString();

            foreach (var report in response.Reports)
            {
                ColumnHeader              header           = report.ColumnHeader;
                IList <string>            dimensionHeaders = header.Dimensions;
                IList <MetricHeaderEntry> metricHeaders    = header.MetricHeader.MetricHeaderEntries;
                IList <ReportRow>         rows             = report.Data.Rows;
                if (rows == null)
                {
                    rows = new List <ReportRow>();
                }

                const int MAX_RECORDS = 10;
                int       recordCount;
                if (rows.Count > 10)
                {
                    recordCount = MAX_RECORDS;                  //if needed, this code limits quantity of records up to 10
                }
                else
                {
                    recordCount = rows.Count;
                }

                //recordCount will determine how quantity to save into collection
                for (int a = 0; a < recordCount; ++a)
                {
                    IList <string>          dimensions = rows[a].Dimensions;
                    IList <DateRangeValues> metrics    = rows[a].Metrics;

                    var newRecord = new Dictionary <string, string>();
                    for (int i = 0; i < dimensionHeaders.Count && i < dimensions.Count; ++i)
                    {
                        newRecord.Add(dimensionHeaders[i], dimensions[i]);
                    }
                    for (int v = 0; v < metricHeaders.Count; ++v)
                    {
                        newRecord.Add(metricHeaders[v].Name, metrics[0].Values[v]);
                    }
                    result.Records.Add(newRecord);
                }
            }
            return(result);
        }
Esempio n. 14
0
        private int?GetReportId(Reports reportType)
        {
            GetReportsRequest request = new GetReportsRequest {
                ReportType = reportType, AccountId = this.Identity.ToAccountID()
            };
            GetReportsResponse response = reportService.GetReportByType(request);

            if (response.Report != null)
            {
                return(response.Report.ReportID);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 15
0
        public static AnalyticsReport GetDailySessions(DateTime startDate, DateTime endDate, string site)
        {
            Init();

            DateRange dateRange = new DateRange()
            {
                StartDate = startDate.ToString("yyyy-MM-dd"), EndDate = endDate.ToString("yyyy-MM-dd")
            };
            Metric metric = new Metric()
            {
                Expression = "ga:sessions", Alias = "sessions"
            };
            Dimension dim = new Dimension()
            {
                Name = "ga:date"
            };

            ReportRequest rr = new ReportRequest()
            {
                ViewId     = CachedData.AnalyticsViewId,
                DateRanges = new List <DateRange>()
                {
                    dateRange
                },
                Metrics = new List <Metric>()
                {
                    metric
                },
                Dimensions = new List <Dimension>()
                {
                    dim
                },
                FiltersExpression = "ga:dimension1==" + site
            };

            GetReportsRequest grr = new GetReportsRequest()
            {
                ReportRequests = new List <ReportRequest>()
                {
                    rr
                }
            };
            GetReportsResponse response = service.Reports.BatchGet(grr).Execute();

            return(ConvertResponse(response.Reports[0]));
        }
Esempio n. 16
0
        private static Report GetReport(string sViewId, string startDate, string endDate)
        {
            string[] scopes = new string[] { AnalyticsReportingService.Scope.Analytics };

            var credential_google = GoogleCredential.FromJson(_jsonCertificat).CreateScoped(scopes);

            AnalyticsReportingService analyticsreporting = new AnalyticsReportingService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential_google,
                ApplicationName       = _projectId,
            });

            DateRange dateRange = new DateRange()
            {
                StartDate = startDate, EndDate = endDate
            };

            ReportRequest reportRequest = new ReportRequest
            {
                ViewId     = sViewId,
                DateRanges = new List <DateRange>()
                {
                    dateRange
                },
                Dimensions = _dimensions,
                Metrics    = _metrics
            };

            List <ReportRequest> requests = new List <ReportRequest>
            {
                reportRequest
            };

            // Create the GetReportsRequest object.
            GetReportsRequest getReport = new GetReportsRequest()
            {
                ReportRequests = requests
            };

            // Call the batchGet method.
            GetReportsResponse response = analyticsreporting.Reports.BatchGet(getReport).Execute();

            return(response.Reports[0]);
        }
Esempio n. 17
0
        /// <inheritdoc />
        public int GetTotalPageViews(PageViewsRequest request)
        {
            if (!File.Exists(request.ServiceAccountCredentialFile))
            {
                throw new ArgumentException($"Invalid credential file path {request.ServiceAccountCredentialFile}", nameof(request.ServiceAccountCredentialFile));
            }

            GoogleCredential credential = GoogleCredential
                                          .FromFile(request.ServiceAccountCredentialFile)
                                          .CreateScoped(AnalyticsReportingService.Scope.AnalyticsReadonly);

            BaseClientService.Initializer initializer = new BaseClientService.Initializer
            {
                HttpClientInitializer = credential
            };

            GetReportsResponse response = GetAnalyticsResponse(initializer, request);

            return(response.Reports.FirstOrDefault()?.Data.Totals.Sum(t => int.Parse(t.Values.FirstOrDefault() ?? "0")) ?? 0);
        }
Esempio n. 18
0
        /// <summary>
        /// Requests data each time the period specified by Interval has elapsed.
        /// </summary>
        public async override Task RequestData()
        {
            try {
                GetReportsResponse = AnalyticsReportingService.Reports.BatchGet(GetReportsRequest).Execute();

                List <string> columns = Analytics.v4.GetReportsResponse.GetColumns(GetReportsResponse)[0];
                List <List <Dictionary <string, string> > > values = Analytics.v4.GetReportsResponse.GetValues(GetReportsResponse);

                // Gets the date of the latest dataset in order to only add newer data.
                List <IInfluxSeries> last = await InfluxClient.QueryMultiSeriesAsync(DatabaseName, "SELECT last(*) FROM " + MeasurementName);

                DateTime now    = DateTime.UtcNow;
                DateTime New    = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0);
                DateTime?latest = last.FirstOrDefault()?.Entries[0].Time;

                foreach (List <Dictionary <string, string> > report in values)
                {
                    List <InfluxDatapoint <InfluxValueField> > list = new List <InfluxDatapoint <InfluxValueField> >();
                    foreach (Dictionary <string, string> row in report)
                    {
                        if (latest == null)
                        {
                            list.Add((InfluxDatapoint <InfluxValueField>) typeof(T).GetMethod("CreateInfluxDatapoint").Invoke(null, new object[] { MeasurementName, row }));
                        }
                        else
                        {
                            New = new DateTime(Convert.ToInt32(row["ga:year"]), Convert.ToInt32(row["ga:month"]), Convert.ToInt32(row["ga:day"]), 0, 0, 0);
                            if (New > latest)
                            {
                                list.Add((InfluxDatapoint <InfluxValueField>) typeof(T).GetMethod("CreateInfluxDatapoint").Invoke(null, new object[] { MeasurementName, row }));
                            }
                        }
                    }
                    await WriteToDatabase(list);
                }
            }
            catch (Exception ex) {
                Console.WriteLine("Error trying to request Google Analytics data: " + ex.Message);
            }
        }
Esempio n. 19
0
        static void Main(string[] args)
        {
            AnalyticsReportingService service = AutenticarComServiceAccount(_jsonCredenciaisSA);

            var dateRange = new DateRange()
            {
                StartDate = "2020-12-20",
                EndDate   = "2021-01-20"
            };

            //máximo 5
            var requests = new List <ReportRequest>()
            {
                new TotalClicksWhatsapp(_viewId, dateRange),
                new TotalClicksWhatsapp(_viewId, dateRange, "O BOTICÁRIO"),
                new MaisBuscados(_viewId, dateRange)
            };

            var getReportsRequest = new GetReportsRequest {
                ReportRequests = requests
            };

            GetReportsResponse response = service.Reports.BatchGet(getReportsRequest).Execute();

            foreach (var report in response.Reports)
            {
                Console.WriteLine("------------------------------------------------------------------------------------");

                foreach (var row in report.Data.Rows)
                {
                    string dimensions = row.Dimensions.Aggregate((x, y) => x + ", " + y);

                    foreach (var metric in row.Metrics)
                    {
                        Console.WriteLine($"{dimensions}, {metric.Values.Aggregate((a, y) => a + ", " + y)}");
                    }
                }
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Get the number of views for a specific page
        /// </summary>
        /// <param name="currentUrl">the page of which calculate views numer, this a relative URL is comprhensive of query parameter </param>
        /// <param name="startDate">the date whence the count starts </param>
        /// <param name="viewId">VIEW_ID specified in the Analytics console</param>
        /// <param name="_dimensions">See https://developers.google.com/analytics/devguides/reporting/core/dimsmets#cats=session</param>
        /// <param name="_metrics">See https://developers.google.com/analytics/devguides/reporting/core/dimsmets#cats=session</param>
        /// <returns>A string representing views count</returns>
        public string GetVisitorsCount(string currentUrl,
                                       string startDate,
                                       string viewId,
                                       List <Dimension> dimensions,
                                       List <Metric> metrics)
        {
            int       _pageVisitCount = 0, i;
            DateRange dateRange = new DateRange
            {
                StartDate = startDate,
                EndDate   = DateTime.Now.ToString("yyyy-MM-dd")
            };

            List <DateRange> dateRanges = new List <DateRange> {
                dateRange
            };

            GetReportsResponse response = SendReportRequest(dateRanges, dimensions, metrics, viewId);

            foreach (var report in response.Reports)
            {
                List <ReportRow> rows = (List <ReportRow>)report.Data.Rows;
                foreach (ReportRow row in rows)
                {
                    for (i = 0; i < row.Metrics.Count(); i++)
                    {
                        if (row.Dimensions[i] == currentUrl)
                        {
                            _pageVisitCount += int.Parse(row.Metrics[i].Values[0]);
                        }
                    }
                }
            }

            return(_pageVisitCount.ToString());
        }
        private static int[] GetTotals(List <Metric> requiredMetrics, GetReportsResponse response)
        {
            var metricTotals = new int[requiredMetrics.Count];

            foreach (var report in response.Reports)
            {
                var header        = report.ColumnHeader;
                var metricHeaders = (List <MetricHeaderEntry>)header.MetricHeader.MetricHeaderEntries;
                var rows          = (List <ReportRow>)report.Data.Rows;

                foreach (var metrics in rows.Select(row => (List <DateRangeValues>)row.Metrics))
                {
                    for (var j = 0; j < metrics.Count(); j++)
                    {
                        var values = metrics[j];
                        for (var k = 0; k < values.Values.Count() && k < metricHeaders.Count(); k++)
                        {
                            metricTotals[k] += int.Parse(values.Values[k]);
                        }
                    }
                }
            }
            return(metricTotals);
        }
Esempio n. 22
0
        public IQueryable <ModelGAResult> GenerateData(string type, string START_DATE, string END_DATE)
        {
            string filepath = HttpContext.Current.Server.MapPath("~/App_Data/secret_key.json");

            #region Google Credentials
            GoogleCredential credential;
            using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                string[] scopes           = { AnalyticsReportingService.Scope.AnalyticsReadonly };
                var      googleCredential = GoogleCredential.FromStream(stream);
                credential = googleCredential.CreateScoped(scopes);
            }
            #endregion

            SimulationGAResult resultList = new SimulationGAResult();
            // LOOP
            using (var svc = new AnalyticsReportingService(
                       new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "Google Analytics Reporting API"
            }))
            {
                #region DateRange Report Analytics
                var dateRange = new DateRange
                {
                    StartDate = START_DATE,
                    EndDate   = END_DATE
                };
                #endregion

                #region Metric Collections
                var SessionMetric = new Metric
                {
                    Expression     = "ga:sessions",
                    Alias          = "Sessions",
                    FormattingType = "INTEGER"
                };
                var UserMetric = new Metric
                {
                    Expression     = "ga:users",
                    Alias          = "Users",
                    FormattingType = "INTEGER"
                };
                var BounceRateMetric = new Metric
                {
                    Expression     = "ga:bounceRate",
                    Alias          = "Bounce Rate",
                    FormattingType = "PERCENT"
                };
                var SessionDurationMetric = new Metric
                {
                    Expression     = "ga:sessionDuration",
                    Alias          = "Session Durations",
                    FormattingType = "TIME"
                };
                #endregion

                #region Dimension Result
                Dimension DateDimension = new Dimension {
                    Name = "ga:date"
                };
                Dimension BrowserDimension = new Dimension {
                    Name = "ga:browser"
                };
                #endregion

                #region Metric Use
                Metric usedMetric = new Metric();
                switch (type)
                {
                case "user":
                    usedMetric = UserMetric;
                    break;

                case "session":
                    usedMetric = SessionMetric;
                    break;

                case "bounce":
                    usedMetric = BounceRateMetric;
                    break;

                case "duration":
                    usedMetric = SessionDurationMetric;
                    break;
                }

                var reportRequest = new ReportRequest
                {
                    DateRanges = new List <DateRange> {
                        dateRange
                    },
                    Dimensions = new List <Dimension> {
                        DateDimension
                    },
                    Metrics = new List <Metric> {
                        usedMetric
                    },
                    ViewId = "173657940"
                };
                #endregion

                List <ReportRequest> requests = new List <ReportRequest>();
                requests.Add(reportRequest);

                // Create the GetReportsRequest object.
                GetReportsRequest getReport = new GetReportsRequest()
                {
                    ReportRequests = requests
                };

                // Call the batchGet method.
                GetReportsResponse response = null;
                response = svc.Reports.BatchGet(getReport).Execute();

                foreach (var item in response.Reports.First().Data.Rows)
                {
                    ModelGAResult    mdlres = new ModelGAResult();
                    List <PointType> points = new List <PointType>();
                    for (int index = 0; index < item.Dimensions.Count; index++)
                    {
                        string tmpDate = item.Dimensions[index];
                        string year    = tmpDate.Substring(0, 4);
                        string month   = tmpDate.Substring(4, 2);
                        string date    = tmpDate.Substring(6, 2);
                        mdlres.DIMENSION = year + "-" + month + "-" + date;
                        var MetricValues      = string.Join(",", item.Metrics.First().Values);
                        var MetricValuesArray = MetricValues.Split(',');
                        for (var metricIndex = 0; metricIndex < MetricValuesArray.Length; metricIndex++)
                        {
                            mdlres.VALUE = MetricValuesArray[0];
                        }
                    }
                    resultList.RESULTs.Add(mdlres);
                }
            }
            return(resultList.RESULTs.AsQueryable());
        }
Esempio n. 23
0
        public DataTable GetData(GARequestConfig requestConfig)
        {
            var reportRequest = requestConfig.CreateReportRequest();
            GetReportsRequest requestContainer = new GetReportsRequest
            {
                ReportRequests = new List <ReportRequest> {
                    reportRequest
                }
            };
            DataTable dt = new DataTable();

            foreach (var dimension in reportRequest.Dimensions)
            {
                dt.Columns.Add(dimension.Name);
            }

            foreach (var metric in reportRequest.Metrics)
            {
                dt.Columns.Add(metric.Alias ?? metric.Expression);
            }

            while (requestContainer.ReportRequests.Count > 0)
            {
                GetReportsResponse response = AnalyticsService.Reports.BatchGet(requestContainer).Execute();
                requestContainer.ReportRequests = new List <ReportRequest>();
                foreach (Report report in response.Reports)
                {
                    var dimensionHeaders = report.ColumnHeader.Dimensions;
                    var metricHeaders    = report.ColumnHeader.MetricHeader.MetricHeaderEntries;

                    foreach (ReportRow row in report.Data.Rows)
                    {
                        var dimensionValues = row.Dimensions;
                        var metricValues    = row.Metrics;
                        var dataRow         = dt.NewRow();

                        for (int i = 0; i < dimensionHeaders.Count && i < dimensionValues.Count; i++)
                        {
                            dataRow[dimensionHeaders[i]] = dimensionValues[i];
                        }

                        for (int l = 0; l < metricValues.Count; l++)
                        {
                            DateRangeValues values = metricValues[l];
                            for (int i = 0; i < values.Values.Count && i < metricHeaders.Count; i++)
                            {
                                dataRow[metricHeaders[i].Name] = values.Values[i];
                            }
                        }

                        dt.Rows.Add(dataRow);
                    }

                    if (!String.IsNullOrEmpty(report.NextPageToken))
                    {
                        requestConfig.NextPageToken = report.NextPageToken;
                        requestContainer.ReportRequests.Add(requestConfig.CreateReportRequest());
                    }
                }
            }
            return(dt);
        }
Esempio n. 24
0
        public VisitedPagesReport Build(DateTime startDate, DateTime endDate)
        {
            // Create the DateRange object.
            DateRange dateRange = new DateRange()
            {
                StartDate = ReportDate.GetDateAsString(startDate),
                EndDate   = ReportDate.GetDateAsString(endDate)
            };

            // Create the Metrics object.
            //https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/

            Metric pageViewsMetric = new Metric {
                Expression = "ga:pageviews", Alias = "Page views"
            };
            Metric uniquePageViewsMetric = new Metric {
                Expression = "ga:uniquePageviews", Alias = "Unique page views"
            };
            Metric entrancesMetric = new Metric {
                Expression = "ga:entrances", Alias = "Entrances"
            };

            //Create the Dimensions object.
            Dimension dimensionPage = new Dimension {
                Name = "ga:pagePath"
            };

            OrderBy order = new OrderBy
            {
                FieldName = "ga:pageviews",
                SortOrder = "DESCENDING"
            };

            string nextPageToken = null;

            var reportRows = new List <VisitedPagesReportRow>();

            do
            {
                // Create the ReportRequest object.
                ReportRequest reportRequest = new ReportRequest
                {
                    ViewId     = _GAViewID,
                    DateRanges = new List <DateRange>()
                    {
                        dateRange
                    },
                    Dimensions = new List <Dimension>()
                    {
                        dimensionPage
                    },
                    Metrics = new List <Metric>()
                    {
                        pageViewsMetric, uniquePageViewsMetric, entrancesMetric
                    },
                    OrderBys = new List <OrderBy> {
                        order
                    },
                    PageToken = nextPageToken
                };

                List <ReportRequest> requests = new List <ReportRequest>();
                requests.Add(reportRequest);

                // Create the GetReportsRequest object.
                GetReportsRequest getReport = new GetReportsRequest()
                {
                    ReportRequests = requests
                };

                // Call the batchGet method.
                GetReportsResponse response = _Service.Reports.BatchGet(getReport).Execute();

                var report = response.Reports[0];

                var rows = report.Data.Rows;

                foreach (var row in rows)
                {
                    string url = row.Dimensions[0];

                    string valPageViews     = row.Metrics[0].Values[0];
                    string valUniqPageViews = row.Metrics[0].Values[1];
                    string valEntrances     = row.Metrics[0].Values[2];

                    int pageViews     = int.Parse(valPageViews);
                    int uniqPageViews = int.Parse(valUniqPageViews);
                    int entrances     = int.Parse(valEntrances);

                    var reportRow = new VisitedPagesReportRow(url, pageViews, uniqPageViews, entrances);
                    reportRows.Add(reportRow);
                }                //foreach

                nextPageToken = report.NextPageToken;
                if (nextPageToken == null)
                {
                    break;
                }
            } while (true);

            var result = new VisitedPagesReport(reportRows.ToArray());

            return(result);
        }
        private static List <KeywordResult> GoogleDataFeedToRemoveDuplicatesOrBadWords(GetReportsResponse dataFeed)
        {
            // Rules to perform on transformation
            // 1. Lowercase all keywords
            // 2. Remove duplicates
            // 3. Add the views of the duplicates to the existing keyword so as to keep its true position
            // 4. Remove keywords that are on the blacklist e.g. urls
            // 5. Order by page views in descending order i.e. most popular first
            List <KeywordResult> keywords = new List <KeywordResult>();

            foreach (var item in dataFeed.Reports.First().Data.Rows)
            {
                int matchIndex = -1;
                for (int i = 0; i < keywords.Count; i++)
                {
                    if (keywords[i].Keyword == item.Dimensions.First().ToLower())
                    {
                        matchIndex = i;
                    }
                }
                if (matchIndex > -1)
                {
                    // Match keyword and get page views
                    int matchedKeywordPageViewCount = keywords[matchIndex].PageViews;
                    // Increment page views to include duplicate page views
                    matchedKeywordPageViewCount += Convert.ToInt32(item.Metrics.First().Values.First());
                    // Update page views for keyword
                    keywords[matchIndex].PageViews = matchedKeywordPageViewCount;
                }
                else
                {
                    // Still need to apply rule 4
                    string checkedKeyword = RemoveBlacklistedKeywords(item.Dimensions.First().ToLower());
                    if (checkedKeyword.Length > 0)
                    {
                        keywords.Add(new KeywordResult()
                        {
                            Keyword = checkedKeyword, PageViews = Convert.ToInt32(item.Metrics.First().Values.First()), FeedDate = DateTime.Today
                        });
                    }
                }
            }
            // Rule 5 reorder based on pageviews in descending order
            return(keywords.OrderByDescending(x => x.PageViews).ToList());
        }
        public List <KeywordResult> ReadKeywords()
        {
            var response = new GetReportsResponse();

            try
            {
                var credential = GetCredential().Result;

                using (var svc = new AnalyticsReportingService(
                           new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Google Analytics API Console"
                }))
                {
                    var dateRange = new DateRange
                    {
                        StartDate = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd"),
                        EndDate   = DateTime.Now.ToString("yyyy-MM-dd"),
                    };
                    var pageviews = new Metric
                    {
                        Expression = "ga:pageviews",
                        Alias      = "Pageviews"
                    };
                    var searchKeyword = new Dimension {
                        Name = "ga:searchKeyword"
                    };
                    var orderBy = new OrderBy {
                        FieldName = "ga:pageviews"
                    };

                    var reportRequest = new ReportRequest
                    {
                        DateRanges = new List <DateRange> {
                            dateRange
                        },
                        Dimensions = new List <Dimension> {
                            searchKeyword
                        },
                        Metrics = new List <Metric> {
                            pageviews
                        },
                        PageSize = 10000,
                        OrderBys = new List <OrderBy> {
                            orderBy
                        },
                        ViewId = ConfigurationManager.AppSettings["GoogleAnalyticsProfileId"]
                    };
                    var getReportsRequest = new GetReportsRequest
                    {
                        ReportRequests = new List <ReportRequest> {
                            reportRequest
                        }
                    };
                    var batchRequest = svc.Reports.BatchGet(getReportsRequest);
                    response = batchRequest.Execute();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(GoogleDataFeedToRemoveDuplicatesOrBadWords(response));
        }
        public GCustomReport Get(GCustomReportInitializer initializer)
        {
            var credetials =
                (config.CredentialServiceAccountJsonPath != null) ?
                new GCommonCredentialManager <GServiceAccountFileCredentialReceiver, GServiceAccountFileCredentialInitializer>().Get(new GServiceAccountFileCredentialInitializer(config.CredentialServiceAccountJsonPath, GConstants.Scopes)):
                new GCommonCredentialManager <GRestUserCredentialReceiver, GRestUserCredentialInitializer>().Get(new GRestUserCredentialInitializer(config.CredentialUserAccountJsonPath, GConstants.Scopes));
            DateRange dateRange = new DateRange
            {
                StartDate = initializer.DateStart.ToString(GConstants.DateParamFormat),
                EndDate   = initializer.DateEnd.ToString(GConstants.DateParamFormat)
            };

            // Create the Metrics object.
            //Metric sessions = new Metric { Expression = "ga:sessions", Alias = "Sessions" };

            //Create the Dimensions object.
            //Dimension browser = new Dimension { Name = "ga:browser" };
            string nextPageToken = null;
            List <GetReportsResponse> responseList = new List <GetReportsResponse>();

            // Create the ReportRequest object.
            do
            {
                ReportRequest reportRequest = new ReportRequest
                {
                    ViewId           = initializer.View.Id,
                    PageToken        = nextPageToken,
                    IncludeEmptyRows = true,
                    DateRanges       = new List <DateRange>()
                    {
                        dateRange
                    },
                    Dimensions = initializer.Dimensions.Select(x => new Dimension {
                        Name = x.Name
                    }).ToList(),
                    Metrics = initializer.Metrics.Select(x => new Metric {
                        Expression = x.Name, Alias = x.Name
                    }).ToList()
                };

                List <ReportRequest> requests = new List <ReportRequest>
                {
                    reportRequest
                };

                // Create the GetReportsRequest object.
                GetReportsRequest getReport = new GetReportsRequest()
                {
                    ReportRequests = requests
                };

                // Call the batchGet method.
                AnalyticsReportingService s = new AnalyticsReportingService(new Google.Apis.Services.BaseClientService.Initializer {
                    HttpClientInitializer = credetials.GoogleCredential
                });
                GetReportsResponse response = s.Reports.BatchGet(getReport).Execute();
                responseList.Add(response);
                nextPageToken = response.Reports.FirstOrDefault().NextPageToken;
            }while (nextPageToken != null);

            GCustomReport customReport = new GCustomReport(initializer)
            {
                Rows = new List <CustomReportRow>()
            };

            foreach (var response in responseList)
            {
                var apiReport = response.Reports.FirstOrDefault();
                if (apiReport?.Data?.Rows != null)
                {
                    foreach (var row in apiReport.Data.Rows)
                    {
                        List <CustomReportCell> reportRowCells = new List <CustomReportCell>();
                        if (apiReport?.ColumnHeader?.Dimensions != null)
                        {
                            for (int i = 0; i < apiReport.ColumnHeader.Dimensions.Count; i++)
                            {
                                reportRowCells.Add(new GCustomDimensionValued(initializer.Dimensions.Where(x => x.Name == apiReport.ColumnHeader.Dimensions[i]).FirstOrDefault(), row.Dimensions[i]));
                            }
                        }
                        if (apiReport?.ColumnHeader?.MetricHeader != null)
                        {
                            for (int i = 0; i < apiReport.ColumnHeader.MetricHeader.MetricHeaderEntries.Count; i++)
                            {
                                reportRowCells.Add(new GCustomMetricValued(initializer.Metrics.Where(x => x.Name == apiReport.ColumnHeader.MetricHeader.MetricHeaderEntries[i].Name).FirstOrDefault(), row.Metrics.FirstOrDefault().Values[i]));
                            }
                        }
                        // reportRowCells.Add(new CustomReportCell(initializer.Columns[]))
                        customReport.Rows.Add(new CustomReportRow(reportRowCells));
                    }
                }
            }
            return(customReport);
        }
        public List <KeywordResult> ReadKeywords()
        {
            var response = new GetReportsResponse();
            var serviceAccountCredentialFilePath = Path.GetFullPath("Secret.json");

            try
            {
                if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
                {
                    throw new Exception("Path to the service account credentials file is required.");
                }
                if (!File.Exists(serviceAccountCredentialFilePath))
                {
                    throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
                }
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["GoogleServiceAccount"]))
                {
                    throw new Exception("ServiceAccountEmail is required.");
                }

                // These are the scopes of permissions you need. It is best to request only what you need and not all of them
                string[] scopes = new string[] { AnalyticsReportingService.Scope.Analytics };             // View your Google Analytics data

                // For Json file
                if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
                {
                    GoogleCredential credential;
                    using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                                     .CreateScoped(scopes);
                    }
                    // Create the  Analytics service.
                    using (var svc = new AnalyticsReportingService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "AnalyticsReporting Service account Authentication"
                    }))
                    {
                        var dateRange = new DateRange
                        {
                            StartDate = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd"),
                            EndDate   = DateTime.Now.ToString("yyyy-MM-dd"),
                        };
                        var pageviews = new Metric
                        {
                            Expression = "ga:pageviews",
                            Alias      = "Pageviews"
                        };
                        var searchKeyword = new Dimension {
                            Name = "ga:searchKeyword"
                        };
                        var orderBy = new OrderBy {
                            FieldName = "ga:pageviews"
                        };

                        var reportRequest = new ReportRequest
                        {
                            DateRanges = new List <DateRange> {
                                dateRange
                            },
                            Dimensions = new List <Dimension> {
                                searchKeyword
                            },
                            Metrics = new List <Metric> {
                                pageviews
                            },
                            PageSize = 10000,
                            OrderBys = new List <OrderBy> {
                                orderBy
                            },
                            ViewId = ConfigurationManager.AppSettings["GoogleAnalyticsProfileId"]
                        };
                        var getReportsRequest = new GetReportsRequest
                        {
                            ReportRequests = new List <ReportRequest> {
                                reportRequest
                            }
                        };
                        var batchRequest = svc.Reports.BatchGet(getReportsRequest);
                        response = batchRequest.Execute();
                    }
                }
                else
                {
                    throw new Exception("Unsupported Service account credentials.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Create service account AnalyticsReportingService failed " + ex.Message);
                throw new Exception("CreateServiceAccountAnalyticsReportingFailed ", ex);
            }
            return(GoogleDataFeedToRemoveDuplicatesOrBadWords(response));
        }
        static void Main(string[] args)
        {
            UserCredential userCredential;

            userCredential = GetCredential();

            // Create AnalyticsReportingService API service.
            var service = new AnalyticsReportingService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = userCredential,
                ApplicationName       = config.APPLICATION_NAME,
            });

            // Create the DataRange Object
            DateRange dataRange = new DateRange()
            {
                StartDate = "2018-01-01", EndDate = "2018-03-07"
            };

            //Create the Metrics Object "ga:pageviews,ga:entrances"
            Metric sessions = new Metric {
                Expression = "ga:sessions", Alias = "Session"
            };

            //Crate the Dimensions Object. "ga:date,ga:pagePath,ga:landingPagePath,ga:source"
            Dimension browser = new Dimension {
                Name = "ga:browser"
            };

            //Create the ReportRequest Object
            ReportRequest reportRequest = new ReportRequest
            {
                ViewId     = config.VIEW_ID,
                DateRanges = new List <DateRange>()
                {
                    dataRange
                },
                Dimensions = new List <Dimension>()
                {
                    browser
                },
                Metrics = new List <Metric>()
                {
                    sessions
                }
            };

            List <ReportRequest> requests = new List <ReportRequest>();

            requests.Add(reportRequest);

            //Create the GetReportsRequest object
            GetReportsRequest getReport = new GetReportsRequest()
            {
                ReportRequests = requests
            };

            //Call the batchGet Method
            GetReportsResponse response = service.Reports.BatchGet(getReport).Execute();

            printResults(response.Reports);
            //writeResultsToFile();
        }
        /// <summary>
        /// Get all reports configured in App.config
        /// </summary>
        /// <returns></returns>
        public GetReportsResponse GetReport(string viewId)
        {
            var combinedReportResponse = new GetReportsResponse {
                Reports = new List <GAReport>()
            };

            try
            {
                Logger.Info("Processing View Id: " + viewId);
                var config = ReportConfiguration.GetConfig();
                foreach (var item in config.Reports)
                {
                    if (item is Report report)
                    {
                        var stopwatch = new Stopwatch();
                        Logger.Info("Started fetching report: " + report.Name);
                        // Create the Metrics and dimensions object based on configuration.
                        var metrics = report.Metrics.Split(',').Select(m => new Metric {
                            Expression = m
                        }).ToList();
                        var dimensions = report.Dimensions.Split(',').Select(d => new Dimension {
                            Name = d
                        }).ToList();
                        var reportRequest = new ReportRequest
                        {
                            DateRanges    = GetDateRangeFromConfiguration(config),
                            Metrics       = metrics,
                            Dimensions    = dimensions,
                            ViewId        = viewId.Trim(),
                            SamplingLevel = "LARGE",                                           //https://developers.google.com/analytics/devguides/reporting/core/v4/basics#sampling
                            PageSize      = report.RecordCount > 0? report.RecordCount : 10000 //The Analytics Core Reporting API returns a maximum of 10,000 rows per request, no matter how many you ask for. https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet
                        };
                        //Sorting as per report configuration
                        if (!string.IsNullOrEmpty(report.OrderBy))
                        {
                            reportRequest.OrderBys = report.OrderBy.Split(',').Select(o => new OrderBy {
                                FieldName = o.Split('-')[0], SortOrder = o.Split('-')[1]
                            }).ToList();
                        }
                        stopwatch.Start();
                        var reportsResponse = analyticsReportingServiceInstance.Reports.BatchGet(new GetReportsRequest
                        {
                            ReportRequests = new List <ReportRequest> {
                                reportRequest
                            }
                        }).Execute();

                        if (reportsResponse != null)
                        {
                            ((List <GAReport>)combinedReportResponse.Reports).AddRange(reportsResponse.Reports);
                            while (reportsResponse.Reports[0].NextPageToken != null)
                            {
                                reportRequest.PageToken = reportsResponse.Reports[0].NextPageToken;
                                reportsResponse         = analyticsReportingServiceInstance.Reports.BatchGet(new GetReportsRequest
                                {
                                    ReportRequests = new List <ReportRequest> {
                                        reportRequest
                                    }
                                }).Execute();
                                ((List <GAReport>)combinedReportResponse.Reports).AddRange(reportsResponse.Reports);
                            }
                            stopwatch.Stop();
                        }
                        Logger.Info("Finished fetching report: " + report.Name);
                        Logger.Info(string.Format("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error in fetching reports: " + ex);
            }
            return(combinedReportResponse);
        }