Last active
February 8, 2018 13:59
-
-
Save paveljasek/1d148dd365c4aada76cc4740fc295123 to your computer and use it in GitHub Desktop.
Adwords Script to check yesterday campaign performance
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
// Budget Guard hlídá, zda se některé kampaně přiblížily využití denního rozpočtu | |
// Upravte nastavení a naplánujte si denní spouštění skriptu v Google AdWords | |
// Autor: Pavel Jašek, jasek@google.com | |
// Nastavení: upravte práh citlivosti a obsah e-mailu | |
var threshold = 0.9; // Hranice pro překročení rozpočtu. 0.9 = 90 % | |
var recepients = "jasek@google.com"; | |
var subject = "Kampaně včera překročily 90 % rozpočtu"; | |
var header = "Ahoj,<br>následující kampaně překročily 90 % nastaveného rozpočtu. Zkontroluj je, prosím, ať se nepřipravuješ o další výkon!<br><br>"; | |
var DATE = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"); | |
function main(){ | |
var data = []; | |
var campaigns = []; | |
var account = AdWordsApp.currentAccount().getName(); | |
var accountId = AdWordsApp.currentAccount().getCustomerId(); | |
var report = AdWordsApp.report("SELECT AssociatedCampaignName, Amount, Cost FROM BUDGET_PERFORMANCE_REPORT WHERE Cost > 0 DURING YESTERDAY").rows(); | |
while(report.hasNext()) | |
{ | |
var row = report.next(); | |
var cost = parseFloat((row['Cost']).replace(",","")); | |
var amount = parseFloat((row['Amount']).replace(",","")); | |
if( cost / amount > threshold) | |
{ | |
data.push([account,accountId,row['AssociatedCampaignName'],amount,cost,Math.floor(cost / amount * 100) / 100,DATE]); | |
} | |
} | |
Logger.log(data); | |
// list of campaigns | |
campaigns = campaigns.concat(data); | |
if(campaigns.length > 0) | |
{ | |
writeMessage(campaigns); | |
} | |
} | |
function sortByKey(array, key) { | |
return array.sort(function(a, b) { | |
var x = a[key]; var y = b[key]; | |
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); | |
}); | |
} | |
function writeMessage(campaigns) { | |
var email = { | |
to: recepients, | |
subject: subject, | |
htmlBody: header | |
} | |
// sort the table by budget | |
campaigns = sortByKey(campaigns, 3).reverse(); | |
email.htmlBody += "<table><tr><th>Název účtu</th><th>Identifikátor účtu</th><th>Kampaň</th><th>Rozpočet</th><th>Včerejší investice</th></tr>" | |
for(var i = 0; i < campaigns.length; i++) | |
{ | |
if(campaigns[i][0] == "") | |
{ | |
break; | |
} | |
email.htmlBody += "<tr><td>"+campaigns[i][0]+"</td><td>"+campaigns[i][1]+"</td><td>"+campaigns[i][2]+"</td><td>"+campaigns[i][3]+"</td><td>"+campaigns[i][4]+"</td></tr>"; | |
} | |
MailApp.sendEmail(email); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment