如何使用 Google Apps 脚本监控 google 站点的页面 activity

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

问题 我希望能够使用 Apps 脚本在我的 google 网站上监控用户 activity,特别是用户正在访问哪些页面。我不能使用 Google 分析(它不在我与 Google 的合同范围内)。到目前为止,Apps 脚本已经能够 return 访问页面时用户的用户 ID(电子邮件地址),但是我不知道如何 return 哪个页面被那个激活用户。

到目前为止我做了什么 我创建了一个 Web 应用程序并将其部署/嵌入到测试 google 站点的 2 个页面中。它 return 将值发送到链接的 google 表格文件中的 table。这是脚本:

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]);
};

"UserLog.html"代码是这样的:

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

到目前为止,return 值如下所示(地址已匿名): Table Values

如您所见,'Page' 字段为空白。

我需要的 使用嵌入式网络应用 return 激活页面 URL 或 return 激活页面的另一个独特方面,例如页面标题或 "page 1"、"page 2", 等等(网站上的所有页面都有一个唯一的标题)。

如何做到这一点?这可能吗?

你可以用 e.parameters

  • 例如合并您在 WebApp URL.

  • 末尾附加的参数 page
  • 当您在每个页面中嵌入 WebApp URL 时,为页面分配一个唯一值,例如 https://script.google.com/a/XXX/macros/s/XXX/exec?page=1https://script.google.com/a/XXX/macros/s/XXX/exec?page=2

现在,在 Apps 脚本中,您只需稍微修改 doGet() 函数即可检索页面:

function doGet(event) {
  var page = event.parameter.page;
  return HtmlService.createHtmlOutputFromFile('UserLog.html');
}
  • 剩下的就看你的喜好了。最简单的方法是直接将 page 的值从 doGet() 函数粘贴到电子表格中 - 这将避免将参数传递给 html 文件,然后使用 google.script.run回到 .gs 函数。