Created
May 11, 2015 18:11
-
-
Save mskutta/502341795384eed3750f to your computer and use it in GitHub Desktop.
Social Sort Manager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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