Analytics Getting data from a profile

Getting data from a profile

Getting data has a lot of options. In the example below, we get data for profileId for the last month. The data returned is AnalyticsMetric.Visits and AnalyticsMetric.Pageviews grouped by AnalyticsDimension.Date.

AnalyticsDataResponse data = analytics.GetData(
    AnalyticsMetric.Visits + AnalyticsMetric.Pageviews,

You can also use the AnalyticsDataOptions class. While the class has more properties for advanced use, the example below will do the same as above:

AnalyticsDataOptions options = new AnalyticsDataOptions {
    StartDate = DateTime.Now.AddDays(-31),
    EndDate = DateTime.Now,
    Dimensions = AnalyticsDimension.Date,
    Metrics = AnalyticsMetric.Visits + AnalyticsMetric.Pageviews

AnalyticsDataResponse data = analytics.GetData(profileId, options);

You can then iterate over the data as (dimensions are listed first, then metrics):

foreach (AnalyticsDataRow row in data.Rows) {

    // Get the date (both calls give the same result)
    string date1 = row.GetString(AnalyticsDimension.Date);
    string date2 = row.GetString("ga:date");

    int visits = row.GetInt32(AnalyticsMetric.Visits);
    int pageviews = row.GetInt32(AnalyticsMetric.Pageviews);

    // Almost as before, but cell is now an object rather than a string
    foreach (AnalyticsDataCell cell in row.Cells) {