您必須在 Google Ads 轉換帳戶中啟用轉換追蹤,才能記錄轉換。本指南將詳細說明如何確認是否已啟用轉換追蹤、啟用轉換追蹤 (如果尚未啟用),以及擷取現有轉換動作的相關資訊。
此外,您還必須採取額外步驟,才能追蹤大多數轉換動作。如要進一步瞭解各種轉換動作類型及其規定,請參閱建立轉換動作指南。
設定網站以追蹤轉換
如果您要開始整合離線轉換匯入功能,請先按照「設定 Google 代碼以使用待開發客戶強化轉換」指南中的步驟,設定網站來追蹤待開發客戶強化轉換。您也可以按照「設定 Google 代碼管理工具,以使用待開發客戶強化轉換」指南中的步驟,使用 Google 代碼管理工具設定網站。
在 Google Ads 轉換帳戶中啟用轉換追蹤
擷取轉換追蹤設定的相關資訊
您可以查詢 Customer
資源的 ConversionTrackingSetting
,檢查帳戶的轉換追蹤設定,並確認已啟用轉換追蹤。使用 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
google_ads_conversion_customer
欄位會指出為這個客戶建立及管理轉換的 Google Ads 帳戶。如果客戶使用跨帳戶轉換追蹤,這個 ID 就是管理員帳戶的 ID。在 Google Ads API 要求中,建立及管理轉換時,應提供 Google Ads 轉換客戶 ID 做為 customer_id
。請注意,即使未啟用轉換追蹤,系統仍會填入這個欄位。
「conversion_tracking_status
」欄位會指出是否已啟用轉換追蹤,以及帳戶是否使用跨帳戶轉換追蹤。
在 Google Ads 轉換顧客中建立轉換動作
如果 conversion_tracking_status
值為 NOT_CONVERSION_TRACKED
,表示帳戶未啟用轉換追蹤。如要啟用轉換追蹤,請在 Google Ads 轉換帳戶中建立至少一個ConversionAction
,如下例所示。或者,您也可以按照說明中心的指示,在使用者介面中建立轉換動作,啟用所需的轉換類型。
請注意,透過 Google Ads API 傳送資料時,系統會自動啟用強化轉換,但您可以在 Google Ads 使用者介面中停用這項功能。
程式碼範例
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}".' )
小茹
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; }
確認 conversion_action_type
已設為正確的ConversionActionType
值。如需在 Google Ads API 中建立轉換動作的更多指引,請參閱「建立轉換動作」。
擷取現有轉換動作
您可以發出下列查詢,擷取現有轉換動作的詳細資料。請確認要求中的客戶 ID 已設為上述 Google Ads 轉換客戶,且轉換動作類型已設為正確的ConversionActionType
值。
SELECT
conversion_action.resource_name,
conversion_action.name,
conversion_action.status
FROM conversion_action
WHERE conversion_action.type = 'INSERT_CONVERSION_ACTION_TYPE'
跨帳戶轉換追蹤
如果您使用跨帳戶轉換追蹤,ConversionActionService
會傳回下列轉換動作:
- 帳戶用於跨帳戶轉換追蹤的管理員帳戶所定義的所有轉換動作
- 客戶累積統計資料的所有轉換動作,包括系統定義的動作,以及管理員擁有的動作 (即使該管理員隨後取消連結)
- 顧客在自家帳戶中定義的所有動作
- 在已連結的 Google Analytics 資源中建立的 Analytics 轉換。
包括未匯入 Google Ads 的 Analytics 轉換動作,狀態為
HIDDEN
。
自 v19.1
起,您可以在建立客戶帳戶時,使用 Google Ads API 選擇啟用跨轉換追蹤。
建立新的 Customer
時,請將 conversion_tracking_setting.google_ads_conversion_customer
設為管理員帳戶的資源名稱,該帳戶應代表客戶帳戶管理轉換動作。這個管理員帳戶也必須是發出新客戶帳戶create
要求的帳戶。
自 v20
起,您可以使用 Google Ads API,在建立和更新客戶帳戶時,選擇啟用跨轉換追蹤。
更新現有客戶帳戶時,您可以設定 conversion_tracking_setting.google_ads_conversion_customer
欄位,選擇採用跨帳戶轉換追蹤。這個欄位應設為管理員帳戶的資源名稱,該帳戶應代表客戶帳戶管理轉換動作。這個管理員帳戶也必須是發出客戶帳戶update
要求的帳戶。
採用跨帳戶轉換追蹤,或為現有客戶帳戶切換轉換追蹤管理員時,適用與在使用者介面中進行這項變更時相同的注意事項。詳細說明:
- 用戶端帳戶會採用新轉換追蹤管理工具的預設轉換價值規則和預設顧客生命週期目標。
- 如果廣告活動指定特定轉換動作,系統會改用轉換管理員帳戶的預設轉換目標。如果繼續指定特定轉換動作,可能會導致行為不一致,因為管理員帳戶的目標可能與客戶帳戶不同。確保廣告活動已針對正確目標進行最佳化。
- 如果帳戶同屬多個管理員帳戶,則只能採用其中一個管理員帳戶的轉換動作。如未指定轉換追蹤帳戶,帳戶預設會將自己做為轉換追蹤帳戶。
建立轉換動作
如要評估轉換,請為要追蹤的轉換動作設定ConversionAction
type
。舉例來說,線上購物和來電需要不同的轉換動作。
在 API 中設定新轉換動作的最佳方式,是使用下方的「新增轉換動作」程式碼範例。這個範例會為您處理所有背景驗證工作,並逐步說明如何建立 ConversionAction
。
此外,您還必須採取額外步驟,才能追蹤大多數轉換動作。舉例來說,如要追蹤網站上的轉換,您必須在網站的轉換頁面中加入稱為「代碼」的程式碼片段。如要瞭解其他轉換動作類型的詳細規定,請參閱說明中心文章。
程式碼範例
下列程式碼範例逐步說明如何建立新的轉換動作。具體來說,這會建立轉換動作,並將 type
設為 UPLOAD_CLICKS
。
並將 category
設為 DEFAULT
。
系統會套用下列預設設定:
Google Ads API 會自動設定
primary_for_goal
欄位,但您可以明確設定這個欄位,在與轉換目標合併時,控管轉換動作對帳戶報表和出價的影響。Google Ads API 會自動將
counting_type
設為MANY_PER_CLICK
。詳情請參閱「關於轉換計算選項」。Google Ads API 會將
attribution_model_settings
欄位設為AttributionModel
的GOOGLE_SEARCH_ATTRIBUTION_DATA_DRIVEN
值,將歸因模式設為「資料驅動」。如要進一步瞭解歸因模式,請參閱這篇說明中心文章。
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}".' )
小茹
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; }
您可以在用戶端程式庫的「再行銷」資料夾中,以及程式碼範例集合中查看這個範例:新增轉換動作程式碼範例。
驗證
Google Ads 和 Google Ads API 支援各種轉換動作,因此部分驗證規則會因動作的type
而異。
建立轉換動作時,最常見的錯誤是DUPLICATE_NAME
。
請確認每個轉換動作都使用了不重複的名稱。
以下是設定 ConversionAction
欄位的訣竅:
- 所有列舉欄位
- 嘗試將任何列舉欄位設為
UNKNOWN
會導致RequestError.INVALID_ENUM_VALUE
錯誤。 app_id
app_id
屬性不可變更,只能在建立新的應用程式轉換時設定。attribution_model_settings
- 如果將此值設為已淘汰的選項,會導致
CANNOT_SET_RULE_BASED_ATTRIBUTION_MODELS
錯誤。Google Ads 僅支援GOOGLE_ADS_LAST_CLICK
和GOOGLE_SEARCH_ATTRIBUTION_DATA_DRIVEN
。 click_through_lookback_window_days
如果將這個屬性設為允許範圍以外的值,會導致
RangeError.TOO_LOW
或RangeError.TOO_HIGH
錯誤。如果是
AD_CALL
或WEBSITE_CALL
轉換動作,這項屬性必須在[1,60]
範圍內。對於大多數其他轉換動作,允許的範圍為[1,30]
。include_in_conversions_metric
在
create
或update
作業中設定這個值會失敗,並出現FieldError.IMMUTABLE_FIELD
錯誤。請改為按照轉換目標指南中的說明設定primary_for_goal
。phone_call_duration_seconds
如果嘗試在非來電轉換動作中設定這項屬性,會導致
FieldError.VALUE_MUST_BE_UNSET
錯誤。type
type
屬性不可變動,只能在建立新轉換時設定。如果更新轉換動作時,
type
等於UNKNOWN
,就會導致MutateError.MUTATE_NOT_ALLOWED
錯誤。value_settings
WEBSITE_CALL
或AD_CALL
轉換動作的value_settings
必須將always_use_default_value
設為true
。建立或更新這個值時,如果指定false
,就會導致INVALID_VALUE
錯誤。view_through_lookback_window_days
如果將這個屬性設為允許範圍以外的值,會導致
RangeError.TOO_LOW
或RangeError.TOO_HIGH
錯誤。大多數轉換動作的允許範圍為[1,30]
。無法在
AD_CALL
或WEBSITE_CALL
轉換動作中設定這項屬性。指定值會導致VALUE_MUST_BE_UNSET
錯誤。