Google Analytics

Get an Analytics account

function getAccountByName() {
  var accountName = 'INSERT_ACCOUNT_NAME_HERE';
  var accounts = Analytics.Management.Accounts.list();

  for (var i = 0; i < accounts.items.length; i++) {
    if (accountName == accounts.items[i].name) {
      Logger.log('Account ID: %s, Name = %s', accounts.items[i].id,
          accounts.items[i].name);
    }
  }
  return;
}

Get a web property

function getWebPropertyById() {
  var accountId = 'INSERT_ACCOUNT_ID_HERE';
  var webPropertyId = 'INSERT_WEB_PROPERTY_ID_HERE';

  var webProperty = Analytics.Management.Webproperties.get(
      accountId, webPropertyId);

  Logger.log('Web Property ID: %s, Name: %s', webProperty.id, webProperty.name);
}

List all profiles

function listAllProfiles() {
  var accountId = 'INSERT_ACCOUNT_ID_HERE';
  var webPropertyId = 'INSERT_WEB_PROPERTY_ID_HERE';

  var profiles = Analytics.Management.Profiles.list(accountId, webPropertyId);

  for (var i = 0; i < profiles.items.length; i++) {
    Logger.log('Profile ID: %s, Name: %s', profiles.items[i].id,
        profiles.items[i].name);
  }
}

Get stats for an Analytics profile

function getStatsForProfileId() {
  var profileId = 'INSERT_PROFILE_ID_HERE';

  // Dates should be in yyyy-mm-dd format.
  var startDate = 'INSERT_START_DATE_HERE';
  var endDate = 'INSERT_END_DATE_HERE';

  var results = Analytics.Data.Ga.get('ga:' + profileId, startDate,
      endDate, 'ga:sessions');

  Logger.log('Profile Name: %s', results.profileInfo.profileName);
  Logger.log('Total Sessions: %s', results.rows[0][0]);
}

Run a multi-channel funnel report

function runMultiChannelFunnelReport() {
  // See https://support.google.com/analytics/answer/1191180 to learn more about
  // multi-channel funnel reports in Google Analytics.

  var profileId = 'INSERT_PROFILE_ID_HERE';

  // Dates should be in yyyy-mm-dd format.
  var startDate = 'INSERT_START_DATE_HERE';
  var endDate = 'INSERT_END_DATE_HERE';

  var results = Analytics.Data.Mcf.get(
      'ga:' + profileId,
      startDate,
      endDate,
      'mcf:totalConversions', // List of all metrics to retrieve.
      {
        'dimensions': 'mcf:sourcePath',
        'sort': '-mcf:totalConversions',
        'max-results': 25
      }
  );

  var headers = [];
  for (var i = 0; i < results.columnHeaders.length; i++) {
    headers.push(results.columnHeaders[i].name);
  }
  Logger.log(headers.join(','));

  for (var i = 0; i < results.rows.length; i++) {
    var rowData = [];

    var row = results.rows[i];
    for (var j = 0; j < row.length; j++) {
      var cell = row[j];
      var dataType = results.columnHeaders[j].dataType;
      if (dataType == 'MCF_SEQUENCE') {
        rowData.push(getStringFromMcfSequence(cell.conversionPathValue));
      } else {
        rowData.push(cell.primitiveValue);
      }
    }
    Logger.log(rowData.join(','));
  }
}

Filter stats for an Analytics profile

function filterStats() {
  var profileId = 'INSERT_PROFILE_ID_HERE';

  var results = Analytics.Data.Ga.get(
      'ga:' + profileId,
      '2014-01-01',  // Start date in yyyy-mm-dd format.
      '2014-01-15',  // End date in yyyy-mm-dd format.
      'ga:sessions', // List of all metrics to retrieve.
      {
        // Filter for Firefox browser users in the USA. See
        // https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters
        // for filter syntax, and
        // https://developers.google.com/analytics/devguides/reporting/core/dimsmets
        // for the list of supported Dimensions and Metrics.
        'filters': 'ga:browser==Firefox;ga:country==United States'
      }
  );

  Logger.log('View (Profile) Name: %s', results.profileInfo.profileName);
  Logger.log('Total Sessions: %s', results.rows[0][0]);
}

Run real-time Analytics report

function runRealTimeReport() {
  // See https://support.google.com/analytics/answer/1638635 to learn more about
  // real-time reporting.

  var profileId = 'INSERT_PROFILE_ID_HERE';

  var results = Analytics.Data.Realtime.get(
    'ga:' + profileId,
    'rt:activeUsers',
    {
       'dimensions': 'rt:medium'
    }
  );

  var headers = [];
  for (var i = 0; i < results.columnHeaders.length; i++) {
    headers.push(results.columnHeaders[i].name);
  }
  Logger.log(headers.join(','));

  for (var i = 0; i < results.rows.length; i++) {
    var rowData = [];

    var row = results.rows[i];
    for (var j = 0; j < row.length; j++) {
      var cell = row[j];
      rowData.push(cell);
    }
    Logger.log(rowData.join(','));
  }
}

发送以下问题的反馈:

此网页
AdWords Scripts
AdWords Scripts
需要帮助?请访问我们的支持页面