Profile/Report Query: The second class I mentioned before is GAData
:
public class GAData
{
public int Pageviews { get; set; }
public int Bounces { get; set; }
public int Entrances { get; set; }
public int Exits { get; set; }
public int NewVisits { get; set; }
public double TimeOnPage { get; set; }
public double TimeOnSite { get; set; }
public int Visitors { get; set; }
public int Visits { get; set; }
public int UniquePageviews { get; set; }
public string ExitPagePath { get; set; }
public string LandingPagePath { get; set; }
public string NextPagePath { get; set; }
public string PagePath { get; set; }
public string PageTitle { get; set; }
public string PreviousPagePath { get; set; }
public string SecondPagePath { get; set; }
public string Browser { get; set; }
public string BrowserVersion { get; set; }
public string City { get; set; }
public string ConnectionSpeed { get; set; }
public string Country { get; set; }
public string Date { get; set; }
public string DaysSinceLastVisit { get; set; }
public string Day { get; set; }
public string FlashVersion { get; set; }
public string Hostname { get; set; }
public string IsMobile { get; set; }
public string Hour { get; set; }
public string JavaEnabled { get; set; }
public string Language { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string Month { get; set; }
public string NetworkDomain { get; set; }
public string NetworkLocation { get; set; }
public string OperatingSystem { get; set; }
public string OperatingSystemVersion { get; set; }
public string PageDepth { get; set; }
public string Region { get; set; }
public string ScreenColors { get; set; }
public string ScreenResolution { get; set; }
public string SubContinent { get; set; }
public string UserDefinedValue { get; set; }
public int VisitCount { get; set; }
public int VisitLength { get; set; }
public string VisitorType { get; set; }
public int Week { get; set; }
public int Year { get; set; }
public string Source { get; set; }
}
The GAData
objects contain the real statistics about our pages. To set the dimensions and metrics that we want to retrieve, two enum
s are defined:
public enum Dimension
{
exitPagePath,
landingPagePath,
nextPagePath,
pagePath,
pageTitle,
previousPagePath,
secondPagePath,
browser,
browserVersion,
city,
connectionSpeed,
country,
date,
daysSinceLastVisit,
day,
flashVersion,
hostname,
isMobile,
hour,
javaEnabled,
language,
latitude,
longitude,
month,
networkDomain,
networkLocation,
operatingSystem,
operatingSystemVersion,
pageDepth,
region,
screenColors,
screenResolution,
subContinent,
userDefinedValue,
visitCount,
visitLength,
visitorType,
week,
year,
source,
}
public enum Metric
{
bounces,
entrances,
exits,
newVisits,
pageviews,
timeOnPage,
timeOnSite,
visitors,
visits,
uniquePageviews
}
To get the final results, first define what dimensions/metrics you're interested in and then call the GetAnalytics
method. The GetAnalytics
method's definition is as follows:
public IEnumerable<GAData> GetAnalytics(string tableID, DateTime from,
DateTime to, int max,
List<Dimension> dimensions, List<Metric> metrics,
Metric sort, SortDirection order)
tableID
can be retrieved from the previous section. notice that the tableID
shouldn't contain "ga:
" and maxNumber
is the maximum number of returned GAData
Items. For example, if you want to get the statistics for the last 30 days, you should do something like:
var dimensions = new List<Dimension>();
var metrics = new List<Metric>();
dimensions.Add(Dimension.pagePath);
dimensions.Add(Dimention.pageTitle);
metrics.Add(Metric.pageviews);
var fromDate=DateTime.Now.AddDays(-30);
var toDate=DateTime.Now;
IEnumerable<GAData> data = gaDataFetcher.GetAnalytics
(myTableId, fromDate, toDate, 10,
dimensions, metrics, metrics[0], GAConnect.SortDirection.Descending);
An important thing to note here is that not all dimension/metric combinations are allowed. If you provide an incorrect dimension/metric combination, you'll get Http 400 (Bad Request) exception. To view the valid combinations, check out this link.
Now, what happens behind the scenes when you call GetAnalytics
method, is that it calls the GetBaseData
method:
public static IEnumerable<BaseData> GetBaseData
(string tableID, IEnumerable<Dimension> dimensions,
IEnumerable<Metric> metrics, DateTime from, DateTime to,
Metric sort, SortDirection direction, int maxrecords)
The GetBaseData
method, itself calls getXMLData
method which retrieves pure XML data (in XDocument
format) from Google.
private static XDocument getXMLData
(string tableID, IEnumerable<Dimension> dimensions,
IEnumerable<Metric> metrics, DateTime from, DateTime to,
Metric sort, SortDirection direction, int maxrecords)
The GetBaseData
method parses the XML retrieved by getXMLData
method, takes out the dimension/metric pairs and passes them as a BaseData
type object to our GetAnalytics
method. The BaseData
class's definition is as follows:
public class BaseData
{
public IEnumerable<KeyValuePair<Dimension, string>> Dimensions { get; set; }
public IEnumerable<KeyValuePair<Metric, string>> Metrics { get; set; }
Now we have our data in an IEnumerable<BaseData>
object. By using lambda expressions, the returned object from GetBaseData
will be mapped to an IEnumerable<GAData>
object and will be returned to the user:
return data.Select(d => new GAData
{
Pageviews = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.pageviews).Value),
Bounces = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.bounces).Value),
Entrances = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.entrances).Value),
Exits = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.exits).Value),
NewVisits = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.newVisits).Value),
TimeOnPage = Convert.ToDouble(d.Metrics.FirstOrDefault
(met => met.Key == Metric.timeOnPage).Value),
TimeOnSite = Convert.ToDouble(d.Metrics.FirstOrDefault
(met => met.Key == Metric.timeOnSite).Value),
Visitors = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.visits).Value),
Visits = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.pageviews).Value),
UniquePageviews = Convert.ToInt32(d.Metrics.FirstOrDefault
(met => met.Key == Metric.uniquePageviews).Value),
ExitPagePath = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.exitPagePath).Value,
LandingPagePath = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.landingPagePath).Value,
NextPagePath = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.nextPagePath).Value,
PagePath = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.pagePath).Value,
PageTitle = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.pageTitle).Value,
PreviousPagePath = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.previousPagePath).Value,
SecondPagePath = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.secondPagePath).Value,
Browser = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.browser).Value,
BrowserVersion = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.browserVersion).Value,
City = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.city).Value,
ConnectionSpeed = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.connectionSpeed).Value,
Country = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.country).Value,
Date = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.date).Value,
DaysSinceLastVisit = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.daysSinceLastVisit).Value,
Day = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.day).Value,
FlashVersion = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.flashVersion).Value,
Hostname = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.hostname).Value,
IsMobile = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.isMobile).Value,
Hour = d.Dimensions.FirstOrDefault(dim => dim.Key == Dimension.hour).Value,
JavaEnabled = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.javaEnabled).Value,
Language = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.language).Value,
Latitude = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.latitude).Value,
Longitude = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.longitude).Value,
Month = d.Dimensions.FirstOrDefault(dim => dim.Key == Dimension.month).Value,
NetworkDomain = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.networkDomain).Value,
NetworkLocation = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.networkLocation).Value,
OperatingSystem = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.operatingSystem).Value,
OperatingSystemVersion = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.operatingSystemVersion).Value,
PageDepth = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.pageDepth).Value,
Region = d.Dimensions.FirstOrDefault(dim => dim.Key == Dimension.region).Value,
ScreenColors = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.screenColors).Value,
ScreenResolution = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.screenResolution).Value,
SubContinent = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.subContinent).Value,
UserDefinedValue = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.userDefinedValue).Value,
VisitCount = Convert.ToInt32(d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.visitCount).Value),
VisitLength = Convert.ToInt32(d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.visitLength).Value),
VisitorType = d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.visitorType).Value,
Week = Convert.ToInt32(d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.week).Value),
Year = Convert.ToInt32(d.Dimensions.FirstOrDefault
(dim => dim.Key == Dimension.year).Value),
Source = d.Dimensions.FirstOrDefault(dim => dim.Key == Dimension.source).Value
});