Last active
August 13, 2021 21:10
-
-
Save nakitadog/bff8f66a33253a0fc019996e774ec7e1 to your computer and use it in GitHub Desktop.
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
//Enter your email address where you want the email to be sent. | |
var RECIPIENT_EMAIL = '[email protected]'; | |
//Enter the subject of the email. | |
var EMAIL_SUBJECT = 'Google Ads - Check Client Optimization Scores.'; | |
//Enter the label for all the accounts you wish to analyze. | |
var ACCOUNT_LABEL_TO_CHECK = "Monitor"; | |
//Ignore for now. | |
var INCLUDE_OPTIMIZATION_SCORE_WEIGHT = false; | |
function main() { | |
var accountSelector = MccApp.accounts().withCondition('LabelNames CONTAINS "' + ACCOUNT_LABEL_TO_CHECK +'"'); | |
//Process the data: | |
accountSelector.executeInParallel('processClientAccount', 'afterProcessAllClientAccounts'); | |
} | |
function processClientAccount() { | |
var accountName = AdsApp.currentAccount().getName(); | |
var accountId = AdsApp.currentAccount().getCustomerId(); | |
Logger.log('Checking account: ' + accountName + ' (' + accountId + ')'); | |
var query = "SELECT " + | |
"customer.optimization_score, " + | |
"customer.optimization_score_weight, " + | |
"customer.id, " + | |
"customer.manager " + | |
"FROM customer "; | |
//Logger.log("query: " + query); | |
try { | |
var results = AdsApp.search(query); | |
while (results.hasNext()) { | |
var row = results.next(); | |
//Logger.log(accountName + '(' + accountId + ') - ' + 'ROW: ' + JSON.stringify(row)); | |
var optimizationScore = row.customer.optimizationScore; | |
var optimizationScoreWeight = row.customer.optimizationScoreWeight; | |
Logger.log(accountName + '(' + accountId + ') - ' + 'optimizationScore:' + optimizationScore + ' - optimizationScoreWeight:' + optimizationScoreWeight); | |
} | |
} catch (error) { | |
Logger.log(error); | |
var jsonObj = { | |
'CheckedClientAccount': accountName + ' (' + accountId + ')', | |
'optimizationScore': error, | |
'optimizationScoreWeight': error | |
}; | |
// return a result, as a text. | |
return JSON.stringify(jsonObj); | |
} | |
//Return the data: | |
var jsonObj = { | |
'CheckedClientAccount': accountName + ' (' + accountId + ')', | |
'optimizationScore': optimizationScore, | |
'optimizationScoreWeight': optimizationScoreWeight | |
}; | |
Logger.log(accountName + '(' + accountId + ') - ' + 'Finished processing account ---> ' + JSON.stringify(jsonObj)); | |
// return a result, as a text. | |
return JSON.stringify(jsonObj); | |
} | |
function afterProcessAllClientAccounts(results) { | |
var HeaderStyle = "font-family:Arial,sans-serif;font-size:14px;font-weight:normal;padding:5px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;color:#ffffff;background-color:#343434;text-align:left;vertical-align:top"; | |
var ClientHeaderStyle = "font-family:Arial,sans-serif;font-size:14px;font-weight:normal;padding:5px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;color:#ffffff;background-color:#b9b9b9;text-align:left;vertical-align:top"; | |
var RawDataRowStyleNormal = "font-family:Arial,sans-serif;font-weight:normal;padding:10px 10px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;color:#333;background-color:#fff;text-align:left;vertical-align:top"; | |
var RawDataRowStyleError = "font-family:Arial,sans-serif;font-weight:normal;padding:10px 10px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;color:#333;background-color:#f59a9a;text-align:left;vertical-align:top"; | |
var strHTMLBody = '<p>Account optimization scores for all scanned accounts with the label "Monitor".</p>\n' + | |
'<p>Checked a total of ' + results.length + ' accounts.</p>\n' + | |
'<p>Data from: ' + Utilities.formatDate(new Date(), 'America/Chicago', 'EEE, MMM d, yyyy') +'</p>\n\n'; | |
strHTMLBody += '<table style="border-collapse:collapse;border-spacing:0;border-color:#ccc;width: max-content;">\n' | |
//Write out the header row | |
strHTMLBody += '<tr>\n'+ | |
'<th style="' + HeaderStyle + '">Client</th>\n' + | |
'<th style="' + HeaderStyle + '">Optimization Score</th>\n' + | |
'</tr>\n'; | |
//Merg all results so they can be sorted | |
var allData = []; | |
for (var i = 0; i < results.length; i++) { | |
var objResults = JSON.parse(results[i].getReturnValue()); | |
//Logger.log('objResults: ' + JSON.stringify(objResults)); | |
var CheckedClientAccount = objResults.CheckedClientAccount; | |
var optimizationScore = objResults.optimizationScore; | |
var optimizationScoreWeight = objResults.optimizationScoreWeight; | |
allData.push(new Array(CheckedClientAccount,optimizationScore,optimizationScoreWeight)); | |
} | |
//Logger.log('Before sort - allData: ' + allData); | |
allData = allData.sort(function (a, b) { | |
return a[0].toLowerCase().localeCompare(b[0].toLowerCase()); | |
}); | |
//Logger.log('After sort - allData: ' + allData); | |
for (var i = 0; i < allData.length; i++) { | |
var CheckedClientAccount = allData[i][0]; | |
var optimizationScore = allData[i][1]; | |
var optimizationScoreWeight = allData[i][2]; | |
Logger.log(CheckedClientAccount + ' - optimizationScore:' + optimizationScore + ' - optimizationScoreWeight:' + optimizationScoreWeight); | |
//Data row | |
if (INCLUDE_OPTIMIZATION_SCORE_WEIGHT) { | |
var stroptimization_score_weight = "optimizationScore * optimizationScoreWeight: " + (optimizationScore * optimizationScoreWeight).toFixed(2); | |
strHTMLBody += '<tr>\n'+ | |
'<td style="' + RawDataRowStyleNormal + '">' + CheckedClientAccount + '</td>\n' + | |
'<td style="' + ((optimizationScore >= 0.7) ? RawDataRowStyleNormal : RawDataRowStyleError) + '">' + (optimizationScore * 100).toFixed(2) + '%' + ' - ' + (optimizationScoreWeight).toFixed(2) + '<br>' + stroptimization_score_weight + '</td>\n' + | |
'</tr>\n'; | |
} else { | |
strHTMLBody += '<tr>\n'+ | |
'<td style="' + RawDataRowStyleNormal + '">' + CheckedClientAccount + '</td>\n' + | |
'<td style="' + ((optimizationScore >= 0.7) ? RawDataRowStyleNormal : RawDataRowStyleError) + '">' + (optimizationScore * 100).toFixed(2) + '%' + '</td>\n' + | |
'</tr>\n'; | |
} | |
} | |
strHTMLBody += '<br /><br />\n\n'; | |
// Process your client account here. | |
if (RECIPIENT_EMAIL != '') { | |
//now send. | |
Logger.log('Sending email to %s this is the body: %s',RECIPIENT_EMAIL, strHTMLBody); | |
MailApp.sendEmail({ | |
to: RECIPIENT_EMAIL, | |
subject: EMAIL_SUBJECT, | |
htmlBody: strHTMLBody | |
}); | |
} | |
Logger.log('Finished processing all accounts!'); | |
} | |
function numberWithCommas(x) { | |
var parts = x.toString().split("."); | |
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
return parts.join("."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment