Created
September 24, 2012 22:02
-
-
Save cwiederspan/3778722 to your computer and use it in GitHub Desktop.
Adwords automated Javacript file that enables or pauses Adwords ads based on a tags within the destination URL. We use the file to enable/pause a series of "countdown" ads for a dated event. The data driver for the tags is a simple Google Docs spreadsheet
This file contains hidden or 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
| // AUTHOR: Chris Wiederspan on 09/24/2012 | |
| // PURPOSE: Enable/Pause countdown ads based on a schedule provided in a Google Docs spreadsheet | |
| // DATA FILE: A sample of the data driver file can be found here: http://bit.ly/Ts7Dih | |
| function main() { | |
| // Put the URL to the file that contains the data to be used | |
| var SPREADSHEET_URL = "https://docs.google.com/spreadsheet/ccc?key=0At0IH9OYviWWdFVqa0FBVU5RR2dpQ3dPMkdJRnBoLUE#gid=0"; | |
| // Get today's date | |
| var date = getFormattedDate(); | |
| // DEBUG: date = "20120925"; | |
| Logger.log("Today's Date:" + date); | |
| // Read the schedule from a spreadsheet stored in Google Docs | |
| var spreadsheetData = getSpreadsheetData(SPREADSHEET_URL); | |
| Logger.log("Spreadsheet Data: " + spreadsheetData); | |
| // Use the schedule and today's date to figure out which ads should be showing | |
| var tagToShow = getCountdownTagToShow(spreadsheetData, date); | |
| Logger.log("Tag to Show: " + tagToShow); | |
| // Pause any enabled countdown ads that should be paused | |
| var pauseIterator = AdWordsApp | |
| .ads() | |
| .withCondition("DestinationUrl CONTAINS 'cntdwn'") | |
| .withCondition("DestinationUrl DOES_NOT_CONTAIN '" + tagToShow + "'") | |
| .withCondition("Status = ENABLED") | |
| .get(); | |
| // Loop through and either pause or enable the appropriate ads | |
| while (pauseIterator.hasNext()) { | |
| var ad = pauseIterator.next(); | |
| Logger.log("PAUSE : " + ad.getDestinationUrl()); | |
| ad.pause(); | |
| } | |
| // Enable any paused ads that should be enabled | |
| var activateIterator = AdWordsApp | |
| .ads() | |
| .withCondition("DestinationUrl CONTAINS '" + tagToShow + "'") | |
| .withCondition("Status = PAUSED") | |
| .get(); | |
| // Loop through and either pause or enable the appropriate ads | |
| while (activateIterator.hasNext()) { | |
| var ad = activateIterator.next(); | |
| Logger.log("ENABLE : " + ad.getDestinationUrl()); | |
| ad.enable(); | |
| } | |
| // Send the results | |
| MailApp.sendEmail('somebody@somewhere.com', 'Adwords Script Results', Logger.getLog()); | |
| } | |
| // Returns YYYYMMDD-formatted date. | |
| function getFormattedDate() { | |
| var today = new Date(); | |
| return Utilities.formatDate(today, "MST", "yyyyMMdd"); | |
| } | |
| // Open and return the spreadsheet contents | |
| function getSpreadsheetData(spreadsheetUrl) { | |
| var matches = new RegExp('key=([^&#]*)').exec(spreadsheetUrl); | |
| if (!matches || !matches[1]) { | |
| throw "Invalid spreadsheet URL: " + spreadsheetUrl; | |
| } | |
| var spreadsheetId = matches[1]; | |
| var spreadsheet = SpreadsheetApp.openById(spreadsheetId); | |
| var sheet = spreadsheet.getSheets()[0]; | |
| var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()); | |
| return range.getValues(); | |
| } | |
| // Parse through the spreadsheet data and find the matching date data | |
| function getCountdownTagToShow(spreadsheetData, currentDate) { | |
| var result = "cntdwnX"; | |
| for (var i = 0; i < spreadsheetData.length; i++) { | |
| var row = spreadsheetData[i]; | |
| var spreadsheetDate = row[0]; | |
| var spreadsheetTag = row[1]; | |
| if (spreadsheetDate == currentDate) { | |
| result = spreadsheetTag; | |
| break; | |
| } | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment