李
李晓川
Guest
I want to create a campaign through the Google Ads API. And I've successfully created a campaign. But when I compared it with the UI created one, I realized that I didn't choose a custom goal. In the UI I'll set it up like this. enter image description here
I can get all the custom goals via GAQL.
This is shown in the figure below.enter image description here
Now, I want to select the custom goal from the query while creating a campaign using google ads api.How should I write my python code?
Reference document link
Also, I've attached the sample code for the base campaign that I've successfully created, it's not much different from what's in the documentation.
I never found in the documentation how to select a custom goal when creating campaign.and I had to specify the campaign type as DISPLAY. It can't be a
<p>I want to create a campaign through the Google Ads API. And I've successfully created a campaign. But when I compared it with the UI created one, I realized that I didn't choose a custom goal. In the UI I'll set it up like this.
<a href="https://i.sstatic.net/mxzyzDsM.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>I can get all the custom goals via GAQL.</p>
<pre class="lang-sql prettyprint-override"><code>SELECT
custom_conversion_goal.resource_name,
custom_conversion_goal.id,
custom_conversion_goal.name,
custom_conversion_goal.conversion_actions,
custom_conversion_goal.status,
customer.id
FROM custom_conversion_goal
WHERE custom_conversion_goal.name='xxx'
</code></pre>
<p>This is shown in the figure below.<a href="https://i.sstatic.net/zh0qD85n.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>Now, I want to select the custom goal from the query while creating a campaign using google ads api.How should I write my python code?</p>
<p>Reference <a href="https://developers.google.com/google-ads/api/docs/conversions/goals/campaign-goals#custom_goals" rel="nofollow noreferrer">document link</a></p>
<p>Also, I've attached the sample code for the base campaign that I've successfully created, it's not much different from what's in the documentation.</p>
<pre class="lang-py prettyprint-override"><code>import sys
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
_DATE_FORMAT = "%Y%m%d"
def handle_googleads_exception(exception):
print(
f'Request with ID "{exception.request_id}" failed with status '
f'"{exception.error.code().name}" and includes the following errors:'
)
for error in exception.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
def create_campaign(client, customer_id):
campaign_budget_service = client.get_service("CampaignBudgetService")
campaign_service = client.get_service("CampaignService")
campaign_budget_operation = client.get_type("CampaignBudgetOperation")
campaign_budget = campaign_budget_operation.create
ampaign_budget.amount_micros = 50000000
campaign_budget.explicitly_shared = False
campaign_budget_response = None
try:
campaign_budget_response = campaign_budget_service.mutate_campaign_budgets(
customer_id=customer_id, operations=[campaign_budget_operation]
)
except GoogleAdsException as e:
handle_googleads_exception(e)
campaign_operation = client.get_type("CampaignOperation")
campaign = campaign_operation.create
campaign.name = 'campaign-test1'
campaign.status = client.enums.CampaignStatusEnum.PAUSED
campaign.tracking_url_template = "{...}"
campaign.advertising_channel_type = client.enums.AdvertisingChannelTypeEnum.DISPLAY
campaign.targeting_setting.target_restrictions.targeting_dimension = client.enums.TargetingDimensionEnum.AUDIENCE
campaign.targeting_setting.target_restrictions.bid_only = False
campaign.audience_setting.use_audience_grouped = False
campaign.geo_target_type_setting.positive_geo_target_type = client.enums.PositiveGeoTargetTypeEnum.PRESENCE
campaign.geo_target_type_setting.negative_geo_target_type = client.enums.NegativeGeoTargetTypeEnum.PRESENCE
campaign.target_cpa.target_cpa_micros = 100000
campaign.campaign_budget = campaign_budget_response.results[0].resource_name
# Setting the specified custom goal but with an error
# AttributeError: Unknown field for Campaign: conversion_goal_campaign_config
campaign.conversion_goal_campaign_config.custom_conversion_goal = "customers/xxxx/customConversionGoals/xxxxx"
try:
campaign_response = campaign_service.mutate_campaigns(
customer_id=customer_id, operations=[campaign_operation]
)
print(f"Created campaign {campaign_response.results[0].resource_name}.")
except GoogleAdsException as ex:
handle_googleads_exception(ex)
if __name__ == '__main__':
googleads_client = GoogleAdsClient.load_from_storage(
path=r"./google-ads-th.yaml",
version="v17"
)
customer_id = "xxxx"
create_campaign(googleads_client, customer_id)
</code></pre>
<p>I never found in the documentation how to select a custom goal when creating campaign.and I had to specify the campaign type as DISPLAY. It can't be a <code>performance max campaign</code> or an <code>app campaign</code>. Both campaigns can specify a custom goal. Because it is clearly stated in the documentation.</p>
Continue reading...
I can get all the custom goals via GAQL.
Code:
SELECT
custom_conversion_goal.resource_name,
custom_conversion_goal.id,
custom_conversion_goal.name,
custom_conversion_goal.conversion_actions,
custom_conversion_goal.status,
customer.id
FROM custom_conversion_goal
WHERE custom_conversion_goal.name='xxx'
This is shown in the figure below.enter image description here
Now, I want to select the custom goal from the query while creating a campaign using google ads api.How should I write my python code?
Reference document link
Also, I've attached the sample code for the base campaign that I've successfully created, it's not much different from what's in the documentation.
Code:
import sys
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
_DATE_FORMAT = "%Y%m%d"
def handle_googleads_exception(exception):
print(
f'Request with ID "{exception.request_id}" failed with status '
f'"{exception.error.code().name}" and includes the following errors:'
)
for error in exception.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
def create_campaign(client, customer_id):
campaign_budget_service = client.get_service("CampaignBudgetService")
campaign_service = client.get_service("CampaignService")
campaign_budget_operation = client.get_type("CampaignBudgetOperation")
campaign_budget = campaign_budget_operation.create
ampaign_budget.amount_micros = 50000000
campaign_budget.explicitly_shared = False
campaign_budget_response = None
try:
campaign_budget_response = campaign_budget_service.mutate_campaign_budgets(
customer_id=customer_id, operations=[campaign_budget_operation]
)
except GoogleAdsException as e:
handle_googleads_exception(e)
campaign_operation = client.get_type("CampaignOperation")
campaign = campaign_operation.create
campaign.name = 'campaign-test1'
campaign.status = client.enums.CampaignStatusEnum.PAUSED
campaign.tracking_url_template = "{...}"
campaign.advertising_channel_type = client.enums.AdvertisingChannelTypeEnum.DISPLAY
campaign.targeting_setting.target_restrictions.targeting_dimension = client.enums.TargetingDimensionEnum.AUDIENCE
campaign.targeting_setting.target_restrictions.bid_only = False
campaign.audience_setting.use_audience_grouped = False
campaign.geo_target_type_setting.positive_geo_target_type = client.enums.PositiveGeoTargetTypeEnum.PRESENCE
campaign.geo_target_type_setting.negative_geo_target_type = client.enums.NegativeGeoTargetTypeEnum.PRESENCE
campaign.target_cpa.target_cpa_micros = 100000
campaign.campaign_budget = campaign_budget_response.results[0].resource_name
# Setting the specified custom goal but with an error
# AttributeError: Unknown field for Campaign: conversion_goal_campaign_config
campaign.conversion_goal_campaign_config.custom_conversion_goal = "customers/xxxx/customConversionGoals/xxxxx"
try:
campaign_response = campaign_service.mutate_campaigns(
customer_id=customer_id, operations=[campaign_operation]
)
print(f"Created campaign {campaign_response.results[0].resource_name}.")
except GoogleAdsException as ex:
handle_googleads_exception(ex)
if __name__ == '__main__':
googleads_client = GoogleAdsClient.load_from_storage(
path=r"./google-ads-th.yaml",
version="v17"
)
customer_id = "xxxx"
create_campaign(googleads_client, customer_id)
I never found in the documentation how to select a custom goal when creating campaign.and I had to specify the campaign type as DISPLAY. It can't be a
performance max campaign
or an app campaign
. Both campaigns can specify a custom goal. Because it is clearly stated in the documentation.<p>I want to create a campaign through the Google Ads API. And I've successfully created a campaign. But when I compared it with the UI created one, I realized that I didn't choose a custom goal. In the UI I'll set it up like this.
<a href="https://i.sstatic.net/mxzyzDsM.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>I can get all the custom goals via GAQL.</p>
<pre class="lang-sql prettyprint-override"><code>SELECT
custom_conversion_goal.resource_name,
custom_conversion_goal.id,
custom_conversion_goal.name,
custom_conversion_goal.conversion_actions,
custom_conversion_goal.status,
customer.id
FROM custom_conversion_goal
WHERE custom_conversion_goal.name='xxx'
</code></pre>
<p>This is shown in the figure below.<a href="https://i.sstatic.net/zh0qD85n.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>Now, I want to select the custom goal from the query while creating a campaign using google ads api.How should I write my python code?</p>
<p>Reference <a href="https://developers.google.com/google-ads/api/docs/conversions/goals/campaign-goals#custom_goals" rel="nofollow noreferrer">document link</a></p>
<p>Also, I've attached the sample code for the base campaign that I've successfully created, it's not much different from what's in the documentation.</p>
<pre class="lang-py prettyprint-override"><code>import sys
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
_DATE_FORMAT = "%Y%m%d"
def handle_googleads_exception(exception):
print(
f'Request with ID "{exception.request_id}" failed with status '
f'"{exception.error.code().name}" and includes the following errors:'
)
for error in exception.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
def create_campaign(client, customer_id):
campaign_budget_service = client.get_service("CampaignBudgetService")
campaign_service = client.get_service("CampaignService")
campaign_budget_operation = client.get_type("CampaignBudgetOperation")
campaign_budget = campaign_budget_operation.create
ampaign_budget.amount_micros = 50000000
campaign_budget.explicitly_shared = False
campaign_budget_response = None
try:
campaign_budget_response = campaign_budget_service.mutate_campaign_budgets(
customer_id=customer_id, operations=[campaign_budget_operation]
)
except GoogleAdsException as e:
handle_googleads_exception(e)
campaign_operation = client.get_type("CampaignOperation")
campaign = campaign_operation.create
campaign.name = 'campaign-test1'
campaign.status = client.enums.CampaignStatusEnum.PAUSED
campaign.tracking_url_template = "{...}"
campaign.advertising_channel_type = client.enums.AdvertisingChannelTypeEnum.DISPLAY
campaign.targeting_setting.target_restrictions.targeting_dimension = client.enums.TargetingDimensionEnum.AUDIENCE
campaign.targeting_setting.target_restrictions.bid_only = False
campaign.audience_setting.use_audience_grouped = False
campaign.geo_target_type_setting.positive_geo_target_type = client.enums.PositiveGeoTargetTypeEnum.PRESENCE
campaign.geo_target_type_setting.negative_geo_target_type = client.enums.NegativeGeoTargetTypeEnum.PRESENCE
campaign.target_cpa.target_cpa_micros = 100000
campaign.campaign_budget = campaign_budget_response.results[0].resource_name
# Setting the specified custom goal but with an error
# AttributeError: Unknown field for Campaign: conversion_goal_campaign_config
campaign.conversion_goal_campaign_config.custom_conversion_goal = "customers/xxxx/customConversionGoals/xxxxx"
try:
campaign_response = campaign_service.mutate_campaigns(
customer_id=customer_id, operations=[campaign_operation]
)
print(f"Created campaign {campaign_response.results[0].resource_name}.")
except GoogleAdsException as ex:
handle_googleads_exception(ex)
if __name__ == '__main__':
googleads_client = GoogleAdsClient.load_from_storage(
path=r"./google-ads-th.yaml",
version="v17"
)
customer_id = "xxxx"
create_campaign(googleads_client, customer_id)
</code></pre>
<p>I never found in the documentation how to select a custom goal when creating campaign.and I had to specify the campaign type as DISPLAY. It can't be a <code>performance max campaign</code> or an <code>app campaign</code>. Both campaigns can specify a custom goal. Because it is clearly stated in the documentation.</p>
Continue reading...