[Google Apps Script] Get daily page views from Universal Analytics

Created on November 30, 2026

This example shows how Analytics.Data.Ga.get retrieved daily page views over the previous 30 days. It uses the retired Universal Analytics Reporting API and is provided as a historical reference. Google Analytics 4 uses the Google Analytics Data API.

  var viewId = YOURVIEWID;
  var currentDate = new Date();
  var thirtyDaysAgo = new Date(currentDate.getTime() - 30 * 24 * 60 * 60 * 1000);

  var startDate = Utilities.formatDate(
    thirtyDaysAgo,
    Session.getTimeZone(),
    'yyyy-MM-dd'
  );
  var endDate = Utilities.formatDate(
    currentDate,
    Session.getTimeZone(),
    'yyyy-MM-dd'
  );

  var tableId  = 'ga:' + viewId;
  var metric = 'ga:pageviews';
  var options = {
    'dimensions': 'ga:date',
    'sort': 'ga:date',
  };
  var report = Analytics.Data.Ga.get(
    tableId,
    startDate,
    endDate,
    metric,
    options
  );

  Logger.log(report)

The call took five arguments.

  • tableId was the Universal Analytics view ID prefixed with ga:.
  • startDate and endDate specified the date range.
  • metric was ga:pageviews.
  • options used ga:date in dimensions to group results by day and in sort to order the results by date.
Google Apps Script