Skip to content

Instantly share code, notes, and snippets.

@bohdon
Last active December 25, 2023 20:29
Show Gist options
  • Save bohdon/f0ce55a708554a862a3a844340533bd0 to your computer and use it in GitHub Desktop.
Save bohdon/f0ce55a708554a862a3a844340533bd0 to your computer and use it in GitHub Desktop.
Google app script to aggregate email sender domains by count for finding who sends you the most email.
/**
* A Google app script that gathers threads from your inbox and sorts the
* sender domains by quantity of emails, then outputs to a spreadsheet.
*/
/** Given a sender, return just the domain of the email address. */
function getSenderDomain(sender) {
// parse from '"My Name" <[email protected]>'
var regex = RegExp("(?:<.*@)([^>]*)");
var matches = regex.exec(sender);
if (matches)
{
return matches[1];
}
// try simpler regex, just '[email protected]'
var regex2 = RegExp("(?:@)([^>]*)");
matches = regex2.exec(sender);
if (matches)
{
return matches[1];
}
Logger.log("Error: Failed to parse domain from sender: " + sender);
return sender;
}
/** Return a sorted array of tuples containing [(count, sender), ...] */
function sortDomainCounts(countByDomain) {
var keys = Object.keys(countByDomain);
var values = [];
var tuples = [];
keys.forEach((domain) => {
values.push(countByDomain[domain]);
tuples.push([countByDomain[domain], domain]);
});
return tuples.sort((a, b) => b[0] - a[0]);
}
function myFunction() {
var startThread = 0;
var stepSize = 100;
var endThread = 2000;
var countsByDomain = {};
while(startThread <= endThread) {
var threads = GmailApp.getInboxThreads(startThread, Math.min(stepSize, endThread - startThread + 1));
if (threads.length == 0) {
break;
}
for (var idx = 0; idx < threads.length; idx++) {
var sender = threads[idx].getMessages()[0].getFrom();
var domain = getSenderDomain(sender);
if (!countsByDomain[domain])
{
countsByDomain[domain] = 0;
}
countsByDomain[domain] += 1;
}
startThread += threads.length;
Logger.log("Processing " + startThread);
}
// collect into array of tuples for sorting
var sortedDomains = sortDomainCounts(countsByDomain);
// output to doc
var docName = "Email Stats [" + 0 + "-" + endThread + "]";
var outputRange = SpreadsheetApp.create(docName).getSheets()[0].getRange(1, 1, sortedDomains.length, 2);
outputRange.setValues(sortedDomains);
Logger.log("Finished!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment