This repository has been archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from Sitecore/feature/fake-tracking
Fake analytics data
- Loading branch information
Showing
768 changed files
with
103,197 additions
and
70,219 deletions.
There are no files selected for viewing
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
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
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
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(); | ||
} | ||
} | ||
} | ||
} |
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
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; } | ||
} | ||
} |
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
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; } | ||
} | ||
} |
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
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; } | ||
} | ||
|
||
} | ||
} |
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
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), | ||
}; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.