Google Analytics

Recupera un account 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;
}

Visualizza una proprietà web

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

Elenca tutti i profili

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

Visualizza le statistiche per un profilo Analytics

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

Esegue un rapporto sulle canalizzazioni multicanale

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

Filtra le statistiche per un profilo di Analytics

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

Esegui un rapporto di Analytics in tempo reale

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