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 AnalyticsService GetService() | |
{ | |
// Google Analytics API Service Account Authentication | |
var keyFilePath = _settings.GoogleAnalyticsKeyFilePath; // found in developer console under APIs & auth / Credentials | |
var serviceAccountEmail = _settings.GoogleAnalyticsServiceAccountEmail; // found in developer console under APIs & auth / Credentials | |
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable); // notasecret is the standard password for the key file. | |
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) | |
{ | |
Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly } | |
}.FromCertificate(certificate)); |
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 DataResource.GaResource.GetRequest GetRequest(AnalyticsService service) | |
{ | |
// format the profile id | |
var profileId = _settings.GoogleAnalyticsProfileId; | |
if (!profileId.Contains("ga:")) | |
profileId = string.Format("ga:{0}", profileId); | |
var window = _settings.WindowInDays; | |
var startDate = DateTime.Now.AddDays(-window); | |
var endDate = DateTime.Now; |
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 Dictionary<Guid, Metrics> Execute(DataResource.GaResource.GetRequest request) | |
{ | |
// Retrieve data, performing paging if necessary. | |
var metrics = new Dictionary<Guid, Metrics>(); | |
GaData response = null; | |
do | |
{ | |
var startIndex = 1; | |
if (response != null && !string.IsNullOrEmpty(response.NextLink)) |
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]; |
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
public class Task | |
{ | |
public void Run() | |
{ | |
var manager = new Manager(); | |
manager.UpdateCaches(); | |
} | |
} | |
public class Manager : IManager |
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
internal class Cache | |
{ | |
private static Dictionary<Guid, Metrics> _metrics; | |
static Cache() | |
{ | |
_metrics = new Dictionary<Guid, Metrics>(); | |
} | |
internal static void CacheMetrics(Dictionary<Guid, Metrics> metrics) |
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
public class Service : IService | |
{ | |
public int GetPageViews(Item item) | |
{ | |
return GetPageViews(item.ID); | |
} | |
public int GetPageViews(ID id) | |
{ | |
return GetPageViews(id.Guid); |
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
var service = new OneNorth.SocialSort.GoogleAnalytics.Service(); | |
// Obtain the number of page views for an item | |
var pageViews = service.GetPageViews(<Item>); | |
// Obtain the number of social interactions for an item | |
var socialInteractions = service.GetSocialInteractions(<Item>); |
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
var service = new OneNorth.SocialSort.GoogleAnalytics.Service(); | |
List<Item> items = <Get list of items from somewhere>; | |
var sortedItems = items.OrderBy(service.GetPageViews); |