如何使用Google Apps脚本监视Google网站的页面活动

问题描述 投票:2回答:1

问题我希望能够使用Apps脚本来监视我的Google网站上的用户活动,特别是用户正在访问的页面。我无法使用Google Analytics(分析)(不在我与Google签订的合同中)。到目前为止,Apps Script能够在访问页面时返回该用户的用户ID(电子邮件地址),但是我无法确定如何返回该用户激活的页面。

我到目前为止所做的事情我已经创建了一个Web应用程序,并将其部署/嵌入到测试Google站点的2页内。它将值返回到链接的Google表格文件中的表格中。这是脚本:

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>

到目前为止,返回值看起来像这样(地址匿名):Table Values

如您所见,“页面”字段为空白。

我需要什么使用嵌入式Web应用程序返回激活的页面URL,或返回激活的页面的另一个唯一方面,例如页面标题或“页面1”,“页面2”等(网站上的所有页面都有唯一的标题) )。

如何完成?这甚至可能吗?

google-apps-script web-applications google-sites
1个回答
0
投票

您可以使用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函数。] >
© www.soinside.com 2019 - 2024. All rights reserved.