Google Scripts Safety Pattern

Automating your Google Ads accounts is great but does have certain risks, especially when working with bidding and budgets. Luckily there's something you can do about it.

Start Now!
Google Get started!

No script is perfect or perfectly future proof, so you want to be notified ASAP when something goes wrong. Google Ads does not have a built in error notification system and creating your own notification system can be tedious and can clog up your script. Therefore I created this pattern, so you can write safe scripts with no extra effort.

Difficulty

Category
Scripts

Tags

Instructions:

Add your script logic inside the master() function instead of the main() function. The main function will be used for a try/catch which catches all unexpected errors and will automatically email it to you. You can also add custom errors to this by using the err() function multiple times in your script. The same goes for update messages, which are optional. You can easily paste the contents of the main function of your current script to this new pattern and make the script safer.

Further instructions are inside the script:

  • add your logic to master() instead of main()
  • add your logs to msg() instead of console.log()
  • add your (custom) error messages to err()
  • messages, errors and execution errors will be automatically emailed
  • enter email address in config variable
  • set sendMessages true if you want to receive update mails, errs always be sent
The script
/*
 * @name Google Scripts Safety Pattern
 *
 * @author Bas Baudoin
 *
 * @version v1 2022-09-24
 *
 * @instructions
 *  - add your logic to master() instead of main()
 *  - add your logs to msg() instead of console.log()
 *  - add your (custom) error messages to err()
 *  - messages, errors and execution errors will be automatically emailed
 *  - enter email address in config variable
 *  - set sendMessages true if you want to receive update mails, errs always be sent
 */

const config = {
  email: 'bas@example.com',
  sendMessages: false,
}

function master() {
  // add your script logic here
}

// --- start safety logic ---
let msgs = []
let errs = []

function main() {
  try {
    master()
  } catch(e) {
    err(`Main catch: ${e}`)
  }
  handleMessages()
}

function handleMessages() {
  const accountName = AdsApp.currentAccount().getName()
  const errBody = errs.join('\n- ')
  const errSubject = `Errors in script: ${accountName}`
  console.log(`⏩ Sending error mail to ${config.email}`)
  MailApp.sendEmail(config.email, errSubject, errBody)
  
  if (!config.sendMessages || msgs.length === 0) return console.log('⏩ Skipping sending msgs')
  const msgBody = msgs.join('\n- ')
  const msgSubject = `Script updates: ${accountName}`
  console.log(`⏩ Sending update message mail to ${config.email}`)
  MailApp.sendEmail(config.email, msgSubject, msgBody)
}

function err(errMsg) {
  console.log('❌', errMsg)
  errs.push(errMsg)
}

function msg(msg) {
  console.log('⏩', msg)
  msgs.push(msg)
}
// --- end safety logic ---
Show whole script!
Loading Comments
The Experts
Tibbe van Asten Team Lead Performance Marketing
Nils Rooijmans Water Cooler Topics
Martijn Kraan Freelance PPC Specialist
Bas Baudoin Teamlead SEA @ Happy Leads
Jermaya Leijen Digital Marketing Strategist
Krzysztof Bycina PPC Specialist from Poland
How about you? JOIN US!
Sharing Knowledge
Caring

Adsscripts.com is all about sharing knowledge. In the current market, PPC specialists like to keep their knowledge and experience to themselves. We're convinced that sharing knowledge can ensure that everyone gets better at their work. We want to change this by sharing our knowledge about scripts with everyone.

Do you also want to contribute? We are open to new ideas and feedback on everything you find on Adsscripts.com.

Contact us

Training &
Workshop
Contact us!
Adsscripts Training & Workshop