← 返回目录
来源:Google Ads API 官方文档 · 中文版 · 本地归档

素材资源的制作和使用

大多数素材资源类型都必须先使用 AssetService创建,然后才能在广告中使用。 TextAsset 类型是例外情况,因为它是在广告制作期间内嵌创建的。所有其他类型都必须先上传到广告客户的账号,然后才能使用。

素材资源关联级别

通过将素材资源关联到 Google Ads 账号层次结构中的不同实体,可以使素材资源可供投放。关联级别取决于素材资源类型和广告系列类型。用于管理这些关联的常见资源包括:

要使用的具体服务和资源取决于您要实现的目标。

效果最大化广告系列品牌素材资源

虽然 效果最大化广告系列 中的许多素材资源都是使用 AssetGroupAsset 关联到素材资源 组的,但如果广告系列启用了品牌推广指南,商家名称 和商家徽标 素材资源 则属于例外情况。在这种情况下, 它们会使用 CampaignAsset直接在广告系列一级关联,字段 类型分别为 BUSINESS_NAME 和 LOGO。如果广告系列停用了品牌推广指南,则必须使用 AssetGroupAsset 附加这些素材资源。如需了解详情,请参阅效果最大化广告系列中的 素材资源。

创建素材资源

创建图片和媒体包素材资源时,必须使用唯一名称。如果您提供的名称已存在,系统会在现有名称后附加一个唯一字符串,从而创建一个新名称。我们强烈建议您使用具有描述性的唯一名称,以便在素材资源集合不断扩大的情况下更轻松地管理和识别各个素材资源。

以下示例展示了如何通过原始图片数据的网址创建新的图片素材资源。

Java

private void runExample(GoogleAdsClient googleAdsClient, long customerId) throws IOException {
  byte[] imageData = ByteStreams.toByteArray(new URL(IMAGE_URL).openStream());

  // Create the image asset.
  ImageAsset imageAsset = ImageAsset.newBuilder().setData(ByteString.copyFrom(imageData)).build();

  // Creates an asset.
  Asset asset =
      Asset.newBuilder()
          // Provide a unique friendly name to identify your asset.
          // When there is an existing image asset with the same content but a different name, the
          // new name will be dropped silently.
          .setName("Marketing Image")
          .setType(AssetType.IMAGE)
          .setImageAsset(imageAsset)
          .build();

  // Creates the operation.
  AssetOperation operation = AssetOperation.newBuilder().setCreate(asset).build();

  // Creates the service client.
  try (AssetServiceClient assetServiceClient =
      googleAdsClient.getLatestVersion().createAssetServiceClient()) {
    // Issues a mutate request to add the asset.
    MutateAssetsResponse response =
        assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation));
    // Prints the result.
    System.out.printf(
        "The image asset with resource name '%s' was created.%n",
        response.getResults(0).getResourceName());
  }
}
      

C#

public void Run(GoogleAdsClient client, long customerId)
{
    // Get the AssetServiceClient.
    AssetServiceClient assetService =
        client.GetService(Services.V25.AssetService);

    // Creates an image content.
    byte[] imageContent = MediaUtilities.GetAssetDataFromUrl(IMAGE_URL, client.Config);

    // Creates an image asset.
    ImageAsset imageAsset = new ImageAsset()
    {
        Data = ByteString.CopyFrom(imageContent),
        FileSize = imageContent.Length,
        MimeType = MimeType.ImageJpeg,
        FullSize = new ImageDimension()
        {
            HeightPixels = 315,
            WidthPixels = 600,
            Url = IMAGE_URL
        }
    };

    // Creates an asset.
    Asset asset = new Asset()
    {
        // Optional: Provide a unique friendly name to identify your asset.
        // 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.
        // Name = 'Jupiter Trip #' + ExampleUtilities.GetRandomString(),
        Type = AssetType.Image,
        ImageAsset = imageAsset,
        // Provide a unique friendly name to identify your asset.
        // When there is an existing image asset with the same content but a different
        // name, the new name will be dropped silently.
        Name = "Marketing Image"
    };

    // Creates an asset operation.
    AssetOperation operation = new AssetOperation()
    {
        Create = asset
    };

    try
    {
        // Issues a mutate request to add the asset.
        MutateAssetsResponse response =
            assetService.MutateAssets(customerId.ToString(), new[] { operation });

        // Displays the result.
        Console.WriteLine($"Image asset with resource name: " +
            $"'{response.Results.First().ResourceName}' is created.");
    }
    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 an image content.
    $imageContent = file_get_contents(self::IMAGE_URL);

    // Creates an asset.
    $asset = new Asset([
        // Provide a unique friendly name to identify your asset.
        // When there is an existing image asset with the same content but a different
        // name, the new name will be dropped silently.
        'name' => 'Marketing Image',
        'type' => AssetType::IMAGE,
        'image_asset' => new ImageAsset(['data' => $imageContent])
    ]);

    // Creates an asset operation.
    $assetOperation = new AssetOperation();
    $assetOperation->setCreate($asset);

    // Issues a mutate request to add the asset.
    $assetServiceClient = $googleAdsClient->getAssetServiceClient();
    $response = $assetServiceClient->mutateAssets(MutateAssetsRequest::build(
        $customerId,
        [$assetOperation]
    ));

    if (!empty($response->getResults())) {
        // Prints the resource name of the added image asset.
        /** @var MutateAssetResult $addedImageAsset */
        $addedImageAsset = $response->getResults()[0];
        printf(
            "The image asset with resource name '%s' was created.%s",
            $addedImageAsset->getResourceName(),
            PHP_EOL
        );
    } else {
        print 'No image asset was created.' . PHP_EOL;
    }
}
在接这个接口时卡住了?
Stuck wiring this API into production? We build ad-delivery tooling on these platforms — campaign automation, reporting pipelines, error-handling and migration between API versions.
联系我们 · Talk to us 每日平台更新简报 →