Skip to content

Instantly share code, notes, and snippets.

@ArchieR7
Created October 31, 2018 15:06
Show Gist options
  • Save ArchieR7/7f57306eb5141fdfe4ab024c52356adc to your computer and use it in GitHub Desktop.
Save ArchieR7/7f57306eb5141fdfe4ab024c52356adc to your computer and use it in GitHub Desktop.
Archie.tw
var token = "";
function doPost(request) {
if (token != request.parameter.token) {
return;
}
const text = request.parameter.text;
const textForSlack = getAdSenseResponse(text);
const response = {"response_type": "in_channel", "text": textForSlack};
return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON);
}
function getAdSenseResponse(text) {
var slackText = "";
const today = new Date();
const timezone = Session.getTimeZone();
const endDate = Utilities.formatDate(today, timezone, 'yyyy-MM-dd');
var startDate = Utilities.formatDate(new Date(today.getYear(), today.getMonth(),today.getDate() -1), timezone, 'yyyy-MM-dd');
if (text == "本月") {
startDate = Utilities.formatDate(new Date(today.getYear(), today.getMonth(),1), timezone, 'yyyy-MM-dd');
} else if (text == "總計") {
startDate = "2018-01-01";
}
var report = AdSense.Reports.generate(startDate, endDate, {
metric: ['PAGE_VIEWS', 'AD_REQUESTS', 'CLICKS','AD_REQUESTS_CTR', 'COST_PER_CLICK', 'AD_REQUESTS_RPM','EARNINGS'],
}).rows;
if (report) {
slackText += "瀏覽量:" + report[0][0] + " 次\n廣告要求次數:" + report[0][1] + " 次\n點擊:" + report[0][2] +" 次\n點擊率:"+ report[0][3];
slackText += "\n單次點擊出價:" + report[0][4] + "\n千次廣告請求收益:US$" + report[0][5] + "\n預估總收益:US$" + report[0][6];
return slackText;
} else {
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment