Esempio n. 1
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <param name="artifactName">The artifact for which metadata is retrieved.</param>
        public void Run(GoogleAdsClient client, long customerId, string artifactName)
        {
            // Get the GoogleAdsFieldService.
            GoogleAdsFieldServiceClient googleAdsFieldService = client.GetService(
                Services.V4.GoogleAdsFieldService);

            // Create the query.
            string searchQuery = $"SELECT name, category, selectable, filterable, sortable, " +
                                 $"selectable_with, data_type, is_repeated WHERE name = '{artifactName}'";

            try
            {
                // Search for an artifact whose name is the same as the specified artifactName.
                PagedEnumerable <SearchGoogleAdsFieldsResponse, GoogleAdsField> googleAdsFields =
                    googleAdsFieldService.SearchGoogleAdsFields(searchQuery);

                if (googleAdsFields.Any())
                {
                    // Get all returned artifacts and print out their metadata.
                    foreach (GoogleAdsField googleAdsField in googleAdsFields)
                    {
                        Console.WriteLine("An artifact named '{0}' with category '{1}' and " +
                                          "data type '{2}' {3} selectable, {4} filterable, {5} sortable " +
                                          "and {6} repeated.",
                                          googleAdsField.Name, googleAdsField.Category, googleAdsField.DataType,
                                          getIsOrIsNot(googleAdsField.Selectable),
                                          getIsOrIsNot(googleAdsField.Filterable),
                                          getIsOrIsNot(googleAdsField.Sortable),
                                          getIsOrIsNot(googleAdsField.IsRepeated));

                        List <string> selectableLists = new List <string>(
                            googleAdsField.SelectableWith);
                        selectableLists.Sort();

                        Console.WriteLine("The artifact can be selected with the following " +
                                          "artifacts:");
                        foreach (string item in selectableLists)
                        {
                            Console.WriteLine("- " + item);
                        }
                    }
                }
                else
                {
                    Console.Error.WriteLine($"The specified artifact '{artifactName}' " +
                                            "doesn't exist.");
                }
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Sets the starting and ending date times for the new billing setup. Queries the
        /// customer's account to see if there are any approved billing setups. If there are any,
        /// the new billing setup starting date time is set to one day after the last. If not, the
        /// billing setup is set to start immediately. The ending date is set to one day after the
        /// starting date time.
        /// </summary>
        /// <param name="googleAdsService">The Google Ads service client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <param name="billingSetup">The instance of BillingSetup whose starting date time will
        ///     be set.</param>
        private void SetBillingSetupStartDateTime(GoogleAdsServiceClient googleAdsService,
                                                  long customerId, BillingSetup billingSetup)
        {
            // The query to search existing approved billing setups in the end date time descending
            // order.
            // See GetBillingSetup.cs for a more detailed example of requesting billing setup
            // information.
            string query = @"
                SELECT billing_setup.end_date_time
                FROM billing_setup
                WHERE billing_setup.status = 'APPROVED'
                ORDER BY billing_setup.end_date_time DESC
                LIMIT 1";

            // Issues a search request.
            PagedEnumerable <SearchGoogleAdsResponse, GoogleAdsRow> searchResponse =
                googleAdsService.Search(customerId.ToString(), query);

            if (searchResponse.Any())
            {
                // Retrieves the ending date time of the last billing setup.
                string lastEndingDateTimeString = searchResponse.First().BillingSetup.EndDateTime;

                // A null ending date time indicates that the current billing setup is set to run
                // indefinitely. Billing setups cannot overlap, so throw an exception in this case.
                if (lastEndingDateTimeString == null)
                {
                    throw new Exception("Cannot set starting and ending date times for " +
                                        "the new billing setup; the latest existing billing " +
                                        "setup is set to run indefinitely.");
                }

                DateTime lastEndingDateTime = DateTime.Parse(lastEndingDateTimeString);

                // Sets the new billing setup to start one day after the ending date time.
                billingSetup.StartDateTime = lastEndingDateTime.AddDays(1).ToString("yyyy-MM-dd");

                // Sets the new billing setup to end one day after the starting date time.
                billingSetup.EndDateTime = lastEndingDateTime.AddDays(2).ToString("yyyy-MM-dd");
            }
            else
            {
                // Otherwise, the only acceptable start time is TimeType.Now.
                billingSetup.StartTimeType = TimeType.Now;

                // Sets the new billing setup to end tomorrow.
                billingSetup.EndDateTime = DateTime.Today.AddDays(1).ToString("yyyy-MM-dd");
            }
        }
        /// <summary>
        /// Sets the starting date time for the new billing setup. Queries the customer's account
        /// to see if there are any approved billing setups. If there are any, the new billing setup
        /// may have a starting date time of any day in the future. If not, the billing setup must
        /// be set to start immediately.
        /// </summary>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <param name="googleAdsService">The Google Ads service client.</param>
        /// <param name="billingSetup">The instance of BillingSetup whose starting date time will
        ///     be set.</param>
        private void SetBillingSetupStartDateTime(long customerId, GoogleAdsServiceClient
                                                  googleAdsService, BillingSetup billingSetup)
        {
            // Query to see if there are any existing approved billing setups. See
            // GetBillingSetup.cs for a more detailed example of requesting billing setup
            // information.
            string query = @"
                SELECT billing_setup.end_date_time
                FROM billing_setup
                WHERE billing_setup.status = 'APPROVED'
                ORDER BY billing_setup.end_date_time DESC";

            PagedEnumerable <SearchGoogleAdsResponse, GoogleAdsRow> searchResponse =
                googleAdsService.Search(customerId.ToString(), query);

            if (searchResponse.Any())
            {
                // If there are any existing approved billing setups, the new billing setup can
                // start immediately after the last.
                string lastEndingDateTimeString = searchResponse.First().BillingSetup.EndDateTime;

                // Check if the existing billing setup has no end date (i.e., is set to run
                // indefinitely).
                if (lastEndingDateTimeString == null)
                {
                    throw new Exception("Cannot set ending date time for the new " +
                                        "billing setup; the latest existing billing setup is set " +
                                        "to run indefinitely.");
                }

                DateTime lastEndingDateTime = DateTime.Parse(lastEndingDateTimeString);
                billingSetup.StartDateTime = lastEndingDateTime.AddDays(1).ToString("yyyy-MM-dd");
                billingSetup.EndDateTime   = lastEndingDateTime.AddDays(2).ToString("yyyy-MM-dd");
            }
            else
            {
                // If there are no existing approved billing setups, the only acceptable start time
                // is TimeType.Now.
                billingSetup.StartTimeType = TimeType.Now;
            }
        }