Hi All,

Is there documentation describing if campaign can or cannot have selective 
optimization on multiple conversion actions?
For example, in UI I can assign either single conversion action (CA) or 
conversion action set (CAS).
However, I can read via API only data with single CA:
SELECT campaign.id, campaign.selective_optimization.conversion_actions FROM 
campaign;

For campaigns with CAS this field is empty so there should be another one.
*Please describe how to manage CAS with API and assign it in certain 
campaign selective optimization*.

Currently even single CA assignment via API does not work for me:
List<String> conversionIds = Arrays.asList("526204302", "527630287", 
"527627998");

Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, 11662629026L))
.setSelectiveOptimization(Campaign.SelectiveOptimization.newBuilder()
//.addAllConversionActions(conversionIds)
.addConversionActions("526204302")
.build())
.build();

*Request/response are*:

Request
-------
MethodName: google.ads.googleads.v6.services.CampaignService/MutateCampaigns
Endpoint: googleads.googleapis.com:443
Headers: {developer-token=REDACTED, login-customer-id=6766519764, 
x-goog-api-client=gl-java/1.8.0_201 gapic/ gax/1.60.1 grpc/1.32.2}
Body: customer_id: "3744819405"
operations {
  update {
    resource_name: "customers/3744819405/campaigns/11662629026"
    selective_optimization {
      conversion_actions: "526204302"
    }
  }
  update_mask {
    paths: "resource_name"
    paths: "selective_optimization.conversion_actions"
  }
}


Response
--------
Headers: 
Metadata(content-type=application/grpc,request-id=T0yqXrUCk9uXP5la-gNIVQ,date=Tue,
 
13 Apr 2021 09:18:20 GMT,alt-svc=h3-29=":443"; ma=2592000,h3-T051=":443"; 
ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; 
ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43")
Body: null
Failure message: errors {
  error_code {
    request_error: RESOURCE_NAME_MALFORMED
  }
  message: "Resource name \'526204302\' is malformed: expected 
\'customers/{customer_id}/conversionActions/{conversion_action_id}\'."
  location {
    field_path_elements {
      field_name: "operations"
      index: 0
    }
    field_path_elements {
      field_name: "update"
    }
    field_path_elements {
      field_name: "selective_optimization"
    }
    field_path_elements {
      field_name: "conversion_actions"
    }
  }
}

Status: Status{code=INVALID_ARGUMENT, description=Request contains an 
invalid argument., cause=null}.
Request ID T0yqXrUCk9uXP5la-gNIVQ failed due to GoogleAdsException. 
Underlying errors:
  Error 0: error_code {
  request_error: RESOURCE_NAME_MALFORMED
}
message: "Resource name \'526204302\' is malformed: expected 
\'customers/{customer_id}/conversionActions/{conversion_action_id}\'."
location {
  field_path_elements {
    field_name: "operations"
    index: 0
  }
  field_path_elements {
    field_name: "update"
  }
  field_path_elements {
    field_name: "selective_optimization"
  }
  field_path_elements {
    field_name: "conversion_actions"
  }
}

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
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 adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
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 
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/d54de38e-b4de-4290-954c-e16542312d43n%40googlegroups.com.
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ads.googleads.examples.remarketing;

import static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime;

import com.beust.jcommander.Parameter;
import com.google.ads.googleads.examples.utils.ArgumentNames;
import com.google.ads.googleads.examples.utils.CodeSampleParams;
import com.google.ads.googleads.lib.GoogleAdsClient;
import com.google.ads.googleads.lib.utils.FieldMasks;
import com.google.ads.googleads.v6.common.ManualCpc;
import com.google.ads.googleads.v6.enums.AdvertisingChannelTypeEnum;
import com.google.ads.googleads.v6.enums.CampaignStatusEnum;
import com.google.ads.googleads.v6.enums.ConversionActionCategoryEnum.ConversionActionCategory;
import com.google.ads.googleads.v6.enums.ConversionActionStatusEnum.ConversionActionStatus;
import com.google.ads.googleads.v6.enums.ConversionActionTypeEnum.ConversionActionType;
import com.google.ads.googleads.v6.errors.GoogleAdsError;
import com.google.ads.googleads.v6.errors.GoogleAdsException;
import com.google.ads.googleads.v6.resources.Campaign;
import com.google.ads.googleads.v6.resources.ConversionAction;
import com.google.ads.googleads.v6.resources.ConversionAction.ValueSettings;
import com.google.ads.googleads.v6.services.*;
import com.google.ads.googleads.v6.utils.ResourceNames;
import com.google.api.gax.rpc.ServerStream;
import org.joda.time.DateTime;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/** Adds a conversion action. */
public class AddConversionAction {

  private static class AddConversionActionParams extends CodeSampleParams {

    @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
    private Long customerId;
  }

  public static void main(String[] args) {
    AddConversionActionParams params = new AddConversionActionParams();
    if (!params.parseArguments(args)) {

      // Either pass the required parameters for this example on the command line, or insert them
      // into the code here. See the parameter class definition above for descriptions.
      params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
    }

    GoogleAdsClient googleAdsClient = null;
    try {
      googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
    } catch (FileNotFoundException fnfe) {
      System.err.printf(
          "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
      System.exit(1);
    } catch (IOException ioe) {
      System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
      System.exit(1);
    }

    try {
      new AddConversionAction().runExample(googleAdsClient, params.customerId);
    } catch (GoogleAdsException gae) {
      // GoogleAdsException is the base class for most exceptions thrown by an API request.
      // Instances of this exception have a message and a GoogleAdsFailure that contains a
      // collection of GoogleAdsErrors that indicate the underlying causes of the
      // GoogleAdsException.
      System.err.printf(
          "Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
          gae.getRequestId());
      int i = 0;
      for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
        System.err.printf("  Error %d: %s%n", i++, googleAdsError);
      }
      System.exit(1);
    }
  }

  /**
   * Runs the example.
   *
   * @param googleAdsClient the Google Ads API client.
   * @param customerId the client customer ID.
   * @throws GoogleAdsException if an API request failed with one or more service errors.
   */
  // [START add_conversion_action]
  private void runExample_create(GoogleAdsClient googleAdsClient, long customerId) {

    // Creates a ConversionAction.
    ConversionAction conversionAction =
        ConversionAction.newBuilder()
            .setName("Earth to Mars Cruises Conversion #" + getPrintableDateTime())
            .setCategory(ConversionActionCategory.DEFAULT)
            .setType(ConversionActionType.WEBPAGE)
            .setStatus(ConversionActionStatus.ENABLED)
            .setViewThroughLookbackWindowDays(15L)
            .setValueSettings(
                ValueSettings.newBuilder()
                    .setDefaultValue(23.41)
                    .setAlwaysUseDefaultValue(true)
                    .build())
            .build();

    // Creates the operation.
    ConversionActionOperation operation =
        ConversionActionOperation.newBuilder().setCreate(conversionAction).build();

    try (ConversionActionServiceClient conversionActionServiceClient =
        googleAdsClient.getLatestVersion().createConversionActionServiceClient()) {
      MutateConversionActionsResponse response =
          conversionActionServiceClient.mutateConversionActions(
              Long.toString(customerId), Collections.singletonList(operation));
      System.out.printf("Added %d conversion actions:%n", response.getResultsCount());
      for (MutateConversionActionResult result : response.getResultsList()) {
        System.out.printf(
            "New conversion action added with resource name: '%s'%n", result.getResourceName());
      }
    }
  }
  // [END add_conversion_action]

    private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
        List<String> conversionIds = Arrays.asList("526204302", "527630287", "527627998");

        Campaign campaign =
            Campaign.newBuilder()
                .setResourceName(ResourceNames.campaign(customerId, 11662629026L))
                .setSelectiveOptimization(Campaign.SelectiveOptimization.newBuilder()
                    //.addAllConversionActions(conversionIds)
                    .addConversionActions("526204302")
                    .build())
                .build();

        // Creates the operation.
        CampaignOperation op = CampaignOperation.newBuilder()
            .setUpdate(campaign)
            .setUpdateMask(FieldMasks. allSetFieldsOf(campaign))
            .build();
        try (CampaignServiceClient campaignServiceClient =
                 googleAdsClient.getLatestVersion().createCampaignServiceClient())
        {
            MutateCampaignsResponse response =
                campaignServiceClient.mutateCampaigns(
                    Long.toString(customerId), Collections.singletonList(op));
            System.out.printf("Added %d conversion actions:%n", response.getResultsCount());
            for (MutateCampaignResult result : response.getResultsList()) {
                System.out.printf(
                    "New conversion action added with resource name: '%s'%n", result.getResourceName());
            }
        }
    }


    private void runExample_read(GoogleAdsClient googleAdsClient, long customerId) {
        try (GoogleAdsServiceClient googleAdsServiceClient =
                 googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
            //String query = "SELECT conversion_action.id, conversion_action.name FROM conversion_action ORDER BY conversion_action.id";
            String query = "SELECT campaign.id, campaign.selective_optimization.conversion_actions FROM campaign"; // where campaign.id=11662629026
            // Constructs the SearchGoogleAdsStreamRequest.
            SearchGoogleAdsStreamRequest request =
                SearchGoogleAdsStreamRequest.newBuilder()
                    .setCustomerId(Long.toString(customerId))
                    .setQuery(query)
                    .build();

            // Creates and issues a search Google Ads stream request that will retrieve all campaigns.
            ServerStream<SearchGoogleAdsStreamResponse> stream =
                googleAdsServiceClient.searchStreamCallable().call(request);

            // Iterates through and prints all of the results in the stream response.
            for (SearchGoogleAdsStreamResponse response : stream) {
                for (GoogleAdsRow googleAdsRow : response.getResultsList()) {
                    System.out.printf(
                        "Campaign with ID %d, name '%s' and actions %s was found.%n",
                        //googleAdsRow.getConversionAction().getId(), googleAdsRow.getConversionAction().getName());
                        googleAdsRow.getCampaign().getId(), googleAdsRow.getCampaign().getName(), googleAdsRow.getCampaign().getSelectiveOptimization().getConversionActionsList());
                }
            }
        }
    }

}

Reply via email to