Update/Delete Specific Assets in a given Ad Group/Campaign

1,763 views
Skip to first unread message

Gaspard Liu

unread,
Feb 9, 2020, 8:32:38 AM2/9/20
to AdWords API and Google Ads API Forum
Hi,

I'm trying to use the following python code to upload the Image Asset to a given ad group/campaign, but I found I can't set the specific ad group ID or campaign ID with AssetService, I was wondering how can I upload the assets to a specific ad group with given ad group ID? Besides, I found for AssetService with AssetOperation, you can only use 'create' to upload a new asset. But what if I'm trying to delete an existing asset, how can I do so?

def main(client, customer_id):
    """Main method, to run this code example as a standalone application."""

    # Download image from URL
    URL = 'https://goo.gl/3b9Wfh'
    image_content = requests.get(URL).content

    asset_operation = client.get_type('AssetOperation', version='v2')
    asset = asset_operation.create
    #Define the asset type
    asset.type = client.get_type('AssetTypeEnum', version='v2').IMAGE
    asset.image_asset.data.value = image_content
    asset.image_asset.file_size.value = len(image_content)
    asset.image_asset.mime_type = client.get_type('MimeTypeEnum').IMAGE_JPEG
    # Use your favorite image library to determine dimensions
    asset.image_asset.full_size.height_pixels.value = 315
    asset.image_asset.full_size.width_pixels.value = 600
    asset.image_asset.full_size.url.value = URL
    # If you specify the name field, then both the asset name and the image
    # being uploaded should be unique, and should not match another ACTIVE
    # asset in this customer account.
    asset.name = 'Jupiter Trip #' + uuid.uuid4()

    asset_service = client.get_service('AssetService', version='v2')

    try:
        mutate_asset_response = (
            asset_service.mutate_assets(customer_id,
                                        [asset_operation])
        )
        print('Uploaded file(s):')
        for row in mutate_asset_response.results:
            print(f'\tResource name: {row.resource_name}')

    except GoogleAdsException as ex:
        print(f'Request with ID "{ex.request_id}" failed with status '
              f'"{ex.error.code().name}" and includes the following errors:')
        for error in ex.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)


if __name__ == '__main__':
    # GoogleAdsClient will read the google-ads.yaml configuration file in the
    # home directory if none is specified.
    google_ads_client = GoogleAdsClient.load_from_storage()

    parser = argparse.ArgumentParser(description='Upload an image asset from a URL.')
    # The following argument(s) should be provided to run the example.
    parser.add_argument('-c', '--customer_id', type=str, required=True, help='The Google Ads customer ID.')
    args = parser.parse_args()

    main(google_ads_client, args.customer_id)

Google Ads API Forum Advisor Prod

unread,
Feb 10, 2020, 2:41:52 PM2/10/20
to adwor...@googlegroups.com

Hello,

Thank you for reaching out. You need to use AssetService to upload assets to your account but not specific to the campaigns and ad groups. You could then use AdGroupAdService or AdService to mutate ads within the ad groups with these assets. Currently, it is only possible to create assets using Google Ads API Beta. A feature request to support the removal operation is already existing internally within our team. Please keep an eye on our blog post regarding an update related to your concern. Let us know if you have any additional concerns.

Regards,
Nikisha Patel, Google Ads API Team



ref:_00D1U1174p._5001UV0HPC:ref
Reply all
Reply to author
Forward
0 new messages