Created
October 12, 2010 07:38
-
-
Save cramforce/621814 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
var campaign = (function () { | |
// Build a string of all campaigns, adgroups, and keywords. | |
// Add the first 4 natural search results for each keyword. | |
function summary() { | |
// Maltes Account | |
AdWordsApp.setCustomerId(76158113, 6557889129); | |
var campaigns = AdWordsApp.getCampaigns(); | |
var data = []; | |
campaigns.forEach(function (c) { | |
data.push(c.getName() + " " + c.isEnabled()); | |
c.getAdGroups().forEach(function (g) { | |
data.push(" "+ g.getName() + " " + g.isEnabled()); | |
g.getKeywords().forEach(function (keyword) { | |
var text = keyword.getText(); | |
data.push(" " + text); | |
// Fetch results from the AJAX search API | |
var result = UrlFetchApp.fetch("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+encodeURIComponent(text)+"&start=0"); | |
// Who needs error checking anyway? :) | |
var search = Utilities.jsonParse(result.getContentText()).responseData.results; | |
search.forEach(function (result) { | |
data.push(" "+result.url); | |
}) | |
}); | |
}) | |
}); | |
return data.join("\n"); | |
} | |
// Send an email to the current user | |
function mailCurrentUser(subject, text) { | |
var email = Session.getActiveUser().getEmail(); | |
MailApp.sendEmail(email, subject, text); | |
} | |
return { | |
summary: summary, | |
mail: mailCurrentUser | |
} | |
})(); | |
// Expose a dailyMail function. Mails current campaign summary to current user. | |
function dailyMail() { | |
campaign.mail("Daily Results", campaign.summary()); | |
} | |
// Display the summary in an UI panel | |
function uiTest(e) { | |
var mydoc = SpreadsheetApp.getActiveSpreadsheet(); | |
var app = UiApp.createApplication(); | |
var labels = campaign.summary().split(/\n/); // Ouch :) | |
var flowPanel = app.createFlowPanel(); | |
labels.forEach(function (text) { | |
flowPanel.add(app.createLabel(text)); | |
}); | |
app.add(flowPanel); | |
mydoc.show(app); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment