googleads/google-ads-python

Cannot Remove Specific App Ad Asset under Campaign's AdGroup using AdService

galihlprakoso opened this issue · 2 comments

What is your question?
I want to remove specific asset. This is my code:

# ...
def _pause_assets_by_campaign(
        self,
        campaign_resource_name,
        paused_assets_resource_names
    ):
        ga_service = self._client.get_service("GoogleAdsService")
        query = f"""SELECT
                     ad_group_ad.resource_name,
                     ad_group_ad.ad.id,
                     ad_group_ad.ad.app_ad.images,
                     ad_group_ad.ad.app_ad.youtube_videos,
                     campaign.resource_name
                   FROM ad_group_ad
                   WHERE campaign.resource_name = '{campaign_resource_name}'"""

        response = ga_service.search_stream(customer_id=self._customer_id, query=query)

        paused_assets_resource_names_map = {}

        for resource_name in paused_assets_resource_names:
            paused_assets_resource_names_map[resource_name] = True

        for batch in response:
            for row in batch.results:
                ad = row.ad_group_ad.ad

                ad_service = self._client.get_service("AdService")
                ad_operation = self._client.get_type("AdOperation")

                updated_ad = ad_operation.update
                updated_ad.resource_name = ad.resource_name

                youtube_videos = [
                    youtube_asset for youtube_asset in ad.app_ad.youtube_videos if youtube_asset.asset not in paused_assets_resource_names_map
                ]

                updated_ad.app_ad.youtube_videos = youtube_videos

                images = [
                    image_asset for image_asset in ad.app_ad.images if image_asset.asset not in paused_assets_resource_names_map
                ]

                updated_ad.app_ad.images = images

                field_mask = protobuf_helpers.field_mask(
                    None, updated_ad._pb
                )

                self._client.copy_from(ad_operation.update_mask, field_mask)

                response = ad_service.mutate_ads(
                    customer_id=self._customer_id, operations=[ad_operation],
                )

                resource_name = response.results[0].resource_name

There is no error thrown, but my asset is not getting removed from my adgroup campaign. Is there anything wrong with my implementation?

@galihlprakoso my guess, if you're sending the request with an empty field, is that you need to manually add the field name to the field mask in order to communicate to the API that you want to mutate the field to its default value.

See the bottom section of these docs, and let us know if you have any questions.

Changing my code from:

field_mask = protobuf_helpers.field_mask(
    ad._pb, updated_ad._pb
)

Into:

field_mask = protobuf_helpers.field_mask(
   None, updated_ad._pb
)

Somehow fixes the issue. Now when I debugged my fieldmask. It gives the expected result:

 paths: "resource_name"
paths: "app_ad.images"
paths: "app_ad.youtube_videos"

And now my asset is excluded successfully. Thanks! @BenRKarl