"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,
|
||||||
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.
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.
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);
}
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);
}
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);
}
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);
}
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);
}
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);
|
||||||
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>