Skip to content

Instantly share code, notes, and snippets.

@mskutta
Created May 11, 2015 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mskutta/502341795384eed3750f to your computer and use it in GitHub Desktop.
Save mskutta/502341795384eed3750f to your computer and use it in GitHub Desktop.
Social Sort Manager.cs
private void ProcessData(GaData response, Dictionary<Guid, Metrics> metrics)
{
var pageTitleIndex = 0;
var pageViewsIndex = 0;
var socialInteractionsIndex = 0;
// Find associated columns
for (var index = 0; index < response.ColumnHeaders.Count; index++)
{
var header = response.ColumnHeaders[index];
if (string.Equals(header.Name, "ga:pageTitle", StringComparison.OrdinalIgnoreCase))
pageTitleIndex = index;
else if (string.Equals(header.Name, "ga:pageviews", StringComparison.OrdinalIgnoreCase))
pageViewsIndex = index;
else if (string.Equals(header.Name, "ga:socialInteractions", StringComparison.OrdinalIgnoreCase))
socialInteractionsIndex = index;
}
foreach (var row in response.Rows)
{
// Try to get the item id from the page title
var pageTitle = row[pageTitleIndex];
var parts = pageTitle.Split('|').Select(x => x.Trim()).ToArray();
Guid itemId;
if (!Guid.TryParse(parts.Last(), out itemId))
continue;
// Get page views
int pageViews;
if (!int.TryParse(row[pageViewsIndex], out pageViews))
pageViews = 0;
// Get Social Interactions
int socialInteractions;
if (!int.TryParse(row[socialInteractionsIndex], out socialInteractions))
socialInteractions = 0;
if (!metrics.ContainsKey(itemId))
metrics.Add(itemId, new Metrics { PageViews = pageViews, SocialInteractions = socialInteractions });
else
{
var entry = metrics[itemId];
entry.PageViews += pageViews;
entry.SocialInteractions += socialInteractions;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment