Example #1
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (TargetingDimension != 0)
            {
                hash ^= TargetingDimension.GetHashCode();
            }
            if (bidOnly_ != null)
            {
                hash ^= BidOnly.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
Example #2
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (TargetingDimension != global::Google.Ads.GoogleAds.V5.Enums.TargetingDimensionEnum.Types.TargetingDimension.Unspecified)
            {
                hash ^= TargetingDimension.GetHashCode();
            }
            if (HasBidOnly)
            {
                hash ^= BidOnly.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for the conversion action is
        ///     added.</param>
        /// <param name="adGroupId">The ad group ID for which to update the audience targeting
        ///     restriction.</param>
        public void Run(GoogleAdsClient client, long customerId, long adGroupId)
        {
            // Get the GoogleAdsService client.
            GoogleAdsServiceClient googleAdsServiceClient =
                client.GetService(Services.V10.GoogleAdsService);

            // Create a search request that retrieves the targeting settings from a given ad group.
            // [START update_audience_target_restriction]
            string query = $@"
                SELECT ad_group.id, ad_group.name, ad_group.targeting_setting.target_restrictions
                FROM ad_group
                WHERE ad_group.id = {adGroupId}";

            // [END update_audience_target_restriction]

            try
            {
                // Issue the search request.
                PagedEnumerable <SearchGoogleAdsResponse, GoogleAdsRow> searchResponse =
                    googleAdsServiceClient.Search(customerId.ToString(), query);

                // Create an empty TargetingSetting instance.
                TargetingSetting targetingSetting = new TargetingSetting();

                // Create a flag that specifies whether or not we should update the targeting
                // setting. We should only do this if we find an AUDIENCE target restriction with
                // bid_only set to false.
                bool shouldUpdateTargetingSetting = false;

                // Iterate over all rows in all pages and prints the requested field values for the
                // ad group in each row.
                foreach (GoogleAdsRow googleAdsRow in searchResponse)
                {
                    AdGroup adGroup = googleAdsRow.AdGroup;

                    Console.WriteLine($"Ad group with ID {adGroup.Id} and name '{adGroup.Name}' " +
                                      "was found with the following targeting restrictions:");

                    RepeatedField <TargetRestriction> targetRestrictions =
                        adGroup.TargetingSetting.TargetRestrictions;

                    // Loop through and print each of the target restrictions. Reconstruct the
                    // TargetingSetting object with the updated audience target restriction because
                    // Google will overwrite the entire targeting_setting field of the ad group when
                    // the field mask includes targeting_setting in an update operation.
                    // [START update_audience_target_restriction_1]
                    foreach (TargetRestriction targetRestriction in targetRestrictions)
                    {
                        TargetingDimension targetingDimension =
                            targetRestriction.TargetingDimension;
                        bool bidOnly = targetRestriction.BidOnly;

                        Console.WriteLine("\tTargeting restriction with targeting dimension " +
                                          $"'{targetingDimension}' and bid only set to '{bidOnly}'.");

                        // Add the target restriction to the TargetingSetting object as is if the
                        // targeting dimension has a value other than AUDIENCE because those should
                        // not change.
                        if (targetingDimension != TargetingDimension.Audience)
                        {
                            targetingSetting.TargetRestrictions.Add(targetRestriction);
                        }
                        else if (!bidOnly)
                        {
                            shouldUpdateTargetingSetting = true;

                            // Add an AUDIENCE target restriction with bid_only set to true to the
                            // targeting setting object. This has the effect of setting the AUDIENCE
                            // target restriction to "Observation". For more details about the
                            // targeting setting, visit
                            // https://support.google.com/google-ads/answer/7365594.
                            targetingSetting.TargetRestrictions.Add(new TargetRestriction
                            {
                                TargetingDimension = TargetingDimension.Audience,
                                BidOnly            = true
                            });
                        }
                    }
                    // [END update_audience_target_restriction_1]
                }

                // Only update the TargetSetting on the ad group if there is an AUDIENCE
                // TargetRestriction with bid_only set to false.
                if (shouldUpdateTargetingSetting)
                {
                    UpdateTargetingSetting(client, customerId, adGroupId, targetingSetting);
                }
                else
                {
                    Console.WriteLine("No target restrictions to update.");
                }
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }