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);
}
}
From: Google Ads API Forum Advisor <[email protected]>
Sent: Thursday, 21 July 2022 6:59 PM
To: Bruce Axtens <[email protected]>
Cc: [email protected]
Subject: Re: How to enable AverageCpcMicros on GenerateHistoricalMetrics in
V11's KeywordPlanService?
Hi Bruce,
Thank you for the clarification.
Moving forward, could you confirm if you have considered checking this
documentation<https://developers.google.com/google-ads/api/docs/keyword-planning/generate-historical-metrics>?
I asked this because this document has the steps on how to generate historical
metrics using the Google Ads API.
Regards,
[Google Logo]
Carmela
Google Ads API Team
[https://google-dev-relations.my.salesforce.com/servlet/servlet.ImageServer?oid=00D1U000001174p&esid=0184Q00001I99hV&from=ext]
ref:_00D1U1174p._5004Q2bmEgh:ref
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/ME3P282MB1729C9D2AAF558F4696C74339B909%40ME3P282MB1729.AUSP282.PROD.OUTLOOK.COM.