Created
June 21, 2020 12:10
-
-
Save martinrayenglish/ba7665609e91092e8fa5134026925bf6 to your computer and use it in GitHub Desktop.
Sitecore xConnect XConnectDataAdapterProvider extension to only goals and other events specified in configuration
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading; | |
using System.Xml; | |
using Sitecore.Analytics.Model; | |
using Sitecore.Analytics.XConnect.DataAccess; | |
using Sitecore.Framework.Conditions; | |
using Sitecore.Xml; | |
namespace MyProject.Analytics.XConnect.DataAccess | |
{ | |
public class CustomXConnectDataAdapterProvider : XConnectDataAdapterProvider | |
{ | |
private bool FilteringEnabled { get; set; } | |
private bool IncludeGoals { get; set; } | |
private Guid[] _pageEventDefinitions = Array.Empty<Guid>(); | |
public override void SaveVisit(VisitData visit) | |
{ | |
Condition.Requires(visit, nameof(visit)).IsNotNull(); | |
if (FilteringEnabled) | |
{ | |
var events = visit.Pages.SelectMany(x => x.PageEvents).ToList(); | |
if (IncludeGoals) | |
{ | |
if (!events.Any(x => x.IsGoal) && !events.Any(x => _pageEventDefinitions.Contains(x.PageEventDefinitionId))) | |
return; | |
} | |
else | |
{ | |
if (!events.Any(x => _pageEventDefinitions.Contains(x.PageEventDefinitionId))) | |
return; | |
} | |
} | |
base.SaveVisit(visit); | |
} | |
private void AddFilterEvents(XmlNode configNode) | |
{ | |
Condition.Requires(configNode.Name, "configNode.Name").IsEqualTo("PageEventDefinitionId"); | |
AddFacet(new Guid(XmlUtil.GetAttribute("Id", configNode))); | |
} | |
private void AddFacet(Guid id) | |
{ | |
var source = new HashSet<Guid>(_pageEventDefinitions); | |
if (!source.Add(id)) | |
return; | |
Interlocked.Exchange(ref _pageEventDefinitions, source.ToArray()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment