Skip to content

Instantly share code, notes, and snippets.

@mskutta
mskutta / Manager.cs
Created May 11, 2015 17:59
Social Sort Manager.cs
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));
@mskutta
mskutta / Manager.cs
Created May 11, 2015 18:03
Social Sort Manager.cs
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;
@mskutta
mskutta / Manager.cs
Created May 11, 2015 18:07
Social Sort Manager.cs
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))
@mskutta
mskutta / Manager.cs
Created May 11, 2015 18:11
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];
@mskutta
mskutta / Task.cs
Last active August 29, 2015 14:20
Social Sort Task.cs
public class Task
{
public void Run()
{
var manager = new Manager();
manager.UpdateCaches();
}
}
public class Manager : IManager
@mskutta
mskutta / Cache.cs
Created May 11, 2015 18:24
Social Sort Cache.cs
internal class Cache
{
private static Dictionary<Guid, Metrics> _metrics;
static Cache()
{
_metrics = new Dictionary<Guid, Metrics>();
}
internal static void CacheMetrics(Dictionary<Guid, Metrics> metrics)
@mskutta
mskutta / OneNorth.SocialSort.config
Created May 11, 2015 18:30
Social Sort OneNorth.SocialSort.config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<settings>
<setting name="OneNorth.SocialSort.GoogleAnalytics.ProfileId" value="" />
<setting name="OneNorth.SocialSort.GoogleAnalytics.ServiceAccountEmail" value="" />
<setting name="OneNorth.SocialSort.GoogleAnalytics.KeyFilePath" value="" />
<setting name="OneNorth.SocialSort.WindowInDays" value="30" />
</settings>
</sitecore>
@mskutta
mskutta / Service.cs
Created May 11, 2015 18:33
Social Sort Service.cs
public class Service : IService
{
public int GetPageViews(Item item)
{
return GetPageViews(item.ID);
}
public int GetPageViews(ID id)
{
return GetPageViews(id.Guid);
@mskutta
mskutta / Example.cs
Created May 11, 2015 18:36
Social Sort Example.cs
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>);
@mskutta
mskutta / Example.cs
Created May 11, 2015 18:37
Social Sort Example.cs
var service = new OneNorth.SocialSort.GoogleAnalytics.Service();
List<Item> items = <Get list of items from somewhere>;
var sortedItems = items.OrderBy(service.GetPageViews);