Skip to content
This repository has been archived by the owner on Dec 6, 2019. It is now read-only.

Commit

Permalink
Merge pull request #324 from Sitecore/feature/fake-tracking
Browse files Browse the repository at this point in the history
Fake analytics data
  • Loading branch information
eldblom authored Jun 8, 2017
2 parents 99118eb + 5cd2afe commit 6b1e89d
Show file tree
Hide file tree
Showing 768 changed files with 103,197 additions and 70,219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<initialize>
<processor type="Sitecore.Feature.Demo.Pipelines.RegisterWebApiRoutes, Sitecore.Feature.Demo" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
</initialize>
<startAnalytics>
<processor type="Sitecore.Feature.Demo.Pipelines.FakeTrackerData, Sitecore.Feature.Demo" method="GetFakeTrackerData" patch:before="processor[@type='Sitecore.Analytics.Pipelines.StartAnalytics.StartTracking, Sitecore.Analytics']" />
<processor type="Sitecore.Feature.Demo.Pipelines.FakeTrackerData, Sitecore.Feature.Demo" method="SetFakeTrackerData" patch:after="processor[@type='Sitecore.Analytics.Pipelines.StartAnalytics.StartTracking, Sitecore.Analytics']" />
</startAnalytics>
</pipelines>

</sitecore>
Expand Down
4 changes: 3 additions & 1 deletion src/Feature/Demo/code/Controllers/DemoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Sitecore.Feature.Demo.Controllers
using System.Net;
using System.Web.Mvc;
using Sitecore.Analytics;
using Sitecore.Data.Items;
using Sitecore.ExperienceEditor.Utils;
using Sitecore.ExperienceExplorer.Business.Managers;
using Sitecore.Feature.Demo.Models;
Expand Down Expand Up @@ -59,7 +60,8 @@ public ActionResult DemoContent()
throw new InvalidDataSourceItemException($"Item should be not null and derived from {nameof(Templates.DemoContent)} {Templates.DemoContent.ID} template");
}

return this.View("DemoContent", new DemoContent(item));
var demoContent = new DemoContent(item);
return this.View("DemoContent", demoContent);
}

public ActionResult EndVisit()
Expand Down
96 changes: 67 additions & 29 deletions src/Feature/Demo/code/Models/DemoContent.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,77 @@
namespace Sitecore.Feature.Demo.Models
{
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Foundation.SitecoreExtensions.Extensions;
using Sitecore.Text;
using static Configuration.Factory;
using System.Linq;
using Sitecore.Analytics.Model;
using Sitecore.Data.Items;
using Sitecore.ExperienceExplorer.Business.Utilities.Extensions;
using Sitecore.Foundation.SitecoreExtensions.Extensions;
using Sitecore.Text;
using static Configuration.Factory;

public class DemoContent
{
public Item Item { get; set; }

public DemoContent(Item item)
public class DemoContent
{
this.Item = item;
}
private WhoIsInformation _geoData;

public string HtmlContent
{
get
{
var content = this.Item[Templates.DemoContent.Fields.HTMLContent.ToString()];
return this.ReplaceTokens(content);
}
}
public DemoContent(Item item)
{
this.Item = item;
}

public Item Item { get; set; }

public string HtmlContent
{
get
{
var content = this.Item[Templates.DemoContent.Fields.HtmlContent];
return this.ReplaceTokens(content);
}
}

public string Referrer => this.Item[Templates.DemoContent.Fields.Referrer];
public string IpAddress => this.Item[Templates.DemoContent.Fields.IpAddress];

public WhoIsInformation GeoData => this._geoData ?? (this._geoData = this.GetGeoData());

private WhoIsInformation GetGeoData()
{
var lat = this.Item.GetFieldAsDouble(Templates.DemoContent.Fields.Latitude, 0);
var lon = this.Item.GetFieldAsDouble(Templates.DemoContent.Fields.Longitude, 0);

if (lat == 0 || lon == 0)
return null;

var geoData = new WhoIsInformation
{
Latitude = this.Item.GetFieldAsDouble(Templates.DemoContent.Fields.Latitude, 0),
Longitude = this.Item.GetFieldAsDouble(Templates.DemoContent.Fields.Longitude, 0),
AreaCode = this.Item[Templates.DemoContent.Fields.AreaCode],
BusinessName = this.Item[Templates.DemoContent.Fields.BusinessName],
City = this.Item[Templates.DemoContent.Fields.City],
Country = this.Item[Templates.DemoContent.Fields.Country],
Dns = this.Item[Templates.DemoContent.Fields.DNS],
Isp = this.Item[Templates.DemoContent.Fields.ISP],
MetroCode = this.Item[Templates.DemoContent.Fields.MetroCode],
PostalCode = this.Item[Templates.DemoContent.Fields.PostalCode],
Region = this.Item[Templates.DemoContent.Fields.Region],
Url = this.Item[Templates.DemoContent.Fields.Url],
IsUnknown = false
};
return geoData;
}

private string ReplaceTokens(string content)
{
var replacer = GetMasterVariablesReplacer();
using (new ReplacerContextSwitcher(this.GetReplacementTokens()))
return replacer.Replace(content, this.Item);
}
{
var replacer = GetMasterVariablesReplacer();
using (new ReplacerContextSwitcher(this.GetReplacementTokens()))
{
return replacer.Replace(content, this.Item);
}
}

private string[] GetReplacementTokens()
{
return this.Item.Children.Where(i => i.IsDerived(Templates.Token.ID)).SelectMany(i => new[] {$"${i.Name}", i[Templates.Token.Fields.TokenValue]}).ToArray();
private string[] GetReplacementTokens()
{
return this.Item.Children.Where(i => i.IsDerived(Templates.Token.ID)).SelectMany(i => new[] {$"${i.Name}", i[Templates.Token.Fields.TokenValue]}).ToArray();
}
}
}
}
15 changes: 10 additions & 5 deletions src/Feature/Demo/code/Models/Location.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace Sitecore.Feature.Demo.Models
{
public class Location
{
public string Country { get; set; }
public string City { get; set; }
}
public class Location
{
public string Country { get; set; }
public string City { get; set; }
public string BusinessName { get; set; }
public string Url { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string Region { get; set; }
}
}
15 changes: 8 additions & 7 deletions src/Feature/Demo/code/Models/Referral.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
namespace Sitecore.Feature.Demo.Models
{
using System.Collections.Generic;
using System.Collections.Generic;

public class Referral
{
public string ReferringSite { get; set; }
public int TotalNoOfCampaigns { get; set; }
public IEnumerable<Campaign> Campaigns { get; set; }
}
public class Referral
{
public string ReferringSite { get; set; }
public int TotalNoOfCampaigns { get; set; }
public IEnumerable<Campaign> Campaigns { get; set; }
public string Keywords { get; set; }
}
}
129 changes: 129 additions & 0 deletions src/Feature/Demo/code/Pipelines/FakeTrackerData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Sitecore.Feature.Demo.Pipelines
{
using System.Net;
using Sitecore.Analytics;
using Sitecore.Analytics.Model;
using Sitecore.Analytics.Pipelines.ParseReferrer;
using Sitecore.Feature.Demo.Models;
using Sitecore.Foundation.SitecoreExtensions.Extensions;
using Sitecore.Pipelines;

public class FakeTrackerData
{
private string FAKE_TRACKER_DATA = "Sitecore.Feature.Demo.FakeTrackerData";

public void GetFakeTrackerData(PipelineArgs args)
{
var trackerData = CreateTrackerData();
if (trackerData == null)
return;
HttpContext.Current.Session[FAKE_TRACKER_DATA] = trackerData;
}

private static TrackerData CreateTrackerData()
{
if (Sitecore.Context.Item == null || !Sitecore.Context.Item.IsDerived(Templates.DemoContent.ID))
return null;

var demoContent = new DemoContent(Sitecore.Context.Item);
var trackerData = new TrackerData();

InitializeReferrer(demoContent, trackerData);
InitializeIpAddress(demoContent, trackerData);
InitializeGeoData(demoContent, trackerData);
return trackerData;
}

private static void InitializeGeoData(DemoContent demoContent, TrackerData trackerData)
{
trackerData.GeoData = demoContent.GeoData;
}

private static void InitializeIpAddress(DemoContent demoContent, TrackerData trackerData)
{
IPAddress address;
if (!IPAddress.TryParse(demoContent.IpAddress, out address))
address = null;
trackerData.IpAddress = address;
}

private static void InitializeReferrer(DemoContent demoContent, TrackerData trackerData)
{
Uri referrerUri;
if (!Uri.TryCreate(demoContent.Referrer, UriKind.Absolute, out referrerUri))
referrerUri = null;
trackerData.Referrer = referrerUri;
}

public void SetFakeTrackerData(PipelineArgs args)
{
if (!Tracker.IsActive)
return;

if (!(HttpContext.Current.Session[FAKE_TRACKER_DATA] is TrackerData))
return;
var trackerData = (TrackerData)HttpContext.Current.Session[this.FAKE_TRACKER_DATA];
SetReferrer(trackerData);
SetIpAddress(trackerData);
SetLocation(trackerData);

HttpContext.Current.Session.Remove(FAKE_TRACKER_DATA);
}

private void SetLocation(TrackerData trackerData)
{
if (trackerData.GeoData == null)
return;

if (Tracker.Current.Interaction.HasGeoIpData && Tracker.Current.Interaction.GeoData.Latitude.HasValue)
return;
Tracker.Current.Interaction.SetGeoData(trackerData.GeoData);
Tracker.Current.Interaction.UpdateLocationReference();
}

private void SetIpAddress(TrackerData trackerData)
{
if (trackerData.IpAddress == null)
return;

if (Tracker.Current.Interaction.Ip != null && Tracker.Current.Interaction.HasGeoIpData && Tracker.Current.Interaction.GeoData.Latitude.HasValue)
return;
Tracker.Current.Interaction.Ip = trackerData.IpAddress.GetAddressBytes();
Tracker.Current.Interaction.UpdateGeoIpData();
}

private void SetReferrer(TrackerData trackerData)
{
if (trackerData.Referrer == null)
return;

//Only set the referrer if it hasn't been set previously - or if the previous referrer is the current site
if (Tracker.Current.Interaction.Referrer != null && !HttpContext.Current.Request.Url.Host.Equals(Tracker.Current.Interaction.ReferringSite, StringComparison.InvariantCultureIgnoreCase))
{
return;
}

Tracker.Current.Interaction.Referrer = trackerData.Referrer.ToString();
Tracker.Current.Interaction.ReferringSite = trackerData.Referrer.Host;
var args = new ParseReferrerArgs
{
UrlReferrer = trackerData.Referrer,
Interaction = Tracker.Current.Interaction
};
ParseReferrerPipeline.Run(args);
}

private class TrackerData
{
public Uri Referrer { get; set; }
public IPAddress IpAddress { get; set; }
public WhoIsInformation GeoData { get; set; }
}

}
}
45 changes: 27 additions & 18 deletions src/Feature/Demo/code/Repositories/LocationRepository.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
namespace Sitecore.Feature.Demo.Repositories
{
using Sitecore.Analytics;
using Sitecore.Analytics.Tracking;
using Sitecore.Feature.Demo.Models;
using System;
using System.Globalization;
using Sitecore.Analytics;
using Sitecore.Analytics.Tracking;
using Sitecore.Feature.Demo.Models;

internal class LocationRepository : Location
{
public Location GetCurrent()
internal class LocationRepository
{
return !Tracker.Current.Interaction.HasGeoIpData ? null : this.CreateLocation(Tracker.Current.Interaction.GeoData);
}
public Location GetCurrent()
{
return !Tracker.Current.Interaction.HasGeoIpData ? null : this.CreateLocation(Tracker.Current.Interaction.GeoData);
}

private Location CreateLocation(ContactLocation geoData)
{
if (geoData.Latitude == null || geoData.Longitude == null)
return null;
return new Location
{
City = geoData.City,
Country = geoData.Country
};
private Location CreateLocation(ContactLocation geoData)
{
if (geoData.Latitude == null || geoData.Longitude == null)
{
return null;
}
return new Location
{
BusinessName = geoData.BusinessName,
Url = geoData.Url,
City = geoData.City,
Region = geoData.Region,
Country = geoData.Country,
Latitude = string.Format(CultureInfo.InvariantCulture, "{0:0.#######}", geoData.Latitude),
Longitude = string.Format(CultureInfo.InvariantCulture, "{0:0.#######}", geoData.Longitude),
};
}
}
}
}
14 changes: 13 additions & 1 deletion src/Feature/Demo/code/Repositories/OutcomeRepository.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
namespace Sitecore.Feature.Demo.Repositories
{
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;
using Sitecore.Analytics;
using Sitecore.Analytics.Outcome;
using Sitecore.Analytics.Outcome.Extensions;
using Sitecore.Analytics.Outcome.Model;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Feature.Demo.Models;
using Sitecore.Foundation.Dictionary.Repositories;
using Sitecore.Marketing.Definitions;
Expand Down Expand Up @@ -63,7 +66,16 @@ private IEnumerable<IOutcome> GetCurrentOutcomes()

private IEnumerable<IOutcome> GetHistoricalOutcomes()
{
return Tracker.Current != null ? this.outcomeManager.GetForEntity<IOutcome>(new ID(Tracker.Current.Contact.ContactId)) : Enumerable.Empty<IOutcome>();
try
{
return Tracker.Current != null ? this.outcomeManager.GetForEntity<IOutcome>(new ID(Tracker.Current.Contact.ContactId)).ToList() : Enumerable.Empty<IOutcome>();
}
//The OutcomeManager is not very error safe and will throw misc exceptions if the MongoDb database is offline
catch (Exception exception)
{
Log.Debug($"GetHistoricalOutcomes failed. Mongo database is offline or reports an error: {exception.Message}", this);
return Enumerable.Empty<IOutcome>();
}
}

private static IOutcomeDefinition GetOutcomeDefinition(ID outcomeId)
Expand Down
Loading

0 comments on commit 6b1e89d

Please sign in to comment.