-
-
Save ryanrobertsname/a0600054c71ae253410a87533f86e4c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adwords Uptime Robot Monitoring script | |
* | |
* Script that checks whether the websites that the Adword campaigns rely on are still live | |
* and respond fast enough, or if not, pause the related campaigns. | |
* | |
* Services used to determine uptime: | |
* - Uptime Robot | |
* | |
* @version 0.1.0 | |
* @date 16/11/2016 | |
* @author codeorelse | |
* | |
*/ | |
var domains = ['http://www.yoursite.com']; | |
var businessHours = [8, 17]; | |
var uptimeRobotKey = 'xxxxxxxxxxxxx'; | |
function isDuringBusinessHours() { | |
var today = new Date(); | |
var day = today.getDay(); | |
var hour = today.getUTCHours() + 1; // UTC | |
if ([0, 6].indexOf(day) > -1) return false; // weekend | |
return hour >= businessHours[0] && hour < businessHours[1]; | |
} | |
function pauseCampaign() { | |
var campaignIterator = AdWordsApp.campaigns().get(); | |
while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name | |
var campaign = campaignIterator.next(); | |
if (campaignNames.length === 0 || campaignNames.indexOf(campaign.getName()) > -1) { | |
if (!campaign.isPaused()) { | |
campaign.applyLabel('campaign_paused_by_code'); | |
campaign.pause(); | |
Logger.log("campaign " + campaign.getName() + " was paused."); | |
} | |
} | |
} | |
if (pausedCampaigns.length > 0) { | |
sendMail('Campaigns in account ' + accountName + ' paused', 'Paused: \n' + pausedCampaigns.join('\n')); | |
} | |
} | |
function enableCampaign() { | |
var campaignIterator = AdWordsApp.campaigns().get(); | |
while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name | |
var campaign = campaignIterator.next(); | |
var labelIter = campaign.labels().withCondition("Name = 'campaign_paused_by_code'").get(); | |
if(labelIter.hasNext()) { | |
campaign.enable(); | |
campaign.removeLabel('campaign_paused_by_code'); | |
Logger.log("campaign " + campaign.getName() + " was enabled."); | |
} | |
} | |
} | |
function main() { | |
if (isDuringBusinessHours()) { | |
// Don't run the script when we're during office hours | |
return; | |
} | |
var response = UrlFetchApp.fetch('https://api.uptimerobot.com/getMonitors?apiKey='+uptimeRobotKey+'&format=xml'); | |
if (response.getResponseCode() != 200) { | |
throw Utilities.formatString( | |
'Error returned by API: %s, Location searched: %s.', | |
response.getContentText(), location); | |
} | |
var document = XmlService.parse(response.getContentText()); | |
var root = document.getRootElement(); | |
var entries = root.getChildren('monitor'); | |
var hasDownDomain = false; | |
for (var i = 0; i < entries.length; i++) { | |
var url = entries[i].getAttribute('url').getValue(); | |
var status = entries[i].getAttribute('status').getValue(); | |
var looksDown = status == '9'; | |
if (looksDown && domains.indexOf(url) > -1) { | |
hasDownDomain = true; | |
Logger.log(url + ' is down. We\'re going to pause the campaigns'); | |
} | |
} | |
if (hasDownDomain) { | |
pauseCampaign(); | |
} else { | |
enableCampaign(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment