Google Analytics

アナリティクス アカウントを取得

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) {
      console.log('Account ID: %s, Name = %s', accounts.items[i].id,
          accounts.items[i].name);
    }
  }
  return;
}

ウェブ プロパティを取得

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

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

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

プロファイルをすべて表示

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++) {
    console.log('Profile ID: %s, Name: %s', profiles.items[i].id,
        profiles.items[i].name);
  }
}

アナリティクス プロファイルの統計情報を取得

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');

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

マルチチャネル レポートを作成

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);
  }
  console.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);
      }
    }
    console.log(rowData.join(','));
  }
}

アナリティクス プロファイルの統計情報にフィルタを適用

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'
      }
  );

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

アナリティクスのリアルタイム レポートを作成

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);
  }
  console.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);
    }
    console.log(rowData.join(','));
  }
}