Last active
August 29, 2015 13:58
-
-
Save luctus/9939088 to your computer and use it in GitHub Desktop.
Google Script to send a daily report email with Google Analytics data
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
function mailer() { | |
// This function is from the 'Google Analytics Report Automation (Magic)' script | |
// (search for it at the scripts gallery) | |
getData(); | |
try | |
{ | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheetByName('total'); | |
var range = sheet.getRange("A13:E20"); | |
var visits = range.getCell(8,2).getValue(); | |
var visits_last_week = range.getCell(1,2).getValue(); | |
var pageviews = range.getCell(8,3).getValue(); | |
var pageviews_last_week = range.getCell(1,3).getValue(); | |
var avg_time_on_site = range.getCell(8,4).getValue(); | |
var avg_time_on_site_last_week = range.getCell(1,4).getValue(); | |
var bounce_rate = range.getCell(8,5).getValue(); | |
var bounce_rate_last_week = range.getCell(1,5).getValue(); | |
var report_date = range.getCell(8,1).getValue().toString().split(' ').splice(1,3).join(' '); | |
report_date = range.getCell(8,1).getValue().toString().split(' ')[0] + ', ' + report_date; | |
var logo_blob = UrlFetchApp.fetch('http://ad009cdnb.archdaily.net/doodles/archdaily.png').getBlob().setName("logoBlob"); | |
var template = HtmlService.createTemplateFromFile('emailTemplate'); | |
template.domain = 'http://archdaily.com'; | |
template.html_title = 'ArchDaily Daily Analytics'; | |
template.logo_alt = 'ArchDaily Daily Analytics'; | |
template.visits = visits; | |
template.visits_percentage = (100 * (visits - visits_last_week) / visits_last_week).toFixed(2); | |
template.visits_percentage_color = template.visits_percentage > 0 ? '#00AE85' : '#CC0A3C'; | |
template.pageviews = pageviews; | |
template.pageviews_percentage = (100 * (pageviews - pageviews_last_week) / pageviews_last_week).toFixed(2); | |
template.pageviews_percentage_color = template.pageviews_percentage > 0 ? '#00AE85' : '#CC0A3C'; | |
template.avg_time_on_site = secondsWithFormat(avg_time_on_site); | |
template.avg_time_on_site_percentage = (100 * (avg_time_on_site - avg_time_on_site_last_week) / avg_time_on_site_last_week).toFixed(2); | |
template.avg_time_on_site_percentage_color = template.avg_time_on_site_percentage > 0 ? '#00AE85' : '#CC0A3C'; | |
template.bounce_rate = bounce_rate.toFixed(1); | |
template.bounce_rate_percentage = (100 * (bounce_rate - bounce_rate_last_week) / bounce_rate_last_week).toFixed(2); | |
template.bounce_rate_percentage_color = template.bounce_rate_percentage < 0 ? '#00AE85' : '#CC0A3C'; | |
var sheet_most_visited_articles = ss.getSheetByName('most visited articles'); | |
var range_most_visited_articles = sheet_most_visited_articles.getRange("A13:C17"); | |
template.most_visited_articles = range_most_visited_articles.getValues(); | |
var html = template.evaluate().getContent(); | |
var recipient = '[email protected]'; | |
var subject = 'ArchDaily Analytics Report for ' + report_date; | |
var message = 'visits: ' + numberFormat(visits) + ', pageviews: ' + numberFormat(pageviews); | |
message += "\n\nArchDaily.com"; | |
MailApp.sendEmail(recipient, subject, message, {htmlBody:html, inlineImages: | |
{ | |
main_logo: logo_blob | |
}}); | |
} catch (e) { | |
Logger.log(e.toString()); | |
} | |
} | |
function secondsWithFormat(time_in_seconds) { | |
var sec_num = parseInt(time_in_seconds, 10); | |
var hours = Math.floor(sec_num / 3600); | |
var minutes = Math.floor((sec_num - (hours * 3600)) / 60); | |
var seconds = sec_num - (hours * 3600) - (minutes * 60); | |
if (hours < 10) {hours = "0"+hours;} | |
if (minutes < 10) {minutes = "0"+minutes;} | |
if (seconds < 10) {seconds = "0"+seconds;} | |
if (hours == '00') | |
return minutes + ':' + seconds; | |
return hours + ':' + minutes + ':' + seconds; | |
} | |
function numberFormat(num, sumarize) { | |
if(sumarize){ | |
num = Math.round(num/1000); | |
} | |
var str = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
if(sumarize){ | |
str += 'k'; | |
} | |
return str; | |
} | |
function secondsSumarized(time_in_seconds) { | |
hours = (time_in_seconds/60/60).toFixed(1); | |
return hours + 'hrs'; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><?= html_title ?></title> | |
</head> | |
<body style="margin:0; width:100%; border:0; padding:0; font-size:12px; color: #333; font-family:arial,sans-serif;"> | |
<div style="width:98%;margin:0 auto;"> | |
<div style="padding: 0 1px 100px;"> | |
<table style="margin:0 auto; max-width:600px; border:0; padding:0; table-layout:fixed; font-weight:300;" cellpadding="0" cellspacing="5" border="0" width="100%"> | |
<tbody> | |
<tr style="height:20px;"> | |
<td colspan="4"> </td> | |
</tr> | |
<tr style="text-align:center;"> | |
<td colspan="4"><img src="cid:main_logo" width="145" alt="<?= logo_alt ?>" title="<?= logo_alt ?>"></td> | |
</tr> | |
<tr style="height:30px;"> | |
<td colspan="4"> </td> | |
</tr> | |
<!-- overview --> | |
<tr style="text-align:center; font-size:18px; font-weight: bold; color:#026CB6;"> | |
<td><?= numberFormat(visits,true) ?></td> | |
<td><?= numberFormat(pageviews, true) ?></td> | |
<td><?= avg_time_on_site ?></td> | |
<td><?= bounce_rate ?>%</td> | |
</tr> | |
<tr style="text-align:center; font-weight:bold; color:#303030; line-height:1.1em;"> | |
<td>Visits</td> | |
<td>Pageviews</td> | |
<td>Avg. Time on Site</td> | |
<td>Bounce Rate</td> | |
</tr> | |
<tr style="text-align:center; color:white; font-weight:bold;"> | |
<td title="Compared to last week" style="padding: 5px 0; background:<?= visits_percentage_color ?>;"><?= visits_percentage ?>%</td> | |
<td title="Compared to last week" style="padding: 5px 0; background:<?= pageviews_percentage_color ?>;"><?= pageviews_percentage ?>%</td> | |
<td title="Compared to last week" style="padding: 5px 0; background:<?= avg_time_on_site_percentage_color ?>;"><?= avg_time_on_site_percentage ?>%</td> | |
<td title="Compared to last week" style="padding: 5px 0; background:<?= bounce_rate_percentage_color ?>;"><?= bounce_rate_percentage ?>%</td> | |
</tr> | |
<!-- end overview --> | |
</tbody> | |
</table> | |
<table style="margin:0 auto; max-width:600px; border:0; padding:0; table-layout:fixed; font-weight:300;" cellpadding="0" cellspacing="5" border="0" width="100%"> | |
<tbody> | |
<!-- most visited --> | |
<tr style="height:30px;"> | |
<td colspan="4"> </td> | |
</tr> | |
<tr style="font-size:22px;"> | |
<td colspan="4">Most Visited Articles</td> | |
</tr> | |
<tr style="font-size:14px;"> | |
<th colspan="3" style="padding: 5px 0; text-align:left;border-bottom:1px solid #DDD">Article</th> | |
<th style="padding: 5px 0; text-align:right;border-bottom:1px solid #DDD">Views</th> | |
</tr> | |
<? for (var i = 0 ; i < most_visited_articles.length ; i ++) { ?> | |
<tr style="font-size:14px;"> | |
<td colspan="3" style="padding: 0 0 5px; text-align:left;border-bottom:1px solid #F1F1F1"> | |
<a style="color:#026CB6; text-decoration:none;" href="<?= domain ?><?= most_visited_articles[i][0] ?>"> | |
<?= most_visited_articles[i][1] ?> | |
</a> | |
</td> | |
<td style="padding: 0 0 5px; text-align:right;border-bottom:1px solid #F1F1F1"> | |
<?= numberFormat(most_visited_articles[i][2]) ?> | |
</td> | |
</tr> | |
<? } ?> | |
<!-- end most visited --> | |
<tr style="height:40px;"> | |
<td colspan="4"> </td> | |
</tr> | |
<tr style="background:#026CB6;color:white;font-size:18px;font-weight:bold;"> | |
<td colspan="4" style="padding: 20px;">Thanks for reviewing our daily metrics overview. Have a nice day!<br/>The Amazing Dev Team</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment