Opteo / google-ads-api

Google Ads API client library for Node.js
https://opteo.com
MIT License
270 stars 90 forks source link

Negative field in campaignCriteria create operation not applied #442

Closed crazeidea closed 1 year ago

crazeidea commented 1 year ago

Hi, I was implementing a feature where user can create a campaign criteria with keyword. As I was testing my code, I found that the field 'negative' seems not applied to operation correctly.

Here is the code I wrote to execute the operation:

const result = await customer.campaignCriteria.create(
            [
                {
                    campaign: ResourceNames.campaign(customer.credentials.customer_id, campaign.id),
                    type: enums.CriterionType.KEYWORD,
                    keyword: {
                        text: body.keyword,
                        match_type: body.type,
                    },
                    negative: false,
                },
            ],
            {
                response_content_type: 'MUTABLE_RESOURCE',
            }
        );

and also I tried the mutateResources() method with customer class:

const operation: MutateOperation<resources.ICampaignCriterion> = {
            entity: 'campaign_criterion',
            operation: 'create',
            resource: {
                campaign: ResourceNames.campaign(customer.credentials.customer_id, campaign.id),
                type: enums.CriterionType.KEYWORD,
                keyword: {
                    text: body.keyword,
                    match_type: body.type,
                },
                negative: false,
            },
        };

        const result = await customer.mutateResources([operation], { response_content_type: 'MUTABLE_RESOURCE' });

Here is the message I got from onMutationStart hook:

{
  credentials: {
    customer_id: [CUSTOMER_ID],
    login_customer_id: undefined,
    linked_customer_id: undefined
  },
  method: 'CampaignCriterionService.mutateCampaignCriteria',
  mutation: {
    customer_id: [CUSTOMER_ID],
    operations: [
      {
        create: {
          campaign: 'customers/[CUSTOMER_ID]/campaigns/[CAMPAIGN_ID]',
          type: 2,
          keyword: { text: 'TEXT_CAMPAIGN_KEYWORD', match_type: 2 },
          negative: false // negative field is set to false
        },
        operation: 'create'
      }
    ],
    response_content_type: 'MUTABLE_RESOURCE'
  },
  isServiceCall: true,
  cancel: [Function: cancel],
  editOptions: [Function: editOptions]
}

And here's the message I got from onMutationError hook:

GoogleAdsError {
  error_code: ErrorCode { criterion_error: 84 },
  message: "The field is not allowed to be set when the negative field is set to true
, for example, we don't allow bids in negative ad group or campaign criteria.",      
  location: ErrorLocation {
    field_path_elements: [
      FieldPathElement { field_name: 'operations', index: 0 },
      FieldPathElement { field_name: 'create' },
      FieldPathElement { field_name: 'keyword' }
    ]
  }
}

I hope I can find solution and by the way, It is a wonderful package and thank you for all the work!

crazeidea commented 1 year ago

I just realized that campaignCriterion with keyword type is not applicable for negative: false and it can be only negative: true. Sorry for this issue.