Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public PublisherRevenueReport 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 publisherImpressionsMetric = new Metric {
                Expression = "ga:totalPublisherImpressions", Alias = "Publisher Impressions"
            };
            Metric publisherClicksMetric = new Metric {
                Expression = "ga:totalPublisherClicks", Alias = "Publisher Clicks"
            };
            Metric publisherRevenueMetric = new Metric {
                Expression = "ga:totalPublisherRevenue", Alias = "Publisher Revenue"
            };

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

            OrderBy order = new OrderBy
            {
                FieldName = "ga:totalPublisherRevenue",                 //ga:totalPublisherImpressions
                SortOrder = "DESCENDING"
            };
            //https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet?hl=ru#SortOrder

            string nextPageToken = null;
            double revenueTotal  = 0;

            var reportRows = new List <RevenueReportRow>();

            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>()
                    {
                        publisherImpressionsMetric, publisherClicksMetric, publisherRevenueMetric
                    },
                    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];
                int rowCount = 0;
                if (report.Data.RowCount != null)
                {
                    rowCount = report.Data.RowCount.Value;
                }

                var rows = report.Data.Rows;

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

                    string valImpressions = row.Metrics[0].Values[0];
                    string valClicks      = row.Metrics[0].Values[1];
                    string valRevenue     = row.Metrics[0].Values[2];

                    var rev = double.Parse(valRevenue, CultureInfo.InvariantCulture);

                    int impressions = int.Parse(valImpressions);
                    int clicks      = int.Parse(valClicks);

                    RevenueReportRow reportRow = new RevenueReportRow(rev, impressions, clicks, url);
                    reportRows.Add(reportRow);

                    revenueTotal += rev;
                }                //foreach

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

            PublisherRevenueReport result = new PublisherRevenueReport(revenueTotal, reportRows.ToArray());

            return(result);
        }