private void AddDimensionFilters(DfareportingService service,
                                         long profileId, Report report)
        {
            // Query advertiser dimension values for report run dates.
            DimensionValueRequest request = new DimensionValueRequest();

            request.StartDate     = report.Criteria.DateRange.StartDate;
            request.EndDate       = report.Criteria.DateRange.EndDate;
            request.DimensionName = "dfa:advertiser";

            DimensionValueList values =
                service.DimensionValues.Query(request, profileId).Execute();

            if (values.Items.Any())
            {
                // Add a value as a filter to the report criteria.
                report.Criteria.DimensionFilters = new List <DimensionValue>()
                {
                    values.Items[0]
                };
            }

            Console.WriteLine(
                "\nUpdated report criteria (with valid dimension filters):\n{0}",
                JsonConvert.SerializeObject(report.Criteria));
        }
        /// <summary>
        /// Lists the first page of results for a dimension value.
        /// </summary>
        /// <param name="dimensionName">The name of the dimension to retrieve values for.</param>
        /// <param name="userProfileId">The ID number of the DFA user profile to run this request as.</param>
        /// <param name="startDate">Values which existed after this start date will be returned.</param>
        /// <param name="endDate">Values which existed before this end date will be returned.</param>
        /// <param name="maxPageSize">The maximum page size to retrieve.</param>
        /// <returns>The first page of dimension values received.</returns>
        public DimensionValueList Query(string dimensionName, long userProfileId, DateTime startDate,
                                        DateTime endDate, int maxPageSize)
        {
            Console.WriteLine("=================================================================");
            Console.WriteLine("Listing available {0} values", dimensionName);
            Console.WriteLine("=================================================================");

            // Create a dimension value query which selects available dimension values.
            var request = new DimensionValueRequest();

            request.DimensionName = dimensionName;
            request.StartDate     = DfaReportingDateConverterUtil.convert(startDate);
            request.EndDate       = DfaReportingDateConverterUtil.convert(endDate);
            var dimensionQuery = service.DimensionValues.Query(request, userProfileId);

            dimensionQuery.MaxResults = maxPageSize;

            // Retrieve values and display them.
            var values = dimensionQuery.Execute();

            if (values.Items.Count > 0)
            {
                foreach (var dimensionValue in values.Items)
                {
                    Console.WriteLine("{0} with value \"{1}\" was found.", dimensionName, dimensionValue.Value);
                }
            }
            else
            {
                Console.WriteLine("No values found.");
            }
            Console.WriteLine();
            return(values);
        }
        /// <summary>
        /// Lists the first page of results for a dimension value.
        /// </summary>
        /// <param name="dimensionName">The name of the dimension to retrieve values for.</param>
        /// <param name="userProfileId">
        /// The ID number of the DFA user profile to run this request as.
        /// </param>
        /// <param name="startDate">
        /// Values which existed after this start date will be returned.
        /// </param>
        /// <param name="endDate">
        /// Values which existed before this end date will be returned.
        /// </param>
        /// <param name="maxPageSize">The maximum page size to retrieve.</param>
        /// <returns>The first page of dimension values received.</returns>
        public DimensionValueList Query(string dimensionName, long userProfileId,
            DateTime startDate, DateTime endDate, int maxPageSize)
        {
            Console.WriteLine("=================================================================");
            Console.WriteLine("Listing available {0} values", dimensionName);
            Console.WriteLine("=================================================================");

            // Create a dimension value query which selects available dimension values.
            var request = new DimensionValueRequest();
            request.DimensionName = dimensionName;
            request.StartDate = DfaReportingDateConverterUtil.convert(startDate);
            request.EndDate = DfaReportingDateConverterUtil.convert(endDate);
            var dimensionQuery = service.DimensionValues.Query(request, userProfileId);
            dimensionQuery.MaxResults = maxPageSize;

            // Retrieve values and display them.
            var values = dimensionQuery.Execute();

            if (values.Items.Count > 0)
            {
                foreach (var dimensionValue in values.Items)
                {
                    Console.WriteLine("{0} with value \"{1}\" was found.", dimensionName,
                        dimensionValue.Value);
                }
            }
            else
            {
                Console.WriteLine("No values found.");
            }
            Console.WriteLine();
            return values;
        }
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="service">An initialized Dfa Reporting service object
        /// </param>
        public override void Run(DfareportingService service)
        {
            long profileId = long.Parse(_T("INSERT_USER_PROFILE_ID_HERE"));

            // Limit the fields returned.
            String fields = "nextPageToken,items(id,value)";

            // Construct the dimension value request.
            DimensionValueRequest request = new DimensionValueRequest();

            request.DimensionName = "dfa:advertiser";

            // Set the date range from 1 year ago until today.
            request.StartDate =
                DfaReportingDateConverterUtil.convertToDateString(DateTime.Now.AddYears(-1));
            request.EndDate =
                DfaReportingDateConverterUtil.convertToDateString(DateTime.Now);

            DimensionValueList values;
            String             nextPageToken = null;

            do
            {
                // Create and execute the dimension value query request.
                DimensionValuesResource.QueryRequest query =
                    service.DimensionValues.Query(request, profileId);
                query.Fields    = fields;
                query.PageToken = nextPageToken;
                values          = query.Execute();

                foreach (DimensionValue value in values.Items)
                {
                    Console.WriteLine("Dimension value with ID {0} and value \"{1}\" was found.",
                                      value.Id, value.Value);
                }

                // Update the next page token.
                nextPageToken = values.NextPageToken;
            } while (values.Items.Any() && !String.IsNullOrEmpty(nextPageToken));
        }
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="service">An initialized Dfa Reporting service object
        /// </param>
        public override void Run(DfareportingService service)
        {
            long profileId = long.Parse(_T("INSERT_USER_PROFILE_ID_HERE"));

              // Limit the fields returned.
              String fields = "nextPageToken,items(id,value)";

              // Construct the dimension value request.
              DimensionValueRequest request = new DimensionValueRequest();
              request.DimensionName = "dfa:advertiser";

              // Set the date range from 1 year ago until today.
              request.StartDate =
              DfaReportingDateConverterUtil.convertToDateString(DateTime.Now.AddYears(-1));
              request.EndDate =
              DfaReportingDateConverterUtil.convertToDateString(DateTime.Now);

              DimensionValueList values;
              String nextPageToken = null;

              do {
            // Create and execute the dimension value query request.
            DimensionValuesResource.QueryRequest query =
            service.DimensionValues.Query(request, profileId);
            query.Fields = fields;
            query.PageToken = nextPageToken;
            values = query.Execute();

            foreach (DimensionValue value in values.Items) {
              Console.WriteLine("Dimension value with ID {0} and value \"{1}\" was found.",
              value.Id, value.Value);
            }

            // Update the next page token.
            nextPageToken = values.NextPageToken;
              } while (values.Items.Any() && !String.IsNullOrEmpty(nextPageToken));
        }
        /// <summary>
        /// Retrieves list of report dimension values for a list of filters.
        /// Documentation https://developers.google.com/dfareporting/v2.8/reference/dimensionValues/query
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Dfareporting service.</param>
        /// <param name="profileId">The DFA user profile ID.</param>
        /// <param name="body">A valid Dfareporting v2.8 body.</param>
        /// <param name="optional">Optional paramaters.</param>
        /// <returns>DimensionValueListResponse</returns>
        public static DimensionValueList Query(DfareportingService service, string profileId, DimensionValueRequest body, DimensionValuesQueryOptionalParms optional = null)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }
                if (profileId == null)
                {
                    throw new ArgumentNullException(profileId);
                }

                // Building the initial request.
                var request = service.DimensionValues.Query(body, profileId);

                // Applying optional parameters to the request.
                request = (DimensionValuesResource.QueryRequest)SampleHelpers.ApplyOptionalParms(request, optional);

                // Requesting data.
                return(request.Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request DimensionValues.Query failed.", ex);
            }
        }