Last active
September 4, 2016 15:01
-
-
Save takvol/f88a7a20930b9f24b3068a472c97ce5f to your computer and use it in GitHub Desktop.
Google script that checks that a user has accessed the content of email.
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
//USAGE | |
//sendMail("[email protected]", "test", "test", "test", "<body>test</body>"); | |
//Or embed <img src=https://script.google.com/macros/s/macro_id_here/exec?id=someId style = "height: 1px; width: 1px; display: none !important;"> | |
//in email html body | |
var SCRIPT_LINK = "https://script.google.com/macros/s/macro_id_here/exec";//macro url | |
var SHEET_ID = "spreadsheet_id_here";//logger spreadsheet id | |
function sendMail(email, subject, plainText, id, html) { | |
var html = html + '<img src=' + SCRIPT_LINK + '?id=' + id + | |
' style = "height: 1px; width: 1px; display: none !important;">'; | |
GmailApp.sendEmail(email, subject, plainText, {htmlBody: html}); | |
} | |
function doGet(e) { | |
var id = e.parameter.id; | |
var sheet = SpreadsheetApp.openById(SHEET_ID).getSheets()[0]; | |
var lastRow = sheet.getLastRow(); | |
var idCol = "A"; | |
var statusCol = "B"; | |
var dateCol = "C"; | |
var idData = sheet.getRange(idCol + "1:" + idCol + lastRow).getValues(); | |
var recordExists = false; | |
if(id) { | |
for(var i = 0; i < idData.length; i++) { | |
if(idData[i]) { | |
lastRow = i + 1; | |
} | |
if(idData[i] == id) { | |
if (!sheet.getRange(statusCol + (i + 1)).getValue()) { | |
//If no status has been recorded yet, add new data | |
sheet.getRange(statusCol + (i + 1)).setValue("Opened"); | |
sheet.getRange(dateCol + (i + 1)).setValue(new Date()); | |
} | |
recordExists = true; | |
break; | |
} | |
} | |
//Add new record if not exists | |
if(!recordExists) { | |
sheet.getRange(idCol + (lastRow + 1)).setValue(id); | |
sheet.getRange(statusCol + (lastRow + 1)).setValue("Opened"); | |
sheet.getRange(dateCol + (lastRow + 1)).setValue(new Date()); | |
} | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment