googleads / google-ads-python

Google Ads API Client Library for Python
Apache License 2.0
526 stars 480 forks source link

How to set the asset_automation_setting of Pmax Campaign ? #827

Closed Light-jason closed 10 months ago

Light-jason commented 10 months ago

I want to create a pmax campaign through api, and set the asset_automation as OPTED_OUT.

Here is my code, but it is wrong. ` mutate_operation = self.client.get_type("MutateOperation") campaign = mutate_operation.campaign_operation.create campaign.name = f"Performance Max campaign #{uuid4()}" if not campaign_name else campaign_name

    campaign.status = self.client.enums.CampaignStatusEnum.PAUSED if not status else self.client.enums.CampaignStatusEnum.ENABLED

    campaign.advertising_channel_type = (
        self.client.enums.AdvertisingChannelTypeEnum.PERFORMANCE_MAX
    )

    campaign.bidding_strategy_type = (
        self.biddingstrategytype[biddingstrategy]
    )
    campaign.maximize_conversion_value.target_roas = target_roas
    campaign.url_expansion_opt_out = url_expansion_opt_out

    # Assign the resource name with a temporary ID.
    campaign_service = self.client.get_service("CampaignService")
    campaign.resource_name = campaign_service.campaign_path(
        customer_id, self._PERFORMANCE_MAX_CAMPAIGN_TEMPORARY_ID
    )
    # Set the budget using the given budget resource name.
    campaign.campaign_budget = campaign_service.campaign_budget_path(
        customer_id, self._BUDGET_TEMPORARY_ID
    )

    # Optional fields
    campaign.start_date = (datetime.now() + timedelta(start_date)).strftime("%Y%m%d")
    campaign.end_date = (datetime.now() + timedelta(end_date)).strftime("%Y%m%d")
    campaign.asset_automation_settings = (self.client.enums.AssetAutomationTypeEnum.TEXT_ASSET_AUTOMATION,self.client.enums.AssetAutomationStatusEnum.OPTED_OUT)

    return mutate_operation

`

BenRKarl commented 10 months ago

@Light-jason what error do you receive when you submit this request to the API?

BenRKarl commented 10 months ago

@Light-jason after taking a closer look I see the problem. Instead of setting the campaign.asset_automation_settings field to a tuple, do the following:

# Since the AssetAutomationMessage is defined in the same file as Campaign, in this library it exists as a property on the Campaign class.
asset_automation_setting = campaign.AssetAutomationSetting()
asset_automation_setting.asset_automation_type = client.enums.AssetAutomationTypeEnum.TEXT_ASSET_AUTOMATION
asset_automation_setting.asset_automation_status = client.enums.AssetAutomationStatusEnum.OPTED_OUT
campaign.asset_automation_settings.append(asset_automation_setting)
Light-jason commented 10 months ago

@Light-jason after taking a closer look I see the problem. Instead of setting the campaign.asset_automation_settings field to a tuple, do the following:

# Since the AssetAutomationMessage is defined in the same file as Campaign, in this library it exists as a property on the Campaign class.
asset_automation_setting = campaign.AssetAutomationSetting()
asset_automation_setting.asset_automation_type = client.enums.AssetAutomationTypeEnum.TEXT_ASSET_AUTOMATION
asset_automation_setting.asset_automation_status = client.enums.AssetAutomationStatusEnum.OPTED_OUT
campaign.asset_automation_settings.append(asset_automation_setting)

thanks~ I would try this way.

Light-jason commented 6 months ago

@Light-jason after taking a closer look I see the problem. Instead of setting the campaign.asset_automation_settings field to a tuple, do the following:

# Since the AssetAutomationMessage is defined in the same file as Campaign, in this library it exists as a property on the Campaign class.
asset_automation_setting = campaign.AssetAutomationSetting()
asset_automation_setting.asset_automation_type = client.enums.AssetAutomationTypeEnum.TEXT_ASSET_AUTOMATION
asset_automation_setting.asset_automation_status = client.enums.AssetAutomationStatusEnum.OPTED_OUT
campaign.asset_automation_settings.append(asset_automation_setting)

when I used this code to close the text_asset_automation. The error is happened that "TypeError: Parameter to MergeFrom() must be instance of same class: expected google.ads.googleads.v16.resources.Campaign.AssetAutomationSetting got AssetAutomationSetting. "