Пример #1
0
        // [END add_smart_campaign_1]

        // [START add_smart_campaign_10]
        /// <summary>
        /// Retrieves creative suggestions for a Smart campaign ad.
        ///
        /// Using the SmartCampaignSuggestService to suggest creatives for new
        /// and existing Smart campaigns is highly recommended because it helps
        /// the campaigns achieve optimal performance.
        /// </summary>
        /// <param name="client"></param>
        /// <param name="customerId">The Google Ads customer ID.</param>
        /// <param name="suggestionInfo">a SmartCampaignSuggestionInfo instance
        /// with details about the business being advertised.</param>
        /// <returns>A SmartCampaignAdInfo instance with suggested headlines and
        /// descriptions.</returns>
        private SmartCampaignAdInfo GetAdSuggestions(GoogleAdsClient client,
                                                     long customerId, SmartCampaignSuggestionInfo suggestionInfo)
        {
            SmartCampaignSuggestServiceClient smartCampaignSuggestService =
                client.GetService(Services.V10.SmartCampaignSuggestService);

            SuggestSmartCampaignAdRequest request = new SuggestSmartCampaignAdRequest
            {
                CustomerId = customerId.ToString(),
                // Unlike the SuggestSmartCampaignBudgetOptions method, it's only possible to
                // use suggestion_info to retrieve ad creative suggestions.
                SuggestionInfo = suggestionInfo
            };

            // Issue a request to retrieve ad creative suggestions.
            SuggestSmartCampaignAdResponse response =
                smartCampaignSuggestService.SuggestSmartCampaignAd(request);

            // The SmartCampaignAdInfo object in the response contains a list of up to
            // three headlines and two descriptions. Note that some of the suggestions
            // may have empty strings as text. Before setting these on the ad you should
            // review them and filter out any empty values.
            SmartCampaignAdInfo adSuggestions = response.AdInfo;

            if (adSuggestions != null)
            {
                Console.WriteLine($"The following headlines were suggested:");
                foreach (AdTextAsset headline in adSuggestions.Headlines)
                {
                    Console.WriteLine($"\t{headline.Text}");
                }

                Console.WriteLine($"And the following descriptions were suggested:");
                foreach (AdTextAsset description in adSuggestions.Descriptions)
                {
                    Console.WriteLine($"\t{description.Text}");
                }
            }
            else
            {
                Console.WriteLine("No ad suggestions were found.");
                adSuggestions = new SmartCampaignAdInfo();
            }

            return(adSuggestions);
        }
Пример #2
0
        // [START add_smart_campaign_11]
        /// <summary>
        /// Retrieves KeywordThemeConstants suggestions with the SmartCampaignSuggestService.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID.</param>
        /// <param name="suggestionInfo">The suggestion information.</param>
        /// <returns>The suggestions.</returns>
        private List <KeywordThemeConstant> GetKeywordThemeSuggestions(
            GoogleAdsClient client, long customerId, SmartCampaignSuggestionInfo suggestionInfo)
        {
            SmartCampaignSuggestServiceClient smartCampaignSuggestService =
                client.GetService(Services.V10.SmartCampaignSuggestService);

            SuggestKeywordThemesRequest request = new SuggestKeywordThemesRequest()
            {
                SuggestionInfo = suggestionInfo,
                CustomerId     = customerId.ToString()
            };

            SuggestKeywordThemesResponse response =
                smartCampaignSuggestService.SuggestKeywordThemes(request);

            // Prints some information about the result.
            Console.WriteLine($"Retrieved {response.KeywordThemes.Count} keyword theme " +
                              $"constant suggestions from the SuggestKeywordThemes method.");
            return(response.KeywordThemes.ToList());
        }
Пример #3
0
        // [END add_smart_campaign_9]

        // [START add_smart_campaign_1]
        /// <summary>
        /// Retrieves a suggested budget amount for a new budget.
        /// Using the SmartCampaignSuggestService to determine a daily budget for new and existing
        /// Smart campaigns is highly recommended because it helps the campaigns achieve optimal
        /// performance.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID.</param>
        /// <param name="suggestionInfo"></param>
        /// <returns>A daily budget amount in micros.</returns>
        private long GetBudgetSuggestion(GoogleAdsClient client, long customerId,
                                         SmartCampaignSuggestionInfo suggestionInfo)
        {
            SmartCampaignSuggestServiceClient smartCampaignSuggestServiceClient = client.GetService
                                                                                      (Services.V10.SmartCampaignSuggestService);

            SuggestSmartCampaignBudgetOptionsRequest request =
                new SuggestSmartCampaignBudgetOptionsRequest
            {
                CustomerId = customerId.ToString(),
                // You can retrieve suggestions for an existing campaign by setting the
                // "Campaign" field of the request to the resource name of a campaign and
                // leaving the rest of the request fields below unset:
                // Campaign = "INSERT_CAMPAIGN_RESOURCE_NAME_HERE",

                // Since these suggestions are for a new campaign, we're going to use the
                // SuggestionInfo field instead.
                SuggestionInfo = suggestionInfo,
            };

            LocationInfo locationInfo = new LocationInfo
            {
                // Set the location to the resource name of the given geo target constant.
                GeoTargetConstant = ResourceNames.GeoTargetConstant(GEO_TARGET_CONSTANT)
            };

            // Issue a request to retrieve a budget suggestion.
            SuggestSmartCampaignBudgetOptionsResponse response =
                smartCampaignSuggestServiceClient.SuggestSmartCampaignBudgetOptions(request);

            // Three tiers of options will be returned: "low", "high", and "recommended".
            // Here we will use the "recommended" option. The amount is specified in micros, where
            // one million is equivalent to one currency unit.
            Console.WriteLine($"A daily budget amount of " +
                              $"{response.Recommended.DailyAmountMicros}" +
                              $" was suggested, garnering an estimated minimum of " +
                              $"{response.Recommended.Metrics.MinDailyClicks} clicks and an estimated " +
                              $"maximum of {response.Recommended.Metrics.MaxDailyClicks} clicks per day.");

            return(response.Recommended.DailyAmountMicros);
        }
Пример #4
0
        // [END add_smart_campaign_4]

        // [START add_smart_campaign_8]
        /// <summary>
        /// Creates a list of MutateOperations that create new campaign criteria.
        /// </summary>
        /// <param name="customerId">The Google Ads customer ID.</param>
        /// <param name="keywordThemeInfos">A list of KeywordThemeInfos.</param>
        /// <param name="suggestionInfo">A SmartCampaignSuggestionInfo instance.</param>
        /// <returns>A list of MutateOperations that create new campaign criteria.</returns>
        private IEnumerable <MutateOperation> CreateCampaignCriterionOperations(long customerId,
                                                                                IEnumerable <KeywordThemeInfo> keywordThemeInfos, SmartCampaignSuggestionInfo
                                                                                suggestionInfo)
        {
            List <MutateOperation> mutateOperations = keywordThemeInfos.Select(
                keywordThemeInfo => new MutateOperation
            {
                CampaignCriterionOperation = new CampaignCriterionOperation
                {
                    Create = new CampaignCriterion
                    {
                        // Set the campaign ID to a temporary ID.
                        Campaign = ResourceNames.Campaign(
                            customerId, SMART_CAMPAIGN_TEMPORARY_ID),
                        // Set the keyword theme to each KeywordThemeInfo in turn.
                        KeywordTheme = keywordThemeInfo,
                    }
                }
            }).ToList();

            // Create a location criterion for each location in the suggestion info.
            mutateOperations.AddRange(
                suggestionInfo.LocationList.Locations.Select(
                    locationInfo => new MutateOperation()
            {
                CampaignCriterionOperation = new CampaignCriterionOperation()
                {
                    Create = new CampaignCriterion()
                    {
                        // Set the campaign ID to a temporary ID.
                        Campaign = ResourceNames.Campaign(customerId,
                                                          SMART_CAMPAIGN_TEMPORARY_ID),
                        // Set the location to the given location.
                        Location = locationInfo
                    }
                }
            }).ToList()
                );
            return(mutateOperations);
        }
Пример #5
0
        // [END add_smart_campaign]

        // [START add_smart_campaign_9]
        /// <summary>
        /// Builds a SmartCampaignSuggestionInfo object with business details.
        ///
        /// The details are used by the SmartCampaignSuggestService to suggest a
        /// budget amount as well as creatives for the ad.
        ///
        /// Note that when retrieving ad creative suggestions it's required that the
        /// "final_url", "language_code" and "keyword_themes" fields are set on the
        /// SmartCampaignSuggestionInfo instance.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="businessLocationId">The ID of a Business Profile location.</param>
        /// <param name="businessName">The name of a Business Profile.</param>
        /// <returns>A SmartCampaignSuggestionInfo instance .</returns>
        private SmartCampaignSuggestionInfo GetSmartCampaignSuggestionInfo(GoogleAdsClient client,
                                                                           ulong?businessLocationId, string businessName)
        {
            SmartCampaignSuggestServiceClient smartCampaignSuggestServiceClient =
                client.GetService(Services.V10.SmartCampaignSuggestService);

            SmartCampaignSuggestionInfo suggestionInfo = new SmartCampaignSuggestionInfo
            {
                // Add the URL of the campaign's landing page.
                FinalUrl     = LANDING_PAGE_URL,
                LanguageCode = LANGUAGE_CODE,
                // Construct location information using the given geo target constant. It's
                // also possible to provide a geographic proximity using the "proximity"
                // field on suggestion_info, for example:
                // Proximity = new ProximityInfo
                // {
                //     Address = new AddressInfo
                //     {
                //         PostalCode = "INSERT_POSTAL_CODE",
                //         ProvinceCode = "INSERT_PROVINCE_CODE",
                //         CountryCode = "INSERT_COUNTRY_CODE",
                //         ProvinceName = "INSERT_PROVINCE_NAME",
                //         StreetAddress = "INSERT_STREET_ADDRESS",
                //         StreetAddress2 = "INSERT_STREET_ADDRESS_2",
                //         CityName = "INSERT_CITY_NAME"
                //     },
                //     Radius = Double.Parse("INSERT_RADIUS"),
                //     RadiusUnits = ProximityRadiusUnits.Kilometers
                // }
                // For more information on proximities see:
                // https://developers.google.com/google-ads/api/reference/rpc/latest/ProximityInfo
                LocationList = new LocationList()
                {
                    Locations =
                    {
                        new LocationInfo
                        {
                            // Set the location to the resource name of the given geo target
                            // constant.
                            GeoTargetConstant =
                                ResourceNames.GeoTargetConstant(GEO_TARGET_CONSTANT)
                        }
                    }
                }
            };


            // Add the Business Profile location ID if provided.
            if (businessLocationId.HasValue)
            {
                // Transform Google Business Location ID to a compatible format before
                // passing it onto the API.
                suggestionInfo.BusinessLocationId =
                    ConvertBusinessLocationId(businessLocationId.Value);
            }
            else
            {
                suggestionInfo.BusinessContext = new BusinessContext
                {
                    BusinessName = businessName,
                };
            }

            // Add a schedule detailing which days of the week the business is open. This schedule
            // describes a business that is open on Mondays from 9:00 AM to 5:00 PM.
            AdScheduleInfo adScheduleInfo = new AdScheduleInfo
            {
                // Set the day of this schedule as Monday.
                DayOfWeek = DayOfWeekEnum.Types.DayOfWeek.Monday,
                // Set the start hour to 9 AM.
                StartHour = 9,
                // Set the end hour to 5 PM.
                EndHour = 17,
                // Set the start and end minutes to zero.
                StartMinute = MinuteOfHourEnum.Types.MinuteOfHour.Zero,
                EndMinute   = MinuteOfHourEnum.Types.MinuteOfHour.Zero
            };

            suggestionInfo.AdSchedules.Add(adScheduleInfo);

            return(suggestionInfo);
        }
Пример #6
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID.</param>
        /// <param name="keywordText">A keyword string used for generating keyword themes.</param>
        /// <param name="freeFormKeywordText">A keyword used to create a free-form keyword theme.
        /// </param>
        /// <param name="businessLocationId">The ID of a Business Profile location.</param>
        /// <param name="businessName">The name of a Business Profile business.</param>
        public void Run(GoogleAdsClient client, long customerId, string keywordText,
                        string freeFormKeywordText, ulong?businessLocationId, string businessName)
        {
            GoogleAdsServiceClient googleAdsServiceClient =
                client.GetService(Services.V10.GoogleAdsService);

            try
            {
                // [START add_smart_campaign_12]
                // Gets the SmartCampaignSuggestionInfo object which acts as the basis for many
                // of the entities necessary to create a Smart campaign. It will be reused a number
                // of times to retrieve suggestions for keyword themes, budget amount, ad
                //creatives, and campaign criteria.
                SmartCampaignSuggestionInfo suggestionInfo =
                    GetSmartCampaignSuggestionInfo(client, businessLocationId, businessName);

                // Generates a list of keyword themes using the SuggestKeywordThemes method on the
                // SmartCampaignSuggestService. It is strongly recommended that you use this
                // strategy for generating keyword themes.
                List <KeywordThemeConstant> keywordThemeConstants =
                    GetKeywordThemeSuggestions(client, customerId, suggestionInfo);

                // Optionally retrieves auto-complete suggestions for the given keyword text and
                // adds them to the list of keyWordThemeConstants.
                if (keywordText != null)
                {
                    keywordThemeConstants.AddRange(GetKeywordTextAutoCompletions(
                                                       client, keywordText));
                }

                // Converts the KeywordThemeConstants to KeywordThemeInfos.
                List <KeywordThemeInfo> keywordThemeInfos = keywordThemeConstants.Select(
                    constant =>
                    new KeywordThemeInfo {
                    KeywordThemeConstant = constant.ResourceName
                })
                                                            .ToList();

                // Optionally includes any freeform keywords verbatim.
                if (freeFormKeywordText != null)
                {
                    keywordThemeInfos.Add(new KeywordThemeInfo()
                    {
                        FreeFormKeywordTheme = freeFormKeywordText
                    });
                }

                // Includes the keyword suggestions in the overall SuggestionInfo object.
                // [START add_smart_campaign_13]
                suggestionInfo.KeywordThemes.Add(keywordThemeInfos);
                // [END add_smart_campaign_13]
                // [END add_smart_campaign_12]

                SmartCampaignAdInfo adSuggestions = GetAdSuggestions(client, customerId,
                                                                     suggestionInfo);

                long suggestedBudgetAmount = GetBudgetSuggestion(client, customerId,
                                                                 suggestionInfo);

                // [START add_smart_campaign_7]
                // The below methods create and return MutateOperations that we later provide to
                // the GoogleAdsService.Mutate method in order to create the entities in a single
                // request. Since the entities for a Smart campaign are closely tied to one-another
                // it's considered a best practice to create them in a single Mutate request; the
                // entities will either all complete successfully or fail entirely, leaving no
                // orphaned entities. See:
                // https://developers.google.com/google-ads/api/docs/mutating/overview
                MutateOperation campaignBudgetOperation =
                    CreateCampaignBudgetOperation(customerId, suggestedBudgetAmount);
                MutateOperation smartCampaignOperation =
                    CreateSmartCampaignOperation(customerId);
                MutateOperation smartCampaignSettingOperation =
                    CreateSmartCampaignSettingOperation(customerId, businessLocationId,
                                                        businessName);
                IEnumerable <MutateOperation> campaignCriterionOperations =
                    CreateCampaignCriterionOperations(customerId, keywordThemeInfos,
                                                      suggestionInfo);
                MutateOperation adGroupOperation   = CreateAdGroupOperation(customerId);
                MutateOperation adGroupAdOperation = CreateAdGroupAdOperation(customerId,
                                                                              adSuggestions);

                // Send the operations in a single mutate request.
                MutateGoogleAdsRequest mutateGoogleAdsRequest = new MutateGoogleAdsRequest
                {
                    CustomerId = customerId.ToString()
                };
                // It's important to create these entities in this order because they depend on
                // each other, for example the SmartCampaignSetting and ad group depend on the
                // campaign, and the ad group ad depends on the ad group.
                mutateGoogleAdsRequest.MutateOperations.Add(campaignBudgetOperation);
                mutateGoogleAdsRequest.MutateOperations.Add(smartCampaignOperation);
                mutateGoogleAdsRequest.MutateOperations.Add(smartCampaignSettingOperation);
                mutateGoogleAdsRequest.MutateOperations.Add(campaignCriterionOperations);
                mutateGoogleAdsRequest.MutateOperations.Add(adGroupOperation);
                mutateGoogleAdsRequest.MutateOperations.Add(adGroupAdOperation);

                MutateGoogleAdsResponse response =
                    googleAdsServiceClient.Mutate(mutateGoogleAdsRequest);

                PrintResponseDetails(response);
                // [END add_smart_campaign_7]
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }