Ronit sharma
Ronit sharma

Reputation: 57

Detect who accessed your Google doc

I created a public link for a Google doc. Now anyone with the link can access it. Some users may actually be logged into there Google accounts when they access the Google doc.

  1. Is there any way to get the email ids of users who access my doc ?

  2. If not could there be a workaround for it ? Like placing a link in it which if user clicks fetches the email id of the user ( which will be possible only if he is logged into his Google account)

  3. Any alternative to achieve the same may be using Google tools or some other or may be some other doc's providers ?

Upvotes: 1

Views: 824

Answers (2)

Iamblichus
Iamblichus

Reputation: 19319

You can only access the email addresses of users in your organization, or users with whom you have explicitly shared the document, or users who have explicitly authorized you to access their identity. Security policies don't allow you to access the email addresses of other, unrelated accounts.

For users whose email address you're allowed to access, you can install an onOpen trigger using Apps Script, which could fire a function whenever a user accesses the document. You could do it the following way:

Flow:

  • Install the onOpen trigger, either manually, following these steps or programmatically.
  • Copy the following function to the script editor and save the project. This function does the following: (1) retrieve email of the user who is accessing the document, using getActiveUser(), (2) set user to Not available if security policies don't allow access to user's identity, (3) retrieve a spreadsheet with a certain ID, and (4) append the user and the current date to your sheet.
function onOpenTrigger() {
  let user = Session.getActiveUser().getEmail();
  if (!user) user = "Not available";
  const sheet = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").getSheetByName("Sheet1"); // Spreadsheet ID and sheet name according to your preferences
  sheet.appendRow([user, new Date()]);
}

After doing this, every time a user accesses the document, a new row would be appended to the corresponding spreadsheet, including the email address (or Not available if you're not allowed to access that) and current date.

Rest of accounts:

In order to get the data from the rest of accounts, you would need to get explicit consent from the corresponding users.

Option 1. Custom menu:

One option to achieve this could be using a simple onOpen trigger to create a custom menu which could be used to run the function grabbing the email address. In this case, users should click the custom menu when opening the document, in order to retrieve the access information. See, for example this sample:

function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('Log user')
    .addItem('Run script', 'addUserAccess')
    .addToUi()
}

function addUserAccess(){
  let user = Session.getActiveUser().getEmail(); 
  if (!user) user = "Not available";
}

Then, what you do with this data would depend on your exact situation. You could either append this data to your spreadsheet, as in previous example (but this would require making the spreadsheet public), or store it using PropertiesService, and export it where you prefer later.

Option 2. Editor add-on:

Another option would be making your script an add-on. See Extending Google Docs with Add-ons and Editor add-on authorization for more information.

Upvotes: 0

David
David

Reputation: 326

Users who follow the public link are anonymous. The Google docs api does not provide interfaces for controlling the accessibility of a document, so most likely it is impossible to get users who have access to the document at all

Upvotes: 1

Related Questions