Page Summary
-
Google Ads scripts provide functions to add and manage different types of ads, including expanded text ads, image ads, and responsive display ads.
-
You can pause an ad in an ad group by retrieving the ad and calling the
pause()method. -
Scripts can be used to retrieve iterators for specific ad types within an ad group, such as expanded text ads, text ads, and responsive display ads.
-
Ad statistics for a specific ad group can be retrieved using the
getStatsFor()method on an ad object, specifying a date range.
Add a responsive search ad
function addResponsiveSearchAd ( adGroupName , campaignName ) { const campaignIterator = AdsApp . campaigns () . withCondition ( ` campaign . name = "${campaignName}" ` ) . get (); if ( ! campaignIterator . hasNext ()){ throw new Error ( ` No campaign found with name : "${campaignName}" ` ); } const adGroupIterator = AdsApp . adGroups () . withCondition ( ` ad_group . name = "${adGroupName}" ` ) . get (); if ( ! adGroupIterator . hasNext ()){ throw new Error ( ` No ad group found with name : "${adGroupName}" ` ); } const adGroup = adGroupIterator . next (); adGroup . newAd () . responsiveSearchAdBuilder () . withHeadlines ([ 'First headline' , 'Second headline' , 'Third headline' ]) . withDescriptions ([ 'First description' , 'Second description' ]) . withFinalUrl ( 'http://www.example.com' ) . build (); // ResponsiveSearchAdBuilder has additional options . // For more details , see // https : // developers . google . com / google - ads / scripts / docs / reference / adsapp / adsapp_responsivesearchadbuilder }
Add an image ad
function addImageAd ( adGroupName , imageUrl , imageName ) { const imageBlob = UrlFetchApp . fetch ( imageUrl ) . getBlob (); const mediaOperation = AdsApp . adMedia () . newImageBuilder () . withName ( ` $ { imageName } ` ) . withData ( imageBlob ) . build (); const image = mediaOperation . getResult (); if ( ! image . isSuccessful ()) { throw new Error ( ` Media could not be created from url : "${imageUrl}" ` ) } const adGroupIterator = AdsApp . adGroups () . withCondition ( ` ad_group . name = "${adGroupName}" ` ) . get (); if ( ! adGroupIterator . hasNext ()) { throw new Error ( ` No ad group found with name : "${adGroupName}" ` ); } const adGroup = adGroupIterator . next (); if ( adGroupIterator . totalNumEntities () > 1 ) { console . warn ( ` Multiple ad groups named "${adGroupName}" found . Using the one from campaign "${adGroup.getCampaign().getName()}" . ` ); } const mediaIterator = AdsApp . adMedia () . media () . withCondition ( ` media_file . name = "${imageName}" ` ) . get (); if ( ! mediaIterator . hasNext ()) { throw new Error ( ` No media found with name : "${imageName}" ` ); } const image2 = mediaIterator . next (); adGroup . newAd () . imageAdBuilder () . withName ( 'Ad name' ) . withImage ( image2 ) . withDisplayUrl ( 'http://www.example.com' ) . withFinalUrl ( 'http://www.example.com' ) . build (); // ImageAdBuilder has additional options . // For more details , see // https : // developers . google . com / google - ads / scripts / docs / reference / adsapp / adsapp_imageadbuilder }
Add a responsive display ad
// You create responsive display ads in two steps : // 1. Create or retrieve assets ( marketing images , square marketing images , // optional logos , optional landscape logos , and optional YouTube videos ) // 2. Create the ad . // // The following function assumes you have not already created named assets . function addResponsiveDisplayAd ( campaignName , adGroupName ) { // If you have multiple adGroups with the same name , this snippet will // pick an arbitrary matching ad group each time . In such cases , just // filter on the campaign name as well : // // AdsApp . adGroups () // . withCondition ( ` ad_group . name = "${adGroupName}" ` ) // . withCondition ( ` campaign . name = "${campaignName}" ` ) const adGroupIterator = AdsApp . adGroups () . withCondition ( ` ad_group . name = "${adGroupName}" ` ) . get (); if ( ! adGroupIterator . hasNext ()) { throw new Error ( ` No ad group found with name : $ { adGroupName } ` ); } // If you have already created named image assets , select them like this : // // const marketingImages = []; // const marketingImageIterator = AdsApp . adAssets () // . assets () // . withCondition ( ` ad_group_ad . ad . name IN ( "INSERT_FIRST_ASSET_NAME_HERE" , // "INSERT_SECOND_ASSET_NAME_HERE" ) ` ) // . get (); // while ( marketingImageIterator . hasNext ()) { // marketingImages . push ( marketingImageIterator . next ()); // } const adGroup = adGroupIterator . next (); const adGroupBuilder = adGroup . newAd () . responsiveDisplayAdBuilder () . withBusinessName ( 'Your business name' ) . withFinalUrl ( 'http://www.example.com' ) . withHeadlines ([ 'First headline' , 'Second headline' ]) . withLongHeadline ( 'Long Headline' ) . withDescriptions ( [ 'First description' , 'Second description' , 'Third description' ]); // If you selected assets with a snippet as shown above , then provide those // assets here like this : // // adGroupBuilder = adGroupBuilder . withMarketingImages ( marketingImages ); adGroupBuilder . addMarketingImage ( buildImageAsset ( "rectangular image asset" , "https://goo.gl/3b9Wfh" )) . addSquareMarketingImage ( buildImageAsset ( "square image asset" , "https://goo.gl/mtt54n" )) . build (); // ResponsiveDisplayAdBuilder has additional options . // For more details , see // https : // developers . google . com / google - ads / scripts / docs / reference / adsapp / adsapp_responsivedisplayadbuilder } function buildImageAsset ( assetName , imageUrl ) { const imageBlob = UrlFetchApp . fetch ( imageUrl ) . getBlob (); return AdsApp . adAssets () . newImageAssetBuilder () . withData ( imageBlob ) . withName ( assetName ) . build () . getResult (); }
Pause ad in an ad group
function pauseAdInAdGroup ( adGroupName ) { const adGroupIterator = AdsApp . adGroups () . withCondition ( ` ad_group . name = "${adGroupName}" ` ) . get (); if ( ! adGroupIterator . hasNext ()) { throw new Error ( ` No ad group found with name : "${adGroupName}" ` ); } const adGroup = adGroupIterator . next (); const adsIterator = adGroup . ads () . get (); if ( ! adsIterator . hasNext ()) { throw new Error ( ` No ads found in ad group : "${adGroupName}" ` ); } const ad = adsIterator . next (); ad . pause (); }
Get responsive search ads iterator in an ad group
function getResponsiveSearchAdsIteratorInAdGroup ( adGroupName ) { const adGroupIterator = AdsApp . adGroups () . withCondition ( ` ad_group . name = "${adGroupName}" ` ) . get (); if ( ! adGroupIterator . hasNext ()) { throw new Error ( ` No ad group found with name : "${adGroupName}" ` ); } const adGroup = adGroupIterator . next (); // You can filter for ads of a particular type , using the AdType selector . // See https : // developers . google . com / google - ads / scripts / docs / reference / adsapp / adsapp_ad #getType_0 // for possible values . const responsiveSearchAdsIterator = adGroup . ads () . withCondition ( ` ad_group_ad . ad . type = "RESPONSIVE_SEARCH_AD" ` ) . get (); if ( ! responsiveSearchAdsIterator . hasNext ()) { throw new Error ( ` No responsive search ads found in ad group : "${adGroupName}" ` ); } return responsiveSearchAdsIterator ; }
Get stats for ads in an ad group
function getAdGroupAdStats ( adGroupName ) { const adGroupIterator = AdsApp . adGroups () . withCondition ( ` ad_group . name = "${adGroupName}" ` ) . get (); if ( ! adGroupIterator . hasNext ()) { throw new Error ( ` No ad group found with name : "${adGroupName}" ` ); } const adGroup = adGroupIterator . next (); // If you want to restrict your search to some ads only , then you could // apply a label and retrieve ads as // // const label = AdsApp . labels () // . withCondition ( ` ad_group_ad_label . name = "INSERT_LABEL_NAME_HERE" ` ) // . get () // . next (); // const adsIterator = label . ads () . get (); const adsIterator = adGroup . ads () . get (); if ( ! adsIterator . hasNext ()) { throw new Error ( ` No ads found in ad group : "${adGroupName}" ` ); } const ad = adsIterator . next (); // You can also request reports for pre - defined date ranges . See // https : // developers . google . com / google - ads / api / docs / query / date - ranges // DateRangeLiteral section for possible values . const stats = ad . getStatsFor ( 'LAST_MONTH' ); console . log ( ` $ { adGroup . getName ()}, $ { stats . getClicks ()}, $ { stats . getImpressions ()} ` ); }
Get responsive display ads in an ad group
function getResponsiveDisplayAdIteratorInAdGroup ( adGroupName ) { const adGroupIterator = AdsApp . adGroups () . withCondition ( ` ad_group . name = "${adGroupName}" ` ) . get (); if ( ! adGroupIterator . hasNext ()) { throw new Error ( ` No ad group found with name : $ { adGroupName } ` ); } const adGroup = adGroupIterator . next (); const responsiveDisplayAdsIterator = adGroup . ads () . withCondition ( ` ad_group_ad . ad . type IN ( "RESPONSIVE_DISPLAY_AD" , "LEGACY_RESPONSIVE_DISPLAY_AD" ) ` ) . get (); if ( ! responsiveDisplayAdsIterator . hasNext ()) { throw new Error ( ` No Responsive Display ads found in ad group : "${adGroupName}" ` ); } return responsiveDisplayAdsIterator ; }

