Skip to content

Storing transactional data logs

In some cases, you need a place/database to store some temporary data. Examples:

  • store LOC metric per release
  • failed CSRF checks before enabling it for the whole app (to add missing tokens to forms and requests)
  • type coverage by Psalm

As you can see, these metrics are not product related ones and ideally should not be stored in the application DB. There are other reasons why to not use the main application database, as we need:

  1. Easy way to manage "columns"
  2. Not spend server resources
  3. It should be free or almost free
  4. Around zero maintenance cost

To cover such cases, we currently use Google Spreadsheets as free DB. In order to add a row to such DB, we use POST request to Google Script App (it's free) that has a great API to manage records in Google Spreadsheets.

Google Apps Script

You don't need to learn a new language: Google Apps Script uses a subset of JavaScript.

All that you need to accept POST request by your Google Script App is to implement doPost() function. Example:

js
/**
 * Process POST request that contains output from cloc util
 * @see https://developers.google.com/apps-script/guides/web
 */
function doPost(e) {
    const sheetDocId = '1lSkfT01IxxNh91cwJPoPuLSOm7rwq9RkQQZUB1SvKec';
    const sheetName = 'users';
    const sheet = SpreadsheetApp.openById(sheetid).getSheetByName(sheetName);
    const requestData = JSON.parse(e.postData.contents);

    sheet.appendRow([
        data.name,
        data.email,
        // etc.
    ]);

    return ContentService.createTextOutput('Data received and stored.');
}

After every update in your Google Apps Script you need:

  1. Store project (Ctrl+S)
  2. Deploy -> Manage deployments -> Edit (pen icon) -> On "Version" select New Version -> Deploy (will ask you to authorize access on first run)

Your should transfer ownership of Script and Spreadsheet to developemnt@interaction-design.org Google account and provide edit access to your colleagues from the dev team or to the whole company.

Materials

Examples