当前位置:首页 > 未分类 > 正文内容

How to Track Google Spreadsheet Views with Google Analytics

ceacer3周前 (05-02)未分类4320

You have been using Google Analytics to track visitors (or page views) on your website but did you know that the same analytics service can also be used for tracking views inside Google Spreadsheets. You insert a little snippet of tracking code inside your spreadsheet and when someone opens the sheet, that visit will be recorded permanently in your Google Analytics account.

track-google-spreadsheets.png

Google Analytics provides a JavaScript snippet that can be inserted into web templates for tracking visits. You cannot insert JavaScript inside the cells of a Google Spreadsheet but we can use the IMAGE function combined with some Apps Script to enable tracking inside spreadsheet. The visit is recorded as an “event” and not a “page view” and thus your spreadsheet opens will not artificially inflate your Google Analytics reports.

To get started, go to your Google Analytics dashboard and make a note of the Google Analytics tracking ID which is a string like UA-12345-67. This web tutorial explains how you can locate the ID inside your Analytics dashboard.

Now open any Google Spreadsheet that you wish to track and go to Tools, Script Editor and copy-paste the following code. This is a custom Google Spreadsheet function that will embed the 1x1 tracking GIF image in our spreadsheets.

/**
 * Track Spreadsheet views with Google Analytics
 *
 * @param {string} gaaccount Google Analytics Account like UA-1234-56.
 * @param {string} spreadsheet Name of the Google Spreadsheet.
 * @param {string} sheetname Name of individual Google Sheet.
 * @return The 1x1 tracking GIF image
 * @customfunction
 */

function GOOGLEANALYTICS(gaaccount, spreadsheet, sheetname) {
  /**
   * Written by Amit Agarwal
   * Web: www.ctrlq.org
   * Email: [email protected]
   */

  var imageURL = [
    'https://ssl.google-analytics.com/collect?v=1&t=event',
    '&tid=' + gaaccount,
    '&cid=' + Utilities.getUuid(),
    '&z=' + Math.round(Date.now() / 1000).toString(),
    '&ec=' + encodeURIComponent('Google Spreadsheets'),
    '&ea=' + encodeURIComponent(spreadsheet || 'Spreadsheet'),
    '&el=' + encodeURIComponent(sheetname || 'Sheet')
  ].join('');

  return imageURL;
}

Save the code, close the Apps Script editor window and return to the spreadsheet.

Click an empty cell and insert the following formula. The cell will be blank but it contains an embedded image. You may want to change the background color so it is easy to figure out which cell in the spreadsheet contains the tracking formula.

Google Formula

The GOOGLEANALYTICS() formula takes 3 parameters - the analytics ID, the spreadsheet name and the sheet name. This helps if you would like to separately track individual sheets inside a spreadsheet.

Now open the spreadsheet in a new browser window and go to Google Analytics, Real Time, Overview to test if the tracking is working. It may sometimes take a minute to record the visit. If you would like to see all the visits, go to Behavior - Events - Overview and click on the Google Spreadsheets category.

google-analytics-report.png

Also see: Track Gmail Messages with Google Analytics

The tracking would work even if the user has enabled ad blocking and that’s because Google Spreadsheets, like Gmail, serves images through a proxy server. The downside is that you will never know the location of the visitor since all visits will be show up as United States (the location of Google servers).

相关文章

How to Create Multiple Sub

How to Create Multiple Sub

A teacher would like to create separate Google Drive folders for each student in her class. Within e...

How to Automatically Rename Files in Google Drive with Apps Script and AI

How to Automatically Rename Files in Google Drive with Apps Script and AI

Do you have image files in your Google Drive with generic names like IMG_123456.jpg or Screenshot.pn...

The Best Google Add

The availability of third-party add-ons for Google Docs, Sheets and Google Slides have certainly mad...

How to Mail Merge with Outlook and Google Sheets

How to Mail Merge with Outlook and Google Sheets

The Mail merge add-on for Gmail lets you send personalized emails to your contacts in bulk. You can...

Google Maps Formulas for Google Sheets

Google Maps Formulas for Google Sheets

You can bring the power of Google Maps to your Google Sheets using simple formulas with no coding. Y...

How to Use Conditional Logic in Google Documents

How to Use Conditional Logic in Google Documents

Conditional content allows you to customize your Google Docs template and generate different version...