Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinrayenglish/ba7665609e91092e8fa5134026925bf6 to your computer and use it in GitHub Desktop.
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
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