RE: How to enable AverageCpcMicros on GenerateHistoricalMetrics in V11's KeywordPlanService?

266 views
Skip to first unread message
Message has been deleted

Google Ads API Forum Advisor

unread,
Jul 21, 2022, 2:29:49 AM7/21/22
to bru...@searchsmart.com.au, adwor...@googlegroups.com
Hi Bruce,

Thank you for raising this concern to the Google Ads API team. Informing you that I had to delete your initial post as it contained your logs.

Reiterating your post without the logs:

"I am making a request for historical data using the GenerateHistoricalMetrics method on the Services.V11.KeywordPlanService service. In the results that are returned the HasAverageCpcMicros field is false and therefore there is nothing but zero in AverageCpcMicros. What do I have to add to my KeywordPlan to enable this value?

For reference, my sources are publicly available on github, and a full trace log and the output JSON are attached.
"

Moving forward to your concern,  I can see that this is mainly about the GenerateHistoricalMetrics method. That being said, upon checking, please note to set this include_average_cpc field in your request.

Regards,

Google Logo
Carmela
Google Ads API Team
 


ref:_00D1U1174p._5004Q2bmEgh:ref

Bruce Axtens

unread,
Jul 21, 2022, 5:21:09 AM7/21/22
to Google Ads API and AdWords API Forum
Hello Carmela

Yes, I am aware of the setting you describe. I have it in code already as 
            request.HistoricalMetricsOptions = new HistoricalMetricsOptions()
            {
                IncludeAverageCpc = true
            };
However, this targets KeywordPlanIdeaService via
            KeywordPlanIdeaServiceClient keywordPlanIdeaService =
                client.GetService(Services.V11.KeywordPlanIdeaService);

And this is NOT what I'm asking about. I am asking about the KeywordPlanService via
            KeywordPlanServiceClient kpServiceClient = client.GetService(Services.V11.KeywordPlanService);

So what I want is the Cpc in the historical results derived from the GenerateHistoricalMetrics() call on KeywordPlanService not on the KeywordPlanIdeaService.

Kind regards,
Bruce.

Google Ads API Forum Advisor

unread,
Jul 21, 2022, 6:59:00 AM7/21/22
to bru...@searchsmart.com.au, adwor...@googlegroups.com
Hi Bruce,

Thank you for the clarification.

Moving forward, could you confirm if you have considered checking this documentation? I asked this because this document has the steps on how to generate historical metrics using the Google Ads API.

Bruce Axtens

unread,
Jul 21, 2022, 10:21:00 PM7/21/22
to Google Ads API Forum Advisor, adwor...@googlegroups.com, Steve Poulson

Hello Carmela

Yes, I have checked that documentation link. I have adapted that code below for our workflow. Please demonstrate to me where I have gone wrong. I cannot see where I can enable the AverageCpcMicros value.

Kind regards,

Bruce.

Me

You will see Me occasionally and that is implement as:

        private static string Me => new StackTrace().GetFrame(1).GetMethod().Name;

I do understand the performance penalties involved with using it.

CreateKeywordPlan

This routine is expressed as

        public static (string plan, GoogleAdsException exception) CreateKeywordPlan(

            GoogleAdsClient client,

            string customerId,

            string keywordPlanForecastInterval,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            // Get the KeywordPlanService.

            KeywordPlanServiceClient serviceClient = client.GetService(Services.V11.KeywordPlanService);

 

            // Create a keyword plan for next quarter forecast.

            KeywordPlan keywordPlan = new KeywordPlan()

            {

                Name = $"Keyword plan {Me}_{DateTime.UtcNow.ToString($"yyyy'-'MMM'-'dd' 'HH'-'mm'-'ss'-'ffffff")}",

                ForecastPeriod = new KeywordPlanForecastPeriod()

                {

                    DateInterval = (KeywordPlanForecastInterval)Enum.Parse(typeof(KeywordPlanForecastInterval), keywordPlanForecastInterval),

                }

            };

 

            KeywordPlanOperation operation = new KeywordPlanOperation()

            {

                Create = keywordPlan

            };

 

            // Add the keyword plan.

            MutateKeywordPlansResponse response;

            try

            {

                response = serviceClient.MutateKeywordPlans(

                   customerId, new KeywordPlanOperation[] { operation });

            }

            catch (GoogleAdsException e)

            {

                return (null, e);

            }

            // Display the results.

            String planResource = response.Results[0].ResourceName;

            return (planResource, null);

        }

CreateKeywordPlanCampaign

To implement this I have created two helper methods, New_KeywordPlanCampaign and New_KeywordPlanGeoTarget

        public static (KeywordPlanCampaign campaign, GoogleAdsException exception) New_KeywordPlanCampaign(

            string name,

            string cpcBidMicros,

            string keywordPlanNetwork,

            string keywordPlan,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            return (new KeywordPlanCampaign()

            {

                Name = name,

                CpcBidMicros = long.Parse(cpcBidMicros),

                KeywordPlanNetwork = (KeywordPlanNetwork)Enum.Parse(typeof(KeywordPlanNetwork), keywordPlanNetwork),

                KeywordPlan = keywordPlan

            }, null);

        }

 

        public static (KeywordPlanGeoTarget[] geotarget, GoogleAdsException exception) New_KeywordPlanGeoTarget(

            string geoId,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            var result = new List<KeywordPlanGeoTarget>();

            var ids = geoId.Split(',');

            foreach (var id in from id in ids select long.Parse(id))

            {

                result.Add(new KeywordPlanGeoTarget() { GeoTargetConstant = ResourceNames.GeoTargetConstant(id) });

            }

            return (result.ToArray(), null);

        }

 

        public static (string CampaignPlan, GoogleAdsException exception) CreateKeywordPlanCampaign(

            GoogleAdsClient client,

            string customerId,

            KeywordPlanCampaign campaign,

            KeywordPlanGeoTarget[] keywordPlanGeoTarget,

            string langId,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            // Get the KeywordPlanCampaignService.

            KeywordPlanCampaignServiceClient serviceClient = client.GetService(

                Services.V11.KeywordPlanCampaignService);

            campaign.GeoTargets.AddRange(keywordPlanGeoTarget);

            campaign.LanguageConstants.Add(ResourceNames.LanguageConstant(long.Parse(langId)));

            KeywordPlanCampaignOperation operation = new KeywordPlanCampaignOperation()

            {

                Create = campaign

            };

 

            // Add the campaign.

            MutateKeywordPlanCampaignsResponse response =

                serviceClient.MutateKeywordPlanCampaigns(customerId,

                    new KeywordPlanCampaignOperation[] { operation });

 

            // Display the result.

            String planCampaignResource = response.Results[0].ResourceName;

            //Console.WriteLine($"Created campaign for keyword plan: {planCampaignResource}.");

            return (planCampaignResource, null);

        }

CreateKeywordPlanAdGroup

To implement this, I have created a helper method New_KeywordPlanAdGroup

        public static (KeywordPlanAdGroup adGroup, GoogleAdsException exception) New_KeywordPlanAdGroup(

            string campaignResource,

            string name,

            string cpcBidMicros,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            return (new KeywordPlanAdGroup()

            {

                KeywordPlanCampaign = campaignResource,

                Name = name,

                CpcBidMicros = long.Parse(cpcBidMicros)

            }, null);

        }

 

        public static (string planAdGroup, GoogleAdsException exception) CreateKeywordPlanAdGroup(

            GoogleAdsClient client,

            string customerId,

            KeywordPlanAdGroup keywordPlanAdGroup,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            // Get the KeywordPlanAdGroupService.

            KeywordPlanAdGroupServiceClient serviceClient = client.GetService(

                Services.V11.KeywordPlanAdGroupService);

 

            // Create the keyword plan ad group.

            KeywordPlanAdGroup adGroup = keywordPlanAdGroup;

 

            KeywordPlanAdGroupOperation operation = new KeywordPlanAdGroupOperation()

            {

                Create = adGroup

            };

 

            // Add the ad group.

            MutateKeywordPlanAdGroupsResponse response =

                serviceClient.MutateKeywordPlanAdGroups(

                    customerId, new KeywordPlanAdGroupOperation[] { operation });

 

            // Display the result.

            String planAdGroupResource = response.Results[0].ResourceName;

            //Console.WriteLine($"Created ad group for keyword plan: {planAdGroupResource}.");

            return (planAdGroupResource, null);

        }

CreateKeywordPlanAdGroupKeywords

To implement this I have created two helper methods, New_KeywordPlanAdGroupKeyword and New_KeywordPlanCampaignKeyword

public static (KeywordPlanAdGroupKeyword adGroupKeyword, GoogleAdsException exception) New_KeywordPlanAdGroupKeyword(string plan,

            string cpcBigMicros,

            string keywordMatchType,

            string text,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            return (new KeywordPlanAdGroupKeyword()

            {

                KeywordPlanAdGroup = plan,

                CpcBidMicros = long.Parse(cpcBigMicros),

                MatchType = (KeywordMatchType)Enum.Parse(typeof(KeywordMatchType), keywordMatchType),

                // keywordMatchType,

                Text = text

            }, null);

        }

 

        public static (KeywordPlanCampaignKeyword keywordPlanCampaignKeyword, GoogleAdsException exception) New_KeywordPlanCampaignKeyword(

            string planCampaignResource,

            string keywordMatchType,

            string text,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            return (new KeywordPlanCampaignKeyword()

            {

                KeywordPlanCampaign = planCampaignResource,

                MatchType = (KeywordMatchType)Enum.Parse(typeof(KeywordMatchType), keywordMatchType),

                Text = text,

                Negative = true

            }, null);

        }

 

        public static (MutateKeywordPlanAdGroupKeywordResult[] resultArray, GoogleAdsException exception) CreateKeywordPlanAdGroupKeywords(

            GoogleAdsClient client,

            string customerId,

            object[] keywordPlanAdGroupKeywords,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            // Get the KeywordPlanAdGroupKeywordService.

            KeywordPlanAdGroupKeywordServiceClient serviceClient = client.GetService(

                Services.V11.KeywordPlanAdGroupKeywordService);

 

            KeywordPlanAdGroupKeyword[] kpAdGroupKeywords = (from kwd in keywordPlanAdGroupKeywords select kwd as KeywordPlanAdGroupKeyword).ToArray();

 

            // Create an operation for each plan keyword.

            List<KeywordPlanAdGroupKeywordOperation> operations =

                new List<KeywordPlanAdGroupKeywordOperation>();

 

            foreach (KeywordPlanAdGroupKeyword kpAdGroupKeyword in kpAdGroupKeywords)

            {

                operations.Add(new KeywordPlanAdGroupKeywordOperation

                {

                    Create = kpAdGroupKeyword

                });

            }

 

            // Add the keywords.

            MutateKeywordPlanAdGroupKeywordsResponse response;

            try

            {

                response = serviceClient.MutateKeywordPlanAdGroupKeywords(customerId, operations);

            }

            catch (GoogleAdsException ge)

            {

                return (null, ge);

            }

            MutateKeywordPlanAdGroupKeywordResult[] mutateKeywordPlanAdGroupKeywordResults = (from result in response.Results select result).ToArray();

 

            return (mutateKeywordPlanAdGroupKeywordResults, null);

        }

CreateKeywordPlanCampaignNegativeKeywords

This utilises one of the helper functions described earlier

        public static (MutateKeywordPlanCampaignKeywordResult resultArray, GoogleAdsException exception) CreateKeywordPlanCampaignNegativeKeywords(

            GoogleAdsClient client,

            string customerId,

            KeywordPlanCampaignKeyword negativeKeyword,

            bool debug = false)

        {

            if (debug) Debugger.Launch();

            // Get the KeywordPlanCampaignKeywordService.

            KeywordPlanCampaignKeywordServiceClient service = client.GetService(

                Services.V11.KeywordPlanCampaignKeywordService);

 

            // Create the campaign negative keyword for the keyword plan.

            KeywordPlanCampaignKeyword kpCampaignNegativeKeyword = negativeKeyword as KeywordPlanCampaignKeyword;

 

            KeywordPlanCampaignKeywordOperation operation = new KeywordPlanCampaignKeywordOperation

            {

                Create = kpCampaignNegativeKeyword

            };

 

            // Add the campaign negative keyword.

            MutateKeywordPlanCampaignKeywordsResponse response =

                service.MutateKeywordPlanCampaignKeywords(customerId,

                    new KeywordPlanCampaignKeywordOperation[] { operation });

 

            // Display the result.

            MutateKeywordPlanCampaignKeywordResult result = response.Results[0];

            return (result, null);

        }

GenerateHistoricalMetrics

Finally, I have created this method to generate the historical metrics data

        public static (GenerateHistoricalMetricsResponse response, GoogleAdsException exception) GenerateHistoricalMetrics(GoogleAdsClient client, string plan)

        {

            KeywordPlanServiceClient kpServiceClient = client.GetService(Services.V11.KeywordPlanService);

           

            try

            {

                var response = kpServiceClient.GenerateHistoricalMetrics(plan);

                return (response, null);

            }

            catch (GoogleAdsException e)

            {

                return (null, e);

Google Ads API Forum Advisor

unread,
Jul 22, 2022, 1:16:51 AM7/22/22
to br...@searchsmart.com.au, st...@searchsmart.com.au, adwor...@googlegroups.com
Hi Bruce,

Thank you for providing more details to your concern. I am also a member of the Google Ads API team and let me provide support to your concern.

If you want to include the average_cpc_micros in response, then as initially mentioned by my colleague (Carmela), you will need to set the include_average_cpc as true in the KeywordPlanService.GenerateHistoricalMetrics request. Please note that the historical_metrics_options is an object of the KeywordPlanService.GenerateHistoricalMetrics not just by the keywords ideas. This is the configuration that is missing in your request so the average cpc is missing as well in the response.

Unfortunately, there is no sample code that illustrate setting of this field in KeywordPlanService.GenerateHistoricalMetrics. With this, I would suggest reaching out to the author of the .Net client library via this link to know how you can implement the said field in the KeywordPlanService.GenerateHistoricalMetrics. They are more equipped to provide support on code related concerns.

Regards,
Google Logo
Ernie John
Google Ads API Team
 


ref:_00D1U1174p._5004Q2bmEgh:ref

Bruce Axtens

unread,
Jul 22, 2022, 4:23:22 AM7/22/22
to Google Ads API Forum Advisor, Steve Poulson, adwor...@googlegroups.com

Hello Ernie

 

Thanks for the help. With what you suggested I was able to work out the code below. Sadly, it doesn’t do anything more than before. However, the associate documentation states that AverageCpc is deprecated.

 

Oh well.

 

Kind regards,

Bruce.

 

        public static (GenerateHistoricalMetricsResponse response, GoogleAdsException exception) GenerateHistoricalMetrics(GoogleAdsClient client, string plan)

        {

            KeywordPlanServiceClient kpServiceClient = client.GetService(Services.V11.KeywordPlanService);

 

            GenerateHistoricalMetricsRequest generateHistoricalMetricsRequest = new GenerateHistoricalMetricsRequest()

            {

                KeywordPlan = plan,

                HistoricalMetricsOptions = new Google.Ads.GoogleAds.V11.Common.HistoricalMetricsOptions() { IncludeAverageCpc = true },

                AggregateMetrics = new Google.Ads.GoogleAds.V11.Common.KeywordPlanAggregateMetrics()

            };

            try

            {

                var response = kpServiceClient.GenerateHistoricalMetrics(generateHistoricalMetricsRequest);

                return (response, null);

            }

            catch (GoogleAdsException e)

            {

                return (null, e);

            }

 

        }

 

 

From: Google Ads API Forum Advisor <ads...@forumsupport.google>

Sent: Friday, 22 July 2022 1:17 PM
To: Bruce Axtens <br...@searchsmart.com.au>

Reply all
Reply to author
Forward
0 new messages