Bắt đầu

Bạn phải bật tính năng theo dõi lượt chuyển đổi trong tài khoản chuyển đổi trên Google Ads để ghi lại lượt chuyển đổi. Hướng dẫn này cung cấp thông tin chi tiết về cách xác nhận xem tính năng theo dõi lượt chuyển đổi đã được bật hay chưa, bật tính năng này nếu chưa bật và truy xuất thông tin về các hành động chuyển đổi hiện có.

Hầu hết các hành động chuyển đổi cũng yêu cầu bạn thực hiện thêm các bước để theo dõi. Để biết thêm thông tin về các loại hành động chuyển đổi và yêu cầu của các loại hành động đó, hãy xem Hướng dẫn tạo hành động chuyển đổi.

Thiết lập trang web để theo dõi lượt chuyển đổi

Nếu bạn đang bắt đầu tích hợp tính năng nhập lượt chuyển đổi ngoại tuyến, bước đầu tiên là làm theo các bước trong hướng dẫn Định cấu hình thẻ Google cho lượt chuyển đổi nâng cao cho khách hàng tiềm năng để định cấu hình trang web của bạn theo dõi lượt chuyển đổi nâng cao cho khách hàng tiềm năng. Bạn cũng có thể sử dụng Trình quản lý thẻ của Google để định cấu hình trang web bằng cách làm theo các bước trong hướng dẫn Thiết lập Trình quản lý thẻ của Google để sử dụng tính năng lượt chuyển đổi nâng cao cho khách hàng tiềm năng.

Bật tính năng theo dõi lượt chuyển đổi trong tài khoản chuyển đổi của bạn trên Google Ads

Truy xuất thông tin về chế độ thiết lập tính năng theo dõi lượt chuyển đổi

Bạn có thể kiểm tra chế độ thiết lập tính năng theo dõi lượt chuyển đổi của tài khoản và xác nhận tính năng theo dõi lượt chuyển đổi đã được bật hay chưa bằng cách truy vấn tài nguyên Customer cho ConversionTrackingSetting. Thực hiện truy vấn sau bằng GoogleAdsService.SearchStream:

SELECT
  customer.conversion_tracking_setting.google_ads_conversion_customer,
  customer.conversion_tracking_setting.conversion_tracking_status,
  customer.conversion_tracking_setting.conversion_tracking_id,
  customer.conversion_tracking_setting.cross_account_conversion_tracking_id
FROM customer

Trường google_ads_conversion_customer cho biết tài khoản Google Ads tạo và quản lý lượt chuyển đổi cho khách hàng này. Đối với những khách hàng sử dụng tính năng theo dõi lượt chuyển đổi trên nhiều tài khoản, đây là mã nhận dạng của tài khoản người quản lý. Bạn nên cung cấp mã khách hàng chuyển đổi của Google Ads dưới dạng customer_id trong các yêu cầu API Google Ads để tạo và quản lý lượt chuyển đổi. Xin lưu ý rằng trường này sẽ được điền ngay cả khi bạn chưa bật tính năng theo dõi lượt chuyển đổi.

Trường conversion_tracking_status cho biết liệu tính năng theo dõi lượt chuyển đổi có được bật hay không và liệu tài khoản có đang sử dụng tính năng theo dõi lượt chuyển đổi trên nhiều tài khoản hay không.

Tạo hành động chuyển đổi trong khách hàng chuyển đổi trên Google Ads

Nếu giá trị conversion_tracking_statusNOT_CONVERSION_TRACKED, thì tính năng theo dõi lượt chuyển đổi sẽ không được bật cho tài khoản. Bật tính năng theo dõi lượt chuyển đổi bằng cách tạo ít nhất một ConversionAction trong tài khoản chuyển đổi Google Ads, như trong ví dụ sau. Ngoài ra, bạn có thể tạo hành động chuyển đổi trong giao diện người dùng bằng cách làm theo hướng dẫn trong Trung tâm trợ giúp cho loại lượt chuyển đổi mà bạn muốn bật.

Xin lưu ý rằng tính năng lượt chuyển đổi nâng cao sẽ tự động bật khi được gửi thông qua API Google Ads, nhưng bạn có thể tắt tính năng này thông qua giao diện người dùng Google Ads.

Ví dụ về mã

Java

private void runExample(GoogleAdsClient googleAdsClient, long customerId) {

  // Creates a ConversionAction.
  ConversionAction conversionAction =
      ConversionAction.newBuilder()
          // Note that conversion action names must be unique. If a conversion action already
          // exists with the specified conversion_action_name the create operation will fail with
          // a ConversionActionError.DUPLICATE_NAME error.
          .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());
    }
  }
}
      

C#

public void Run(GoogleAdsClient client, long customerId)
{
    // Get the ConversionActionService.
    ConversionActionServiceClient conversionActionService =
        client.GetService(Services.V20.ConversionActionService);

    // Note that conversion action names must be unique.
    // If a conversion action already exists with the specified name the create operation
    // will fail with a ConversionAction.DUPLICATE_NAME error.
    string ConversionActionName = "Earth to Mars Cruises Conversion #"
        + ExampleUtilities.GetRandomString();

    // Add a conversion action.
    ConversionAction conversionAction = new ConversionAction()
    {
        Name = ConversionActionName,
        Category = ConversionActionCategory.Default,
        Type = ConversionActionType.Webpage,
        Status = ConversionActionStatus.Enabled,
        ViewThroughLookbackWindowDays = 15,
        ValueSettings = new ConversionAction.Types.ValueSettings()
        {
            DefaultValue = 23.41,
            AlwaysUseDefaultValue = true
        }
    };

    // Create the operation.
    ConversionActionOperation operation = new ConversionActionOperation()
    {
        Create = conversionAction
    };

    try
    {
        // Create the conversion action.
        MutateConversionActionsResponse response =
            conversionActionService.MutateConversionActions(customerId.ToString(),
                    new ConversionActionOperation[] { operation });

        // Display the results.
        foreach (MutateConversionActionResult newConversionAction in response.Results)
        {
            Console.WriteLine($"New conversion action with resource name = " +
                $"'{newConversionAction.ResourceName}' was added.");
        }
    }
    catch (GoogleAdsException e)
    {
        Console.WriteLine("Failure:");
        Console.WriteLine($"Message: {e.Message}");
        Console.WriteLine($"Failure: {e.Failure}");
        Console.WriteLine($"Request ID: {e.RequestId}");
        throw;
    }
}
      

PHP

public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId)
{
    // Creates a conversion action.
    $conversionAction = new ConversionAction([
        // Note that conversion action names must be unique.
        // If a conversion action already exists with the specified conversion_action_name
        // the create operation will fail with a ConversionActionError.DUPLICATE_NAME error.
        'name' => 'Earth to Mars Cruises Conversion #' . Helper::getPrintableDatetime(),
        'category' => ConversionActionCategory::PBDEFAULT,
        'type' => ConversionActionType::WEBPAGE,
        'status' => ConversionActionStatus::ENABLED,
        'view_through_lookback_window_days' => 15,
        'value_settings' => new ValueSettings([
            'default_value' => 23.41,
            'always_use_default_value' => true
        ])
    ]);

    // Creates a conversion action operation.
    $conversionActionOperation = new ConversionActionOperation();
    $conversionActionOperation->setCreate($conversionAction);

    // Issues a mutate request to add the conversion action.
    $conversionActionServiceClient = $googleAdsClient->getConversionActionServiceClient();
    $response = $conversionActionServiceClient->mutateConversionActions(
        MutateConversionActionsRequest::build($customerId, [$conversionActionOperation])
    );

    printf("Added %d conversion actions:%s", $response->getResults()->count(), PHP_EOL);

    foreach ($response->getResults() as $addedConversionAction) {
        /** @var ConversionAction $addedConversionAction */
        printf(
            "New conversion action added with resource name: '%s'%s",
            $addedConversionAction->getResourceName(),
            PHP_EOL
        );
    }
}
      

Python

def main(client, customer_id):
    conversion_action_service = client.get_service("ConversionActionService")

    # Create the operation.
    conversion_action_operation = client.get_type("ConversionActionOperation")

    # Create conversion action.
    conversion_action = conversion_action_operation.create

    # Note that conversion action names must be unique. If a conversion action
    # already exists with the specified conversion_action_name, the create
    # operation will fail with a ConversionActionError.DUPLICATE_NAME error.
    conversion_action.name = f"Earth to Mars Cruises Conversion {uuid.uuid4()}"
    conversion_action.type_ = (
        client.enums.ConversionActionTypeEnum.UPLOAD_CLICKS
    )
    conversion_action.category = (
        client.enums.ConversionActionCategoryEnum.DEFAULT
    )
    conversion_action.status = client.enums.ConversionActionStatusEnum.ENABLED
    conversion_action.view_through_lookback_window_days = 15

    # Create a value settings object.
    value_settings = conversion_action.value_settings
    value_settings.default_value = 15.0
    value_settings.always_use_default_value = True

    # Add the conversion action.
    conversion_action_response = (
        conversion_action_service.mutate_conversion_actions(
            customer_id=customer_id,
            operations=[conversion_action_operation],
        )
    )

    print(
        "Created conversion action "
        f'"{conversion_action_response.results[0].resource_name}".'
    )
      

Ruby

def add_conversion_action(customer_id)
  # GoogleAdsClient will read a config file from
  # ENV['HOME']/google_ads_config.rb when called without parameters
  client = Google::Ads::GoogleAds::GoogleAdsClient.new


  # Add a conversion action.
  conversion_action = client.resource.conversion_action do |ca|
    ca.name = "Earth to Mars Cruises Conversion #{(Time.new.to_f * 100).to_i}"
    ca.type = :UPLOAD_CLICKS
    ca.category = :DEFAULT
    ca.status = :ENABLED
    ca.view_through_lookback_window_days = 15

    # Create a value settings object.
    ca.value_settings = client.resource.value_settings do |vs|
      vs.default_value = 15
      vs.always_use_default_value = true
    end
  end

  # Create the operation.
  conversion_action_operation = client.operation.create_resource.conversion_action(conversion_action)

  # Add the ad group ad.
  response = client.service.conversion_action.mutate_conversion_actions(
    customer_id: customer_id,
    operations: [conversion_action_operation],
  )

  puts "New conversion action with resource name = #{response.results.first.resource_name}."
end
      

Perl

sub add_conversion_action {
  my ($api_client, $customer_id) = @_;

  # Note that conversion action names must be unique.
  # If a conversion action already exists with the specified conversion_action_name,
  # the create operation fails with error ConversionActionError.DUPLICATE_NAME.
  my $conversion_action_name = "Earth to Mars Cruises Conversion #" . uniqid();

  # Create a conversion action.
  my $conversion_action =
    Google::Ads::GoogleAds::V20::Resources::ConversionAction->new({
      name                          => $conversion_action_name,
      category                      => DEFAULT,
      type                          => WEBPAGE,
      status                        => ENABLED,
      viewThroughLookbackWindowDays => 15,
      valueSettings                 =>
        Google::Ads::GoogleAds::V20::Resources::ValueSettings->new({
          defaultValue          => 23.41,
          alwaysUseDefaultValue => "true"
        })});

  # Create a conversion action operation.
  my $conversion_action_operation =
    Google::Ads::GoogleAds::V20::Services::ConversionActionService::ConversionActionOperation
    ->new({create => $conversion_action});

  # Add the conversion action.
  my $conversion_actions_response =
    $api_client->ConversionActionService()->mutate({
      customerId => $customer_id,
      operations => [$conversion_action_operation]});

  printf "New conversion action added with resource name: '%s'.\n",
    $conversion_actions_response->{results}[0]{resourceName};

  return 1;
}
      

Đảm bảo rằng conversion_action_type được đặt thành giá trị ConversionActionType chính xác. Để biết thêm hướng dẫn về cách tạo hành động chuyển đổi trong API Google Ads, hãy xem bài viết Tạo hành động chuyển đổi.

Truy xuất một hành động chuyển đổi hiện có

Bạn có thể truy xuất thông tin chi tiết về một hành động chuyển đổi hiện có bằng cách đưa ra truy vấn sau. Đảm bảo bạn đặt mã khách hàng trong yêu cầu thành khách hàng chuyển đổi trên Google Ads mà bạn đã xác định ở trên và đặt loại hành động chuyển đổi thành giá trị ConversionActionType chính xác.

SELECT
  conversion_action.resource_name,
  conversion_action.name,
  conversion_action.status
FROM conversion_action
WHERE conversion_action.type = 'INSERT_CONVERSION_ACTION_TYPE'

Theo dõi lượt chuyển đổi trên nhiều tài khoản

Nếu bạn đang sử dụng tính năng theo dõi lượt chuyển đổi trên nhiều tài khoản, thì ConversionActionService sẽ trả về các hành động chuyển đổi sau:

  • Tất cả hành động chuyển đổi do tài khoản người quản lý xác định mà tài khoản sử dụng để theo dõi lượt chuyển đổi trên nhiều tài khoản
  • Tất cả hành động chuyển đổi mà khách hàng đã tích luỹ số liệu thống kê, bao gồm cả hành động do hệ thống xác định và hành động do tài khoản người quản lý sở hữu, ngay cả khi tài khoản người quản lý đó huỷ liên kết sau đó
  • Tất cả hành động mà khách hàng đã xác định trong tài khoản của họ
  • Lượt chuyển đổi trên Analytics được tạo trong các tài sản Google Analytics được liên kết. Chỉ số này bao gồm cả những hành động cho lượt chuyển đổi trên Analytics không được nhập vào Google Ads, có trạng thái là HIDDEN.

Kể từ phiên bản v19.1, bạn có thể sử dụng API Google Ads để chọn sử dụng tính năng theo dõi lượt chuyển đổi trên nhiều tài khoản khi tạo tài khoản khách hàng.

Khi tạo một Customer mới, hãy đặt conversion_tracking_setting.google_ads_conversion_customer thành tên tài nguyên của tài khoản người quản lý sẽ thay mặt tài khoản khách hàng quản lý các hành động chuyển đổi. Tài khoản người quản lý này cũng phải là tài khoản phát hành yêu cầu create cho tài khoản khách hàng mới.

Kể từ phiên bản v20, bạn có thể sử dụng API Google Ads để chọn sử dụng tính năng theo dõi lượt chuyển đổi trên nhiều tài khoản, cả khi tạo cập nhật tài khoản khách hàng.

Khi cập nhật tài khoản khách hàng hiện có, bạn có thể chọn theo dõi lượt chuyển đổi trên nhiều tài khoản bằng cách đặt trường conversion_tracking_setting.google_ads_conversion_customer. Bạn nên đặt trường này thành tên tài nguyên của tài khoản người quản lý sẽ thay mặt tài khoản khách hàng quản lý các hành động chuyển đổi. Tài khoản người quản lý này cũng phải là tài khoản phát hành yêu cầu update cho tài khoản khách hàng.

Khi bạn chọn sử dụng tính năng theo dõi lượt chuyển đổi trên nhiều tài khoản hoặc chuyển đổi trình quản lý theo dõi lượt chuyển đổi cho một tài khoản khách hàng hiện có, các lưu ý tương tự sẽ áp dụng như khi bạn thực hiện thay đổi này trong giao diện người dùng. Cụ thể:

  • Tài khoản khách hàng sẽ áp dụng các quy tắc giá trị lượt chuyển đổi mặc định và mục tiêu trong vòng đời khách hàng mặc định của trình quản lý theo dõi lượt chuyển đổi mới.
  • Những chiến dịch nhắm đến một hành động chuyển đổi cụ thể sẽ chuyển sang sử dụng mục tiêu lượt chuyển đổi mặc định của tài khoản người quản lý lượt chuyển đổi. Nếu bạn tiếp tục nhắm đến một hành động chuyển đổi cụ thể, thì điều đó có thể dẫn đến hành vi không nhất quán vì tài khoản người quản lý có thể không có cùng mục tiêu với tài khoản khách hàng. Đảm bảo rằng chiến dịch của bạn được tối ưu hoá cho đúng mục tiêu.
  • Nếu một tài khoản thuộc về nhiều tài khoản người quản lý, thì tài khoản đó chỉ có thể sử dụng hành động chuyển đổi từ một tài khoản người quản lý. Nếu bạn không chỉ định tài khoản theo dõi lượt chuyển đổi, thì tài khoản này sẽ tự sử dụng chính nó làm tài khoản theo dõi lượt chuyển đổi theo mặc định.

Tạo hành động chuyển đổi

Để đo lường lượt chuyển đổi, hãy thiết lập ConversionAction cho type của hành động chuyển đổi mà bạn muốn theo dõi. Ví dụ: một lượt mua hàng trực tuyến và một cuộc gọi điện thoại yêu cầu các hành động chuyển đổi khác nhau.

Cách tốt nhất để thiết lập hành động chuyển đổi mới trong API là sử dụng ví dụ về mã Thêm hành động chuyển đổi ở bên dưới. Mẫu này xử lý tất cả các tác vụ xác thực trong nền cho bạn và hướng dẫn bạn tạo ConversionAction.

Hầu hết các hành động chuyển đổi cũng yêu cầu bạn thực hiện thêm các bước để theo dõi. Ví dụ: để theo dõi lượt chuyển đổi trên trang web, bạn phải thêm một đoạn mã có tên là thẻ vào trang chuyển đổi trên trang web. Để biết các yêu cầu chi tiết về các loại hành động chuyển đổi khác, hãy xem bài viết trên Trung tâm trợ giúp của chúng tôi.

Ví dụ về mã

Ví dụ về mã sau đây sẽ hướng dẫn bạn quy trình tạo một hành động chuyển đổi mới. Cụ thể, hàm này tạo một hành động chuyển đổi với type được đặt thành UPLOAD_CLICKS. Phương thức này cũng đặt category thành DEFAULT.

Các chế độ cài đặt mặc định sau đây sẽ áp dụng:

Java

private void runExample(GoogleAdsClient googleAdsClient, long customerId) {

  // Creates a ConversionAction.
  ConversionAction conversionAction =
      ConversionAction.newBuilder()
          // Note that conversion action names must be unique. If a conversion action already
          // exists with the specified conversion_action_name the create operation will fail with
          // a ConversionActionError.DUPLICATE_NAME error.
          .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());
    }
  }
}
      

C#

public void Run(GoogleAdsClient client, long customerId)
{
    // Get the ConversionActionService.
    ConversionActionServiceClient conversionActionService =
        client.GetService(Services.V20.ConversionActionService);

    // Note that conversion action names must be unique.
    // If a conversion action already exists with the specified name the create operation
    // will fail with a ConversionAction.DUPLICATE_NAME error.
    string ConversionActionName = "Earth to Mars Cruises Conversion #"
        + ExampleUtilities.GetRandomString();

    // Add a conversion action.
    ConversionAction conversionAction = new ConversionAction()
    {
        Name = ConversionActionName,
        Category = ConversionActionCategory.Default,
        Type = ConversionActionType.Webpage,
        Status = ConversionActionStatus.Enabled,
        ViewThroughLookbackWindowDays = 15,
        ValueSettings = new ConversionAction.Types.ValueSettings()
        {
            DefaultValue = 23.41,
            AlwaysUseDefaultValue = true
        }
    };

    // Create the operation.
    ConversionActionOperation operation = new ConversionActionOperation()
    {
        Create = conversionAction
    };

    try
    {
        // Create the conversion action.
        MutateConversionActionsResponse response =
            conversionActionService.MutateConversionActions(customerId.ToString(),
                    new ConversionActionOperation[] { operation });

        // Display the results.
        foreach (MutateConversionActionResult newConversionAction in response.Results)
        {
            Console.WriteLine($"New conversion action with resource name = " +
                $"'{newConversionAction.ResourceName}' was added.");
        }
    }
    catch (GoogleAdsException e)
    {
        Console.WriteLine("Failure:");
        Console.WriteLine($"Message: {e.Message}");
        Console.WriteLine($"Failure: {e.Failure}");
        Console.WriteLine($"Request ID: {e.RequestId}");
        throw;
    }
}
      

PHP

public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId)
{
    // Creates a conversion action.
    $conversionAction = new ConversionAction([
        // Note that conversion action names must be unique.
        // If a conversion action already exists with the specified conversion_action_name
        // the create operation will fail with a ConversionActionError.DUPLICATE_NAME error.
        'name' => 'Earth to Mars Cruises Conversion #' . Helper::getPrintableDatetime(),
        'category' => ConversionActionCategory::PBDEFAULT,
        'type' => ConversionActionType::WEBPAGE,
        'status' => ConversionActionStatus::ENABLED,
        'view_through_lookback_window_days' => 15,
        'value_settings' => new ValueSettings([
            'default_value' => 23.41,
            'always_use_default_value' => true
        ])
    ]);

    // Creates a conversion action operation.
    $conversionActionOperation = new ConversionActionOperation();
    $conversionActionOperation->setCreate($conversionAction);

    // Issues a mutate request to add the conversion action.
    $conversionActionServiceClient = $googleAdsClient->getConversionActionServiceClient();
    $response = $conversionActionServiceClient->mutateConversionActions(
        MutateConversionActionsRequest::build($customerId, [$conversionActionOperation])
    );

    printf("Added %d conversion actions:%s", $response->getResults()->count(), PHP_EOL);

    foreach ($response->getResults() as $addedConversionAction) {
        /** @var ConversionAction $addedConversionAction */
        printf(
            "New conversion action added with resource name: '%s'%s",
            $addedConversionAction->getResourceName(),
            PHP_EOL
        );
    }
}
      

Python

def main(client, customer_id):
    conversion_action_service = client.get_service("ConversionActionService")

    # Create the operation.
    conversion_action_operation = client.get_type("ConversionActionOperation")

    # Create conversion action.
    conversion_action = conversion_action_operation.create

    # Note that conversion action names must be unique. If a conversion action
    # already exists with the specified conversion_action_name, the create
    # operation will fail with a ConversionActionError.DUPLICATE_NAME error.
    conversion_action.name = f"Earth to Mars Cruises Conversion {uuid.uuid4()}"
    conversion_action.type_ = (
        client.enums.ConversionActionTypeEnum.UPLOAD_CLICKS
    )
    conversion_action.category = (
        client.enums.ConversionActionCategoryEnum.DEFAULT
    )
    conversion_action.status = client.enums.ConversionActionStatusEnum.ENABLED
    conversion_action.view_through_lookback_window_days = 15

    # Create a value settings object.
    value_settings = conversion_action.value_settings
    value_settings.default_value = 15.0
    value_settings.always_use_default_value = True

    # Add the conversion action.
    conversion_action_response = (
        conversion_action_service.mutate_conversion_actions(
            customer_id=customer_id,
            operations=[conversion_action_operation],
        )
    )

    print(
        "Created conversion action "
        f'"{conversion_action_response.results[0].resource_name}".'
    )
      

Ruby

def add_conversion_action(customer_id)
  # GoogleAdsClient will read a config file from
  # ENV['HOME']/google_ads_config.rb when called without parameters
  client = Google::Ads::GoogleAds::GoogleAdsClient.new


  # Add a conversion action.
  conversion_action = client.resource.conversion_action do |ca|
    ca.name = "Earth to Mars Cruises Conversion #{(Time.new.to_f * 100).to_i}"
    ca.type = :UPLOAD_CLICKS
    ca.category = :DEFAULT
    ca.status = :ENABLED
    ca.view_through_lookback_window_days = 15

    # Create a value settings object.
    ca.value_settings = client.resource.value_settings do |vs|
      vs.default_value = 15
      vs.always_use_default_value = true
    end
  end

  # Create the operation.
  conversion_action_operation = client.operation.create_resource.conversion_action(conversion_action)

  # Add the ad group ad.
  response = client.service.conversion_action.mutate_conversion_actions(
    customer_id: customer_id,
    operations: [conversion_action_operation],
  )

  puts "New conversion action with resource name = #{response.results.first.resource_name}."
end
      

Perl

sub add_conversion_action {
  my ($api_client, $customer_id) = @_;

  # Note that conversion action names must be unique.
  # If a conversion action already exists with the specified conversion_action_name,
  # the create operation fails with error ConversionActionError.DUPLICATE_NAME.
  my $conversion_action_name = "Earth to Mars Cruises Conversion #" . uniqid();

  # Create a conversion action.
  my $conversion_action =
    Google::Ads::GoogleAds::V20::Resources::ConversionAction->new({
      name                          => $conversion_action_name,
      category                      => DEFAULT,
      type                          => WEBPAGE,
      status                        => ENABLED,
      viewThroughLookbackWindowDays => 15,
      valueSettings                 =>
        Google::Ads::GoogleAds::V20::Resources::ValueSettings->new({
          defaultValue          => 23.41,
          alwaysUseDefaultValue => "true"
        })});

  # Create a conversion action operation.
  my $conversion_action_operation =
    Google::Ads::GoogleAds::V20::Services::ConversionActionService::ConversionActionOperation
    ->new({create => $conversion_action});

  # Add the conversion action.
  my $conversion_actions_response =
    $api_client->ConversionActionService()->mutate({
      customerId => $customer_id,
      operations => [$conversion_action_operation]});

  printf "New conversion action added with resource name: '%s'.\n",
    $conversion_actions_response->{results}[0]{resourceName};

  return 1;
}
      

Bạn có thể xem ví dụ này trong thư mục Tái tiếp thị của thư viện ứng dụng và trong bộ sưu tập ví dụ về mã: Thêm ví dụ về mã Hành động chuyển đổi.

Xác nhận kết quả

Google Ads và API Google Ads hỗ trợ nhiều hành động chuyển đổi, vì vậy, một số quy tắc xác thực sẽ khác nhau tuỳ theo type của hành động.

Lỗi phổ biến nhất khi tạo hành động chuyển đổi là DUPLICATE_NAME. Đảm bảo bạn đang sử dụng tên riêng biệt cho mỗi hành động chuyển đổi.

Sau đây là một số mẹo về cách thiết lập các trường ConversionAction:

Tất cả các trường enum
Nếu bạn cố gắng đặt bất kỳ trường enum nào thành UNKNOWN, bạn sẽ gặp lỗi RequestError.INVALID_ENUM_VALUE.
app_id
Thuộc tính app_id là không thể thay đổi và chỉ có thể được đặt khi tạo một lượt chuyển đổi ứng dụng mới.
attribution_model_settings
Việc đặt thuộc tính này thành tuỳ chọn không dùng nữa sẽ dẫn đến lỗi CANNOT_SET_RULE_BASED_ATTRIBUTION_MODELS. Google Ads chỉ hỗ trợ GOOGLE_ADS_LAST_CLICKGOOGLE_SEARCH_ATTRIBUTION_DATA_DRIVEN.
click_through_lookback_window_days

Việc đặt thuộc tính này thành một giá trị nằm ngoài phạm vi cho phép sẽ dẫn đến lỗi RangeError.TOO_LOW hoặc RangeError.TOO_HIGH.

Thuộc tính này phải nằm trong phạm vi [1,60] đối với hành động chuyển đổi AD_CALL hoặc WEBSITE_CALL. Đối với hầu hết các hành động chuyển đổi khác, phạm vi được phép là [1,30].

include_in_conversions_metric

Không đặt được giá trị này trong thao tác create hoặc update do lỗi FieldError.IMMUTABLE_FIELD. Thay vào đó, hãy đặt primary_for_goal như mô tả trong Hướng dẫn về mục tiêu lượt chuyển đổi.

phone_call_duration_seconds

Nếu bạn cố gắng đặt thuộc tính này trên một hành động chuyển đổi không dành cho cuộc gọi, thì sẽ xảy ra lỗi FieldError.VALUE_MUST_BE_UNSET.

type

Thuộc tính type là không thể thay đổi và chỉ có thể được đặt khi tạo lượt chuyển đổi mới.

Việc cập nhật hành động chuyển đổi có type bằng UNKNOWN sẽ dẫn đến lỗi MutateError.MUTATE_NOT_ALLOWED.

value_settings

value_settings cho hành động chuyển đổi WEBSITE_CALL hoặc AD_CALL phải đặt always_use_default_value thành true. Việc chỉ định giá trị false khi tạo hoặc cập nhật giá trị này sẽ dẫn đến lỗi INVALID_VALUE.

view_through_lookback_window_days

Việc đặt thuộc tính này thành một giá trị nằm ngoài phạm vi cho phép sẽ dẫn đến lỗi RangeError.TOO_LOW hoặc RangeError.TOO_HIGH. Đối với hầu hết các hành động chuyển đổi, phạm vi cho phép là [1,30].

Bạn không thể đặt thuộc tính này trên hành động chuyển đổi AD_CALL hoặc WEBSITE_CALL. Việc chỉ định một giá trị sẽ dẫn đến lỗi VALUE_MUST_BE_UNSET.