Skip to content

Instantly share code, notes, and snippets.

@soramugi
Created July 22, 2018 07:21
Show Gist options
  • Save soramugi/63c55aa3ab9e3b3a00280f2045adad38 to your computer and use it in GitHub Desktop.
Save soramugi/63c55aa3ab9e3b3a00280f2045adad38 to your computer and use it in GitHub Desktop.
gasで昨日のanalyticsで逆アクセス数リストを出してメールする
var scriptProperties = PropertiesService.getScriptProperties();
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
var date = Utilities.formatDate(yesterday, Session.getTimeZone(), 'yyyy-MM-dd');
function setScheduling() {
var triggers = ScriptApp.getProjectTriggers();
triggers.forEach(ScriptApp.deleteTrigger);
ScriptApp.newTrigger("exec")
.timeBased()
.everyDays(1)
.create();
}
function exec() {
var subject = date + "の" + scriptProperties.getProperty("sitename") + "逆アクセス数";
var message = runReport().join("\n");
GmailApp.sendEmail(scriptProperties.getProperty("email"), subject, message);
}
var runReport = function () {
var options = {
'dimensions': 'ga:source',
'sort': '-ga:pageviews',
'filters': 'ga:medium==referral'
};
var report = Analytics.Data.Ga.get('ga:' + scriptProperties.getProperty("profileId"), date, date, 'ga:pageviews', options);
return report.rows;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment