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 allows you to 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.

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::V5::Resources::AdGroupAd->new({
      adGroup => $ad_group_resource_name,
      status => Google::Ads::GoogleAds::V5::Enums::AdGroupAdStatusEnum::ENABLED,
      ad     => Google::Ads::GoogleAds::V5::Resources::Ad->new({
          finalUrls => ["https://www.example.com"],
          localAd   => Google::Ads::GoogleAds::V5::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::V5::Common::AdImageAsset->new({
                    asset => create_image_asset(
                      $api_client,         $customer_id,
                      MARKETING_IMAGE_URL, "Marketing Image"
                    )})
              ],
              logoImages => [
                Google::Ads::GoogleAds::V5::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::V5::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::V5::Services::AdGroupAdService::AdGroupAdOperation
    ->new({create => $ad_group_ad});

  # Issue a mutate request to add the ad group ad.
  my $ad_group_ad_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_ad_response->{results}[0]{resourceName};
}

Creating the image asset

Here is the function that creates the ImageAsset:

Perl

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

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

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

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

  # Print out information about the newly added asset.
  my $asset_resource_name = $asset_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:

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::V5::Resources::Asset->new({
      name => $youtube_video_name,
      type => YOUTUBE_VIDEO,
      youtubeVideoAsset =>
        Google::Ads::GoogleAds::V5::Common::YoutubeVideoAsset->new({
          youtubeVideoId => $youtube_video_id
        })});

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

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

  # Print out information about the newly added asset.
  my $asset_resource_name = $asset_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;
}