How to set multiple location targeting for adwords type SHOPPING?

77 views
Skip to first unread message

Trường Nguyễn Xuân

unread,
Apr 4, 2018, 5:44:49 AM4/4/18
to AdWords API Forum
When I using .net lib libary from github, result error OPERATION_NOT_PERMITTED_FOR_CAMPAIGN_TYPE.
How to fix this problem? 
Thanks so much!.

using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201802;

using System;
using System.Collections.Generic;

namespace Google.Api.Ads.AdWords.Examples.CSharp.v201802
{

    /// <summary>
    /// This code example adds various types of targeting criteria to a campaign.
    /// To get a list of campaigns, run GetCampaigns.cs.
    /// </summary>
    public class AddCampaignTargetingCriteria : ExampleBase
    {

        /// <summary>
        /// Main method, to run this code example as a standalone application.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            AddCampaignTargetingCriteria codeExample = new AddCampaignTargetingCriteria();
            Console.WriteLine(codeExample.Description);
            try
            {
                long campaignId = long.Parse("1341670265");
                string feedIdText = "INSERT_LOCATION_FEED_ID_HERE";

                long? feedId = null;
                long temp;

                if (long.TryParse(feedIdText, out temp))
                {
                    feedId = temp;
                }

                codeExample.Run(new AdWordsUser(), campaignId, feedId);
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred while running this code example. {0}",
                    ExampleUtilities.FormatException(e));
            }
        }

        /// <summary>
        /// Returns a description about the code example.
        /// </summary>
        public override string Description
        {
            get
            {
                return "This code example adds various types of targeting criteria to a campaign. To " +
                    "get a list of campaigns, run GetCampaigns.cs.";
            }
        }

        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign to which targeting criteria
        /// are added.</param>
        /// <param name="feedId">ID of a feed that has been configured for location
        /// targeting, meaning it has an ENABLED FeedMapping with criterionType of
        /// 77. Feeds linked to a GMB account automatically have this FeedMapping.
        /// If you don't have such a feed, set this value to null.</param>
        public void Run(AdWordsUser user, long campaignId, long? feedId)
        {
            using (CampaignCriterionService campaignCriterionService =
                (CampaignCriterionService)user.GetService(
                    AdWordsService.v201802.CampaignCriterionService))
            {

                // Create locations. The IDs can be found in the documentation or
                // retrieved with the LocationCriterionService.
                Location california = new Location()
                {
                    id = 21137L
                };

                Location mexico = new Location()
                {
                    id = 2484L
                };

                // Create languages. The IDs can be found in the documentation or
                // retrieved with the ConstantDataService.
                Language english = new Language()
                {
                    id = 1000L //1040

                };

                Language vietnam = new Language()
                {
                    id = 1040 
                };

                Language spanish = new Language()
                {
                    id = 1003L
                };

                List<Criterion> criteria = new List<Criterion>() {
          california, mexico, english, spanish
        };

                // Distance targeting. Area of 10 miles around the locations in the location feed.
                if (feedId != null)
                {
                    LocationGroups radiusLocationGroup = new LocationGroups()
                    {
                        feedId = feedId.Value,
                        matchingFunction = new Function()
                        {
                            @operator = FunctionOperator.IDENTITY,
                            lhsOperand = new FunctionArgumentOperand[] {
                                new LocationExtensionOperand() {
                                  radius = new ConstantOperand() {
                                    type = ConstantOperandConstantType.DOUBLE,
                                    unit  = ConstantOperandUnit.MILES,
                                    doubleValue = 10
                                  }
                                }
                            }
                        }
                    };

                    criteria.Add(radiusLocationGroup);
                }

                // Create operations to add each of the criteria above.
                List<CampaignCriterionOperation> operations = new List<CampaignCriterionOperation>();
                foreach (Criterion criterion in criteria)
                {
                    CampaignCriterionOperation operation = new CampaignCriterionOperation()
                    {
                        operand = new CampaignCriterion()
                        {
                            campaignId = campaignId,
                            criterion = criterion
                        },
                        @operator = Operator.ADD
                    };

                    operations.Add(operation);
                }

                // Add a negative campaign criterion.

                CampaignCriterion negativeCriterion = new NegativeCampaignCriterion()
                {
                    campaignId = campaignId,
                    criterion = new Keyword()
                    {
                        text = "jupiter cruise",
                        matchType = KeywordMatchType.BROAD
                    }
                };

                CampaignCriterionOperation negativeCriterionOperation = new CampaignCriterionOperation()
                {
                    operand = negativeCriterion,
                    @operator = Operator.ADD
                };

                operations.Add(negativeCriterionOperation);

                try
                {
                    // Set the campaign targets.
                    CampaignCriterionReturnValue retVal = campaignCriterionService.mutate(
                        operations.ToArray());

                    if (retVal != null && retVal.value != null)
                    {
                        // Display campaign targets.
                        foreach (CampaignCriterion criterion in retVal.value)
                        {
                            Console.WriteLine("Campaign criteria of type '{0}' was set to campaign with" +
                                " id = '{1}'.", criterion.criterion.CriterionType, criterion.campaignId);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to set Campaign criteria.", e);
                }
            }
        }
    }
}

Sreelakshmi Sasidharan (AdWords API Team)

unread,
Apr 4, 2018, 1:48:18 PM4/4/18
to AdWords API Forum
Hi,

I see that the campaign (Id : 1341670265) that you are trying to add the criteria is a Shopping Campaign. Please note that Language criterion cannot be targeted at the campaign level for Shopping Campaigns. Please check this guide for details about the criteria usage. Could you retry the request removing the language criterion and see if that works?

Thanks,
Sreelakshmi, AdWords API Team

Trường Nguyễn Xuân

unread,
Apr 4, 2018, 9:47:15 PM4/4/18
to AdWords API Forum
Hi AdWords Team,

Thank you for support, after removing the language, it that works.
Reply all
Reply to author
Forward
0 new messages