Jamesdn
Jamesdn

Reputation: 13

How to monitor page activity of a google site using Google Apps Script

Problem I want to be able to monitor user activity on my google site using Apps Script, specifically which pages users are accessing. I can not use Google Analytics (it is not within my contract with Google). Apps Script has so far been able to return user ID (email address) of the user when a page is accessed, however I can not work out how to return which page(s) are activated by that user.

What I have done so far I have created a web app and deployed / embedded it within to 2 pages in a test google site. It returns the values to a table in a linked google sheets file. Here is the script:

function doGet(event) {
  return HtmlService.createHtmlOutputFromFile('UserLog.html');
}

function getPage(pageTitle) {
  var user = Session.getActiveUser();
  var ss = SpreadsheetApp.getActive();
  var db = "Webapp Log";
  var db_ss = ss.getSheetByName(db);
  var now = new Date();
  var page = pageTitle;
  var values = [now,user,page,"User activated webapp",Session.getTemporaryActiveUserKey()];
  db_ss.getRange(db_ss.getLastRow()+1, 1, 1, 5).setValues([values]);
};

The "UserLog.html" code is this:

<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <script>
       var page = [Solution]
       google.script.run.getPage(page);
    </script>
  </body>
  </html>

And so far the return values look like this (with addresses anonymised): Table Values

As you can see, the 'Page' field is blank.

What I need Use the embedded web app to return the activated page URL OR to return another unique aspect of the activated page, for example the page title or "page 1", "page 2", etc. (all pages on the site have a unique title).

How can this be done? Is this even possible?

Upvotes: 1

Views: 478

Answers (1)

ziganotschka
ziganotschka

Reputation: 26796

You can do it with e.parameters

  • E.g. incorporate a parameter page that you append at the end of the WebApp URL.

  • When you embedd the WebApp URL in each page, assign a unique value to page, like https://script.google.com/a/XXX/macros/s/XXX/exec?page=1, https://script.google.com/a/XXX/macros/s/XXX/exec?page=2

Now, in Apps Script you just have to slightly modify your doGet() function to retrieve the page:

function doGet(event) {
  var page = event.parameter.page;
  return HtmlService.createHtmlOutputFromFile('UserLog.html');
}
  • The rest depends on your preferences. The easiest would be to directly paste the value of page into the spreadsheet from the doGet() function - this will avoid passing the parameter to the html file and then, with google.script.run back to a .gs function.

Upvotes: 2

Related Questions