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

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

C#

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

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

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

C#

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

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

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

C#

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

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;
}