我正在尝试通过CampaignCriterionService
添加从SearchTerms报告中检索到的特定广告系列的关键字的否定关键字。
我有一个工作代码,可以为广告系列添加否定关键字,很棒。问题来自关键字文本包含"特殊字符" (see here)
错误:
关键字文字包含无效字符或符号..(错误:CriterionError.KEYWORD_HAS_INVALID_CHARS,FieldPath:operations [0] .operand.criterion.text,Trigger:plain mirror 100 * 60)
我从搜索字词报告中提取了关键字ID。因此,我尝试使用下面的
使用KeywordId将关键字添加为否定关键字示例:
using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.Util.Reports.v201802;
using Google.Api.Ads.AdWords.v201802;
[TestMethod]
public void AddSingleNegativeKeywordToCampaign()
{
GoogleAdwords.Model.Campaign campaign = new GoogleAdwords.Model.Campaign(CampaignID);
List<Keyword> keywords = new List<Keyword> { new Keyword { id = 27736878000, matchType = KeywordMatchType.EXACT } };
campaign.AddNegativeKeywordstoCampaign(keywords);
}
public class Campaign : AdwordsBase
{
public long CampaignId { get; set; }
public string CampaignName;
public Campaign(long campaignId) : base()
{
CampaignId = campaignId;
}
public void AddNegativeKeywordstoCampaign(List<Keyword> ListofKeywords)
{
AddKeywordstoCampaign(ListofKeywords, Operator.ADD);
}
private void AddKeywordstoCampaign(List<Keyword> keywords, Operator @operator)
{
using (CampaignCriterionService campaignCriterionService =
(CampaignCriterionService)user.GetService(
AdWordsService.v201802.CampaignCriterionService))
{
List<CampaignCriterionOperation> operations = new List<CampaignCriterionOperation>();
foreach (Keyword keyword in keywords)
{
// Create the biddable ad group criterion.
NegativeCampaignCriterion keywordCriterion = new NegativeCampaignCriterion();
keywordCriterion.campaignId = CampaignId;
keywordCriterion.criterion = keyword;
// Create the operations.
CampaignCriterionOperation operation = new CampaignCriterionOperation();
operation.@operator = @operator;
operation.operand = keywordCriterion;
operations.Add(operation);
}
try
{
// Create the keywords.
CampaignCriterionReturnValue retVal = campaignCriterionService.mutate(
operations.ToArray());
// Display the results.
if (retVal != null && retVal.value != null)
{
foreach (CampaignCriterion campaignCriterion in retVal.value)
{
// If you are adding multiple type of criteria, then you may need to
// check for
//
// if (adGroupCriterion is Keyword) { ... }
//
// to identify the criterion type.
Debug.WriteLine("Keyword with Campaign id = '{0}', keyword id = '{1}', text = " +
"'{2}' and match type = '{3}' was {4}.", campaignCriterion.campaignId,
campaignCriterion.criterion.id, (campaignCriterion.criterion as Keyword).text,
(campaignCriterion.criterion as Keyword).matchType, @operator.ToString());
}
}
else
{
Debug.WriteLine("No keywords were added.");
}
}
catch (Exception e)
{
throw new System.ApplicationException("Failed to create keywords.", e);
}
}
}
}
但是当api报告错误时,这并不起作用:
缺少必填字段..(错误:RequiredError.REQUIRED,FieldPath:operations [0] .operand.criterion.text,Trigger:)
我可以在Adwords用户界面中将搜索字词添加为否定关键字,因此,它必须是可能的。有没有人能够使用最新版本的Adwords API实现这一目标? (v201802)
非常清楚,我认为代码没有任何问题,更多的是关于如何添加属于此职权范围的关键字的问题。