Send AdWords alerts directly to Slack with this AdWords script library

Looking to get those AdWords alerts out of your email inbox? Columnist Russell Savage shows how you can use AdWords scripts to send these alerts to Slack instead.

Ugh, email. For years, we’ve tried to get rid of it, but it remains the communication tool of choice, with more than 205 billion emails a day sent in 2015. It’s not going away, but at least at the companies where I’ve worked, many of the alerting and monitoring emails have been replaced with Slack notifications.

One of the best parts of Slack is the huge number of apps and integrations that allow you to run commands and push notifications into the system. Alerts that would normally clutter your inbox are now pushed to the team so that everyone can see and respond if needed.

AdWords alerting pushed to Slack

One of the useful features of AdWords scripts is alerting you when something is going wrong in your account. For example, longtime AdWords script advocate Frederick Vallaeys recently wrote a script to notify you if your account has stopped receiving impressions — extremely handy, until the person it is sending an email to is out on vacation.

In this post, I’m going to share a library you can add to your script that will enable you to send Slack alerts to your #marketing channel when things are going wrong in your account. Here is the full code; below, I’ll explain how to set it up.

/***********************************
* Slack Notifications API – v1.0
* By: Russ Savage (@russellsavage)
* Usage:
* var slack = new SlackAPI({
* webhookUrl : “https://hooks.slack.com/services/…/…/…”
* });
* slack.sendMessage(‘This is a test’);
* slack.sendSimpleMessage({
* channel: “#general”,
* username: “webhookbot”,
* text: “This is posted to #general and comes from a bot named webhookbot.”,
* icon_emoji: “:ghost:”
* });
*
* More complex messages or attachements can be sent as well. For more info,
* check out https://api.slack.com/docs/formatting or
* https://api.slack.com/docs/attachments
***********************************/
function SlackAPI(config) {
this.webhookUrl = config.webhookUrl;
// Send a message to slack. The config can
// be as simple as a string or an object
// for passing more complex messages.
this.sendMessage = function(config) {
if(typeof config == ‘object’) {
postToSlack(this.webhookUrl, config);
} else {
postToSlack(this.webhookUrl, { text : config });
}
};
// Take care of all the messy stuff like
// retries and status codes.
function postToSlack(url, payload) {
var options = {
method: ‘POST’,
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
var retries = 3;
while(retries > 0) {
try {
var resp = UrlFetchApp.fetch(url,options);
if(resp.getResponseCode() == 200) {
return true;
} else {
Logger.log(
Utilities.formatString(
“WARNING: Slack returned status code of %s and a message of: %s”,
resp.getResponseCode(),
resp.getContentText()
)
);
Logger.log(‘Waiting 1 seconds then retrying…’);
Utilities.sleep(1000);
retries;
}
} catch(e) {
Logger.log(“ERROR: Something failed in UrlFetchApp. Retrying in 1 second…”);
Utilities.sleep(1000);
retries;
}
}
throw “Either UrlFetchApp is broken or the Slack Webhook is not configured properly.”;
}
};

Configuring Slack for incoming webhooks

To use this library, you first need to enable the incoming webhooks integration in your Slack installation. You might need to talk to your Slack admin to enable this.

Once you have the custom webhook enabled, you should find a URL that looks something like this: https://hooks.slack.com/services/<SOME-ID>/<ANOTHER-ID>/<ONE-MORE-ID>

That’s the webhookUrl that you pass into the SlackAPI library when you create a new instance of it.

This is an incredibly simple library that only has one function: sendMessage. Here’s how you can send simple messages to Slack.

function main() {
var slack = new SlackAPI({
webhookUrl : “https://hooks.slack.com/services/…”
});
slack.sendMessage(‘This is a test’);
slack.sendMessage({
channel: “#marketing”,
username: “adwords-bot”,
text: “This is posted to #marketing and comes from a bot named adwords-bot.”,
icon_emoji: “:ghost:”
});
}

You can find more information on formatting messages on the Slack API documentation page.

Attachments

If you want to make your messages look a little better, you’ll probably want to play around with attachments. Here’s what it might look like in your Slack channel and the code to generate it.

Sample Slack Alert

function main() {
var slack = new SlackAPI({
webhookUrl : “https://hooks.slack.com/services/…”
});
slack.sendMessage({
“text”: “Your AdWords account has not had any impressions in the past hour!”,
“channel”:“#marketing”,
“icon_url” : “https://ssl.gstatic.com/awfe30/aw3_cm_20160229_030026_RC4/cm/18EFA99EA70FA5326D7FAB2041DA3661.cache.png”,
“attachments”: [
{
“fallback”: “Your AdWords account has not had any impressions in the past hour!”,
“color”: “danger”,
“fields”: [
{
“title”: “Account”,
“value”: AdWordsApp.currentAccount().getName(),
“short”: true
},
{
“title”: “Priority”,
“value”: “Critical”,
“short”: true
}
]
}
]
});
}

This is just one example. You can (and probably should) add links to any supporting reports and to the AdWords login, as well.

Conclusion

By adding this library to the end of any script, you can instantly enable notifications from AdWords Scripts to any Slack channel. You can use this anywhere you are using the MailApp.sendEmail function in your scripting.

I hope you find this as useful as I do as a way to remove some of those alerts from your inbox. Happy Slacking!

AdWords Scripts Workshops from Google

One final note: The AdWords Scripts Team at Google has added a few more workshops to help you get started with scripts. They are free and, in my opinion, extremely helpful. If you want to improve the scripts you use now or develop some on your own, I highly recommend this training.