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.V11.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
def create_local_ad(client, customer_id, ad_group_resource_name): """Adds a local ad to the given client account under the given ad group. Args: client: an initialized GoogleAdsClient instance. customer_id: a client customer ID str. ad_group_resource_name: the resource name str for a ad group. """ ad_group_ad_operation = client.get_type("AdGroupAdOperation") ad_group_ad = ad_group_ad_operation.create ad_group_ad.ad_group = ad_group_resource_name ad_group_ad.status = client.enums.AdGroupAdStatusEnum.ENABLED ad_group_ad.ad.final_urls.append("https://www.example.com") ad_group_ad.ad.local_ad.headlines.extend( [ create_ad_text_asset(client, "Best Space Cruise Line"), create_ad_text_asset(client, "Experience the Stars"), ] ) ad_group_ad.ad.local_ad.descriptions.extend( [ create_ad_text_asset(client, "Buy your tickets now"), create_ad_text_asset(client, "Visit the Red Planet"), ] ) ad_group_ad.ad.local_ad.call_to_actions.append( create_ad_text_asset(client, "Shop Now") ) marketing_image = client.get_type("AdImageAsset") marketing_image.asset = create_image_asset( client, customer_id, _MARKETING_IMAGE_URL, "Marketing Image" ) ad_group_ad.ad.local_ad.marketing_images.append(marketing_image) logo_image = client.get_type("AdImageAsset") logo_image.asset = create_image_asset( client, customer_id, _LOGO_IMAGE_URL, "Square Marketing Image" ) ad_group_ad.ad.local_ad.logo_images.append(logo_image) video = client.get_type("AdVideoAsset") video.asset = create_youtube_video_asset( client, customer_id, _YOUTUBE_VIDEO_ID, "Local Campaigns" ) ad_group_ad.ad.local_ad.videos.append(video) ad_group_ad_service = client.get_service("AdGroupAdService") response = ad_group_ad_service.mutate_ad_group_ads( customer_id=customer_id, operations=[ad_group_ad_operation] ) resource_name = response.results[0].resource_name print(f"Created ad group ad with resource name: '{resource_name}'") def create_ad_text_asset(client, text): """Creates an ad text asset with the given text value. Args: client: an initialized GoogleAdsClient instance. text: a str for the text value of the ad text asset. Returns: an ad text asset. """ ad_text_asset = client.get_type("AdTextAsset") ad_text_asset.text = text return ad_text_asset
Ruby
def create_local_ad(client, customer_id, ad_group_resource_name) # Creates an ad group ad operation. operation = client.operation.create_resource.ad_group_ad do |aga| aga.ad_group = ad_group_resource_name aga.status = :ENABLED aga.ad = client.resource.ad do |ad| ad.final_urls << "https://www.example.com" ad.local_ad = client.resource.local_ad_info do |lai| lai.headlines << client.resource.ad_text_asset do |ata| ata.text = "Best Space Cruise Line" end lai.headlines << client.resource.ad_text_asset do |ata| ata.text = "Experience the Stars" end lai.descriptions << client.resource.ad_text_asset do |ata| ata.text = "Buy your tickets now" end lai.descriptions << client.resource.ad_text_asset do |ata| ata.text = "Visit the Red Planet" end lai.call_to_actions << client.resource.ad_text_asset do |ata| ata.text = "Shop Now" end # Sets the marketing image and logo image assets. lai.marketing_images << client.resource.ad_image_asset do |aia| aia.asset = create_image_asset( client, customer_id, MARKETING_IMAGE_URL, "Marketing Image") end lai.logo_images << client.resource.ad_image_asset do |aia| aia.asset = create_image_asset( client, customer_id, LOGO_IMAGE_URL, "Square Marketing Image") end # Sets the video assets. lai.videos << client.resource.ad_video_asset do |aia| aia.asset = create_youtube_video_asset( client, customer_id, YOUTUBE_VIDEO_ID, "Local Campaigns") end end end end # Issues a mutate request to add the ad group ad. response = client.service.ad_group_ad.mutate_ad_group_ads( customer_id: customer_id, operations: [operation], ) puts "Created ad group ad with resource name: " \ "'#{response.results.first.resource_name}'." end
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::V11::Resources::AdGroupAd->new({ adGroup => $ad_group_resource_name, status => Google::Ads::GoogleAds::V11::Enums::AdGroupAdStatusEnum::ENABLED, ad => Google::Ads::GoogleAds::V11::Resources::Ad->new({ finalUrls => ["https://www.example.com"], localAd => Google::Ads::GoogleAds::V11::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::V11::Common::AdImageAsset->new({ asset => create_image_asset( $api_client, $customer_id, MARKETING_IMAGE_URL, "Marketing Image" )}) ], logoImages => [ Google::Ads::GoogleAds::V11::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::V11::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::V11::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.V11.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
def create_image_asset(client, customer_id, image_url, image_name): """Creates an asset with the given image URL and name. Args: client: an initialized GoogleAdsClient instance. customer_id: a client customer ID str. image_url: a str URL to download an image. image_name: a str to use to name the image. Returns: an asset. """ asset_operation = client.get_type("AssetOperation") asset = asset_operation.create asset.name = image_name asset.type_ = client.enums.AssetTypeEnum.IMAGE asset.image_asset.data = get_image_bytes(image_url) asset_service = client.get_service("AssetService") response = asset_service.mutate_assets( customer_id=customer_id, operations=[asset_operation] ) resource_name = response.results[0].resource_name print( "A new image asset has been added with resource name: " f"'{resource_name}'" ) return resource_name def get_image_bytes(url): """Loads image data from a URL. Args: url: a URL str. Returns: Images bytes loaded from the given URL. """ response = requests.get(url) return response.content
Ruby
def create_image_asset(client, customer_id, image_url, image_name) # Creates an asset operation. operation = client.operation.create_resource.asset do |a| a.name = image_name a.type = :IMAGE a.image_asset = client.resource.image_asset do |ia| ia.data = open(image_url) { |f| f.read } end end # Issues a mutate request to add the asset. response = client.service.asset.mutate_assets( customer_id: customer_id, operations: [operation], ) # Prints out information about the newly added asset. asset_resource_name = response.results.first.resource_name puts "A new image asset has been added with resource name: " \ "'#{asset_resource_name}'." asset_resource_name end
Perl
sub create_image_asset { my ($api_client, $customer_id, $image_url, $image_name) = @_; # Create an asset. my $asset = Google::Ads::GoogleAds::V11::Resources::Asset->new({ name => $image_name, type => IMAGE, imageAsset => Google::Ads::GoogleAds::V11::Common::ImageAsset->new({ data => get_base64_data_from_url($image_url)})}); # Create an asset operation. my $asset_operation = Google::Ads::GoogleAds::V11::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.V11.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
def create_youtube_video_asset( client, customer_id, youtube_video_id, youtube_video_name ): """Creates a asset with the given YouTube video ID and name. Args: client: an initialized GoogleAdsClient instance. customer_id: a client customer ID str. youtube_video_id: a str of a YouTube video ID. youtube_video_name: a str to use for the name of the video asset. Returns: an Asset. """ asset_operation = client.get_type("AssetOperation") asset = asset_operation.create asset.name = youtube_video_name asset.type_ = client.enums.AssetTypeEnum.YOUTUBE_VIDEO asset.youtube_video_asset.youtube_video_id = youtube_video_id asset_service = client.get_service("AssetService") response = asset_service.mutate_assets( customer_id=customer_id, operations=[asset_operation] ) resource_name = response.results[0].resource_name print( "A new YouTube video asset has been added with resource name: " f"'{resource_name}'" ) return resource_name
Ruby
def create_youtube_video_asset( client, customer_id, youtube_video_id, youtube_video_name ) # Creates an asset operation. operation = client.operation.create_resource.asset do |a| a.name = youtube_video_name a.type = :YOUTUBE_VIDEO a.youtube_video_asset = client.resource.youtube_video_asset do |yva| yva.youtube_video_id = youtube_video_id end end # Issues a mutate request to add the asset. response = client.service.asset.mutate_assets( customer_id: customer_id, operations: [operation], ) # Prints out information about the newly added asset. asset_resource_name = response.results.first.resource_name puts "A new YouTube video asset has been added with resource name: " \ "'#{asset_resource_name}'." asset_resource_name end
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::V11::Resources::Asset->new({ name => $youtube_video_name, type => YOUTUBE_VIDEO, youtubeVideoAsset => Google::Ads::GoogleAds::V11::Common::YoutubeVideoAsset->new({ youtubeVideoId => $youtube_video_id })}); # Create an asset operation. my $asset_operation = Google::Ads::GoogleAds::V11::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; }