Create a Local Ad

To create a local ad, you first create a new LocalAdInfo and then assign it to an AdGroupAd. You can create only one local ad in each ad group.

LocalAdInfo lets you reuse assets previously created in your account to set many of its fields, such as:

  • headlines
  • descriptions
  • call_to_actions
  • marketing_images
  • logo_images
  • videos

Refer to LocalAdInfo for specs on texts, images, and videos that can be used to create an asset for each field. For more details about the required assets, see Create a Local campaign.

Code example

Creating the local ad

The following snippet creates a local ad, along with instantiating text, image, and video assets, and then assigning the ad to a new ad group ad.

Java

private void createLocalAd(
    GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName)
    throws IOException {
  // Creates the local ad object.
  Ad ad =
      Ad.newBuilder()
          .addFinalUrls("https://www.example.com")
          .setLocalAd(
              LocalAdInfo.newBuilder()
                  // Adds headline text to the ad.
                  .addHeadlines(createTextAsset("Best Space Cruise Line"))
                  .addHeadlines(createTextAsset("Experience the Stars"))

                  // Adds description text to the ad.
                  .addDescriptions(createTextAsset("Buy your tickets now"))
                  .addDescriptions(createTextAsset("Visit the Red Planet"))

                  // Adds a call to action text to the ad.
                  .addCallToActions(createTextAsset("Shop Now"))

                  // Adds various rich media types to the ad.
                  .addMarketingImages(
                      createImageAsset(
                          googleAdsClient, customerId, MARKETING_IMAGE_URL, "Marketing Image"))
                  .addLogoImages(
                      createImageAsset(
                          googleAdsClient, customerId, LOGO_IMAGE_URL, "Square Marketing Image"))
                  .addVideos(
                      createYoutubeVideoAsset(
                          googleAdsClient, customerId, YOUTUBE_VIDEO_ID, "Local Campaigns"))
                  .build())
          .build();

  // Creates the ad group ad object.
  AdGroupAd adGroupAd =
      AdGroupAd.newBuilder()
          .setAdGroup(adGroupResourceName)
          .setStatus(AdGroupAdStatus.ENABLED)
          .setAd(ad)
          .build();

  // Creates an operation to add the AdGroupAd.
  AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build();

  // Connects to the API.
  try (AdGroupAdServiceClient client =
      googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) {
    // Issues the mutate request.
    MutateAdGroupAdsResponse response =
        client.mutateAdGroupAds(String.valueOf(customerId), ImmutableList.of(operation));

    // Prints some debugging information.
    String resourceName = response.getResults(0).getResourceName();
    System.out.printf("Created ad group ad with resource name: '%s'.%n", resourceName);
  }
}
      

C#

private void CreateLocalAd(GoogleAdsClient client, in long customerId,
    string adGroupResourceName)
{
    // Create the ad group service client.
    AdGroupAdServiceClient adGroupAdServiceClient =
        client.GetService(Services.V6.AdGroupAdService);

    // Create an ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd
    {
        AdGroup = adGroupResourceName,
        Status = AdGroupAdStatus.Enabled,
        Ad = new Ad
        {
            FinalUrls = {"https://www.example.com"},
            LocalAd = new LocalAdInfo
            {
                Headlines =
                {
                    CreateAdTextAsset("Best Space Cruise Line"),
                    CreateAdTextAsset("Experience the Stars")
                },
                Descriptions =
                {
                    CreateAdTextAsset("Buy your tickets now"),
                    CreateAdTextAsset("Visit the Red Planet")
                },
                CallToActions = {CreateAdTextAsset("Shop Now")},
                // Set the marketing image and logo image assets.
                MarketingImages =
                {
                    new AdImageAsset
                    {
                        Asset = CreateImageAsset(client, customerId, MARKETING_IMAGE_URL,
                            "Marketing Image")
                    }
                },
                LogoImages =
                {
                    new AdImageAsset
                    {
                        Asset = CreateImageAsset(client, customerId, LOGO_IMAGE_URL,
                            "Square Marketing Image")
                    }
                },
                // Set the video assets.
                Videos =
                {
                    new AdVideoAsset
                    {
                        Asset = CreateYoutubeVideoAsset(client, customerId,
                            YOUTUBE_VIDEO_ID, "Local Campaigns")
                    }
                }
            }
        }
    };

    // Create an ad group ad operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation
    {
        Create = adGroupAd
    };

    // Issue a mutate request to add the ad group ad and print the resulting ad group ad's
    // resource name.
    MutateAdGroupAdsResponse adGroupAdResponse =
        adGroupAdServiceClient.MutateAdGroupAds(customerId.ToString(),
            new[] {adGroupAdOperation});

    Console.WriteLine("Created ad group ad with resource name " +
        $"'{adGroupAdResponse.Results.First().ResourceName}'.");
}
      

PHP

private static function createLocalAd(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $adGroupResourceName
) {
    $adGroupAd = new AdGroupAd([
        'ad_group' => $adGroupResourceName,
        'status' => AdGroupAdStatus::ENABLED,
        'ad' => new Ad([
            'final_urls' => ['https://www.example.com'],
            'local_ad' => new LocalAdInfo([
                'headlines' => [
                    new AdTextAsset(['text' => 'Best Space Cruise Line']),
                    new AdTextAsset(['text' => 'Experience the Stars'])
                ],
                'descriptions' => [
                    new AdTextAsset(['text' => 'Buy your tickets now']),
                    new AdTextAsset(['text' => 'Visit the Red Planet'])
                ],
                'call_to_actions' => [new AdTextAsset(['text' => 'Shop Now'])],
                // Sets the marketing image and logo image assets.
                'marketing_images' => [new AdImageAsset(['asset' => self::createImageAsset(
                    $googleAdsClient,
                    $customerId,
                    self::MARKETING_IMAGE_URL,
                    'Marketing Image'
                )])],
                'logo_images' => [new AdImageAsset(['asset' => self::createImageAsset(
                    $googleAdsClient,
                    $customerId,
                    self::LOGO_IMAGE_URL,
                    'Square Marketing Image'
                )])],
                // Sets the video assets.
                'videos' => [new AdVideoAsset(['asset' => self::createYoutubeVideoAsset(
                    $googleAdsClient,
                    $customerId,
                    self::YOUTUBE_VIDEO_ID,
                    'Local Campaigns'
                )])]
            ])
        ])
    ]);

    // Creates an ad group ad operation.
    $adGroupAdOperation = new AdGroupAdOperation();
    $adGroupAdOperation->setCreate($adGroupAd);

    // Issues a mutate request to add the ad group ad.
    $adGroupAdServiceClient = $googleAdsClient->getAdGroupAdServiceClient();
    /** @var MutateAdGroupAdsResponse $adGroupAdResponse */
    $adGroupAdResponse = $adGroupAdServiceClient->mutateAdGroupAds(
        $customerId,
        [$adGroupAdOperation]
    );

    printf(
        "Created ad group ad with resource name: '%s'.%s",
        $adGroupAdResponse->getResults()[0]->getResourceName(),
        PHP_EOL
    );
}
      

Python

This example is not yet available in Python; you can take a look at the other languages.
    

Ruby

This example is not yet available in Ruby; you can take a look at the other languages.
    

Perl

sub create_local_ad {
  my ($api_client, $customer_id, $ad_group_resource_name) = @_;

  # Create an ad group ad.
  my $ad_group_ad = Google::Ads::GoogleAds::V6::Resources::AdGroupAd->new({
      adGroup => $ad_group_resource_name,
      status => Google::Ads::GoogleAds::V6::Enums::AdGroupAdStatusEnum::ENABLED,
      ad     => Google::Ads::GoogleAds::V6::Resources::Ad->new({
          finalUrls => ["https://www.example.com"],
          localAd   => Google::Ads::GoogleAds::V6::Common::LocalAdInfo->new({
              headlines => [
                create_ad_text_asset("Best Space Cruise Line"),
                create_ad_text_asset("Experience the Stars")
              ],
              descriptions => [
                create_ad_text_asset("Buy your tickets now"),
                create_ad_text_asset("Visit the Red Planet")
              ],
              callToActions => [create_ad_text_asset("Shop Now")],
              # Set the marketing image and logo image assets.
              marketingImages => [
                Google::Ads::GoogleAds::V6::Common::AdImageAsset->new({
                    asset => create_image_asset(
                      $api_client,         $customer_id,
                      MARKETING_IMAGE_URL, "Marketing Image"
                    )})
              ],
              logoImages => [
                Google::Ads::GoogleAds::V6::Common::AdImageAsset->new({
                    asset => create_image_asset(
                      $api_client,    $customer_id,
                      LOGO_IMAGE_URL, "Square Marketing Image"
                    )})
              ],
              # Set the video assets.
              videos => [
                Google::Ads::GoogleAds::V6::Common::AdVideoAsset->new({
                    asset => create_youtube_video_asset(
                      $api_client,      $customer_id,
                      YOUTUBE_VIDEO_ID, "Local Campaigns"
                    )})]})})}

  );

  # Create an ad group ad operation.
  my $ad_group_ad_operation =
    Google::Ads::GoogleAds::V6::Services::AdGroupAdService::AdGroupAdOperation
    ->new({create => $ad_group_ad});

  # Issue a mutate request to add the ad group ad.
  my $ad_group_ads_response = $api_client->AdGroupAdService()->mutate({
      customerId => $customer_id,
      operations => [$ad_group_ad_operation]});

  printf "Created ad group ad with resource name: '%s'.\n",
    $ad_group_ads_response->{results}[0]{resourceName};
}
      

Creating the image asset

Here is the function that creates the ImageAsset:

Java

private static AdImageAsset createImageAsset(
    GoogleAdsClient googleAdsClient, long customerId, String imageUrl, String imageName)
    throws IOException {
  // Creates a media file.
  byte[] assetBytes = ByteStreams.toByteArray(new URL(imageUrl).openStream());
  Asset asset =
      Asset.newBuilder()
          .setName(imageName)
          .setType(AssetType.IMAGE)
          .setImageAsset(ImageAsset.newBuilder().setData(ByteString.copyFrom(assetBytes)).build())
          .build();

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

  // Creates the asset service client.
  try (AssetServiceClient assetServiceClient =
      googleAdsClient.getLatestVersion().createAssetServiceClient()) {
    // Adds the image asset.
    MutateAssetsResponse response =
        assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation));
    String imageResourceName = response.getResults(0).getResourceName();
    System.out.printf("Created image asset with resource name '%s'.%n", imageResourceName);

    // Wraps the asset resource name in AdImageAsset.
    return AdImageAsset.newBuilder().setAsset(imageResourceName).build();
  }
}
      

C#

private string CreateImageAsset(GoogleAdsClient client, long customerId, string imageUrl,
    string imageName)
{
    // Get the AssetService client.
    AssetServiceClient assetService = client.GetService(Services.V6.AssetService);

    // Creates an image asset.
    byte[] imageContent = MediaUtilities.GetAssetDataFromUrl(imageUrl, client.Config);
    Asset asset = new Asset
    {
        Name = imageName,
        Type = AssetType.Image,
        ImageAsset = new ImageAsset
        {
            Data = ByteString.CopyFrom(imageContent),
        }
    };

    // Create an asset operation.
    AssetOperation assetOperation = new AssetOperation
    {
        Create = asset
    };

    // Issue a mutate request to add the asset, then print and return the resulting asset's
    // resource name.
    MutateAssetsResponse assetResponse =
        assetService.MutateAssets(customerId.ToString(), new[] {assetOperation});

    string assetResourceName = assetResponse.Results.First().ResourceName;
    Console.WriteLine("A new image asset has been added with resource name: " +
        $"'{assetResourceName}'.");

    return assetResourceName;
}
      

PHP

private static function createImageAsset(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $imageUrl,
    string $imageName
) {
    // Creates an asset.
    $asset = new Asset([
        'name' => $imageName,
        'type' => AssetType::IMAGE,
        'image_asset' => new ImageAsset(['data' => file_get_contents($imageUrl)])
    ]);

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

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

    // Prints out information about the newly added asset.
    $assetResourceName = $response->getResults()[0]->getResourceName();
    printf(
        "A new image asset has been added with resource name: '%s'.%s",
        $assetResourceName,
        PHP_EOL
    );

    return $assetResourceName;
}
      

Python

This example is not yet available in Python; you can take a look at the other languages.
    

Ruby

This example is not yet available in Ruby; you can take a look at the other languages.
    

Perl

sub create_image_asset {
  my ($api_client, $customer_id, $image_url, $image_name) = @_;

  # Create an asset.
  my $asset = Google::Ads::GoogleAds::V6::Resources::Asset->new({
      name       => $image_name,
      type       => IMAGE,
      imageAsset => Google::Ads::GoogleAds::V6::Common::ImageAsset->new({
          data => get_base64_data_from_url($image_url)})});

  # Create an asset operation.
  my $asset_operation =
    Google::Ads::GoogleAds::V6::Services::AssetService::AssetOperation->new({
      create => $asset
    });

  # Issue a mutate request to add the asset.
  my $assets_response = $api_client->AssetService()->mutate({
      customerId => $customer_id,
      operations => [$asset_operation]});

  # Print out information about the newly added asset.
  my $asset_resource_name = $assets_response->{results}[0]{resourceName};
  printf "A new image asset has been added with resource name: '%s'.\n",
    $asset_resource_name;

  return $asset_resource_name;
}
      

Creating the YouTube video asset

Here is the function that creates the YoutubeVideoAsset:

Java

private AdVideoAsset createYoutubeVideoAsset(
    GoogleAdsClient googleAdsClient, long customerId, String youtubeVideoId, String videoName) {
  // Creates an Asset object.
  Asset asset =
      Asset.newBuilder()
          .setName(videoName)
          .setType(AssetType.YOUTUBE_VIDEO)
          .setYoutubeVideoAsset(YoutubeVideoAsset.newBuilder().setYoutubeVideoId(youtubeVideoId))
          .build();

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

  // Connects to the API.
  try (AssetServiceClient assetServiceClient =
      googleAdsClient.getLatestVersion().createAssetServiceClient()) {
    // Issues the mutate request.
    MutateAssetsResponse response =
        assetServiceClient.mutateAssets(String.valueOf(customerId), ImmutableList.of(operation));

    // Prints some debugging information.
    String resourceName = response.getResults(0).getResourceName();
    System.out.printf(
        "A new YouTube video asset has been added with resource name: '%s'.%n", resourceName);

    // Wraps the result in an AdVideoAsset.
    return AdVideoAsset.newBuilder().setAsset(resourceName).build();
  }
}
      

C#

private string CreateYoutubeVideoAsset(GoogleAdsClient client, in long customerId,
    string youtubeVideoId, string youtubeVideoName)
{
    // Get the AssetService client.
    AssetServiceClient assetServiceClient = client.GetService(Services.V6.AssetService);

    // Create an asset.
    Asset asset = new Asset
    {
        Name = youtubeVideoName,
        Type = AssetType.YoutubeVideo,
        YoutubeVideoAsset = new YoutubeVideoAsset
        {
            YoutubeVideoId = youtubeVideoId
        }
    };

    // Create an asset operation.
    AssetOperation assetOperation = new AssetOperation
    {
        Create = asset
    };

    // Issue a mutate request to add the asset, then print and return the resulting asset's
    // resource name.
    MutateAssetsResponse assetResponse =
        assetServiceClient.MutateAssets(customerId.ToString(), new[] {assetOperation});

    string assetResourceName = assetResponse.Results.First().ResourceName;
    Console.WriteLine("A new YouTube video asset has been added with resource name: " +
        $"'{assetResourceName}'.");

    return assetResourceName;
}
      

PHP

private static function createYoutubeVideoAsset(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $youtubeVideoId,
    string $youtubeVideoName
) {
    // Creates an asset.
    $asset = new Asset([
        'name' => $youtubeVideoName,
        'type' => AssetType::YOUTUBE_VIDEO,
        'youtube_video_asset' => new YoutubeVideoAsset(['youtube_video_id' => $youtubeVideoId])
    ]);

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

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

    // Prints out information about the newly added asset.
    $assetResourceName = $response->getResults()[0]->getResourceName();
    printf(
        "A new YouTube video asset has been added with resource name: '%s'.%s",
        $assetResourceName,
        PHP_EOL
    );

    return $assetResourceName;
}
      

Python

This example is not yet available in Python; you can take a look at the other languages.
    

Ruby

This example is not yet available in Ruby; you can take a look at the other languages.
    

Perl

sub create_youtube_video_asset {
  my ($api_client, $customer_id, $youtube_video_id, $youtube_video_name) = @_;

  # Create an asset.
  my $asset = Google::Ads::GoogleAds::V6::Resources::Asset->new({
      name => $youtube_video_name,
      type => YOUTUBE_VIDEO,
      youtubeVideoAsset =>
        Google::Ads::GoogleAds::V6::Common::YoutubeVideoAsset->new({
          youtubeVideoId => $youtube_video_id
        })});

  # Create an asset operation.
  my $asset_operation =
    Google::Ads::GoogleAds::V6::Services::AssetService::AssetOperation->new({
      create => $asset
    });

  # Issue a mutate request to add the asset.
  my $assets_response = $api_client->AssetService()->mutate({
      customerId => $customer_id,
      operations => [$asset_operation]});

  # Print out information about the newly added asset.
  my $asset_resource_name = $assets_response->{results}[0]{resourceName};
  printf "A new YouTube video asset has been added with resource name: '%s'.\n",
    $asset_resource_name;

  return $asset_resource_name;
}