Page Summary
-
This content demonstrates how to create, retrieve details, and set schedules for sitelink extensions using AdsApp scripts.
-
The
createSitelinkfunction illustrates how to build a new sitelink and add it to campaigns, ad groups, or the account level. -
The
logSitelinkDetailsfunction shows how to retrieve and log information like text, URL, mobile preference, clicks, and impressions for sitelinks within a campaign. -
The
setSitelinkSchedulefunction provides an example of how to set a specific schedule for when a sitelink should be displayed.
Create a sitelink extension
function createSitelink () { // For full details on creating a new sitelink extension , see : // https : // developers . google . com / google - ads / scripts / docs / reference / adsapp / adsapp_sitelinkbuilder const newSitelink = AdsApp . extensions () . newSitelinkBuilder () // Replace the values below with your link link text , final url , and // mobile preferred . withLinkText ( 'Music' ) // required . withFinalUrl ( 'http://www.example.com/Music' ) // required . withMobilePreferred ( true ) // optional . build () . getResult (); // Add sitelink to a campaign const campaignIterator = AdsApp . campaigns () . withCondition ( 'campaign.name = "INSERT_CAMPAIGN_NAME_HERE"' ) . get (); if ( campaignIterator . hasNext ()) { const campaign = campaignIterator . next (); campaign . addSitelink ( newSitelink ); } // Add sitelink to an ad group const adGroupIterator = AdsApp . adGroups () . withCondition ( 'campaign.name = "INSERT_CAMPAIGN_NAME_HERE"' ) . withCondition ( 'ad_group.name = "INSERT_AD_GROUP_NAME_HERE"' ) . get (); if ( adGroupIterator . hasNext ()) { const adGroup = adGroupIterator . next (); adGroup . addSitelink ( newSitelink ); } // Add sitelink to an account const account = AdsApp . currentAccount (); account . addSitelink ( newSitelink ); }
Log sitelink details for a campaign
function logSitelinkDetails () { // Get a campaign . const campaignIterator = AdsApp . campaigns () . withCondition ( 'campaign.name = "INSERT_CAMPAIGN_NAME_HERE"' ) . get (); if ( ! campaignIterator . hasNext ()) { throw new Error ( 'Campaign not found.' ); } const campaign = campaignIterator . next (); // Retrieve the campaign 's sitelinks. Retrieving an ad group' s and // account 's sitelinks is similar. const sitelinkIterator = campaign . extensions () . sitelinks () . get (); for ( const sitelink of sitelinkIterator ) { // You can also request reports for pre - defined date ranges . See // https : // developers . google . com / adwords / api / docs / guides / awql , // DateRangeLiteral section for possible values . const stats = sitelink . getStatsFor ( 'LAST_MONTH' ); console . log ( ` Sitelink text : $ { sitelink . getLinkText () } ` ); console . log ( ` final URL : $ { sitelink . urls () . getFinalUrl () } ` ); console . log ( ` mobile preferred : $ { sitelink . isMobilePreferred () } ` ); console . log ( ` clicks : $ { stats . getClicks () } ` ); console . log ( ` impressions : $ { stats . getImpressions () } ` ); console . log ( '=======' ); } console . log ( ` $ { sitelinkIterator . totalNumEntities ()} sitelinks in the campaign ` ); }
Set schedule for sitelinks in a campaign
function setSitelinkSchedule () { // Get a campaign . const campaignIterator = AdsApp . campaigns () . withCondition ( 'campaign.name = "INSERT_CAMPAIGN_NAME_HERE"' ) . get (); if ( ! campaignIterator . hasNext ()) { throw new Error ( 'Campaign not found.' ); } const campaign = campaignIterator . next (); // Retrieve the campaign 's sitelinks. Retrieving an ad group' s and // account 's saitelinks is similar. const sitelinkIterator = campaign . extensions () . sitelinks () . get (); for ( const sitelink of sitelinkIterator ) { if ( sitelink . getLinkText () === 'Music' ) { // Set sitelink schedule to run only on Mondays and Tuesdays , 9 AM to // 6 PM . const monday = { dayOfWeek : 'MONDAY' , startHour : 9 , startMinute : 0 , endHour : 18 , endMinute : 0 }; const tuesday = { dayOfWeek : 'TUESDAY' , startHour : 9 , startMinute : 0 , endHour : 18 , endMinute : 0 }; sitelink . setSchedules ([ monday , tuesday ]); break ; } } }

