Skip to content

Instantly share code, notes, and snippets.

@robearlam
Created April 15, 2017 03:31
Show Gist options
  • Save robearlam/c6f2ca5d7e17cf4e97c1f4a5d1724494 to your computer and use it in GitHub Desktop.
Save robearlam/c6f2ca5d7e17cf4e97c1f4a5d1724494 to your computer and use it in GitHub Desktop.
List all Sitecore contacts that have completed a goal
public class HasContactCompletedGoal<T> : TypedQueryableStringOperatorCondition<T, IndexedContact> where T : VisitorRuleContext<IndexedContact>
{
private const string IndexField = "Contact.CompletedGoals";
private const string ContainsOperatorId = "{2E67477C-440C-4BCA-A358-3D29AED89F47}";
public HasContactCompletedGoal()
{
OperatorId = ContainsOperatorId;
}
protected override Expression<Func<IndexedContact, bool>> GetResultPredicate(T ruleContext)
{
ID valueAsId;
var parsedValueToId = ID.TryParse(Value, out valueAsId);
ShortID valueAsShortId;
var parsedValueToShortId = ShortID.TryParse(Value, out valueAsShortId);
if (!parsedValueToId && !parsedValueToShortId)
{
return c => false;
}
if (parsedValueToShortId && valueAsId == (ID)null)
{
valueAsId = valueAsShortId.ToID();
}
var idConvertedToString = valueAsId.Guid.ToString("N").ToLowerInvariant();
return GetCompareExpression(c => c[IndexField], idConvertedToString);
}
}
public class GoalsCompletedField : IComputedIndexField
{
private const string GoalsViewName = "goals";
public string FieldName { get; set; }
public string ReturnType { get; set; }
public object ComputeFieldValue(IIndexable indexable)
{
var contactIndexable = indexable as IContactIndexable;
if (contactIndexable == null)
return null;
var contactId = (Guid)contactIndexable.Id.Value;
if (contactId == Guid.Empty)
return null;
var viewParams = new ViewParameters
{
ContactId = contactId,
ViewName = GoalsViewName,
ViewEntityId = null
};
var resultSet = CustomerIntelligenceManager.ViewProvider.GenerateContactView(viewParams);
return resultSet.Data.Dataset[GoalsViewName].Rows
.Cast<DataRow>()
.Select(GetGoalIdFromDataRow())
.Distinct();
}
private static Func<DataRow, object> GetGoalIdFromDataRow()
{
return dataRow => dataRow[2];
}
}
protected override bool Execute(T ruleContext)
{
Assert.ArgumentNotNull((object) ruleContext, "ruleContext");
Assert.IsNotNull((object) Tracker.Current, "Tracker.Current is not initialized");
Assert.IsNotNull((object) Tracker.Current.Session, "Tracker.Current.Session is not initialized");
Assert.IsNotNull((object) Tracker.Current.Session.Interaction, "Tracker.Current.Session.Interaction is not initialized");
if (!this.GoalGuid.HasValue)
return false;
if (this.HasEventOccurredInInteraction((IInteractionData) Tracker.Current.Session.Interaction))
return true;
Assert.IsNotNull((object) Tracker.Current.Contact, "Tracker.Current.Contact is not initialized");
return this.FilterKeyBehaviorCacheEntries(Tracker.Current.Contact.GetKeyBehaviorCache()).Any<KeyBehaviorCacheEntry>((Func<KeyBehaviorCacheEntry, bool>) (entry =>
{
Guid id = entry.Id;
Guid? goalGuid = this.GoalGuid;
if (goalGuid.HasValue)
return id == goalGuid.GetValueOrDefault();
return false;
}));
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="sitecore_analytics_index" >
<configuration>
<documentOptions>
<fields hint="raw:AddComputedIndexField">
<field fieldName="Contact.CompletedGoals"
type="GoalCompletionReporting.Business.Search.GoalsCompletedField, GoalCompletionReporting.Business"
matchField="type"
matchValue="contact"
separator="" />
</fields>
</documentOptions>
</configuration>
</index>
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>
Copy link

ghost commented Sep 12, 2018

I've upgraded my solution to sitecore 9. After upgrade Sitecore.Analytics.Rules.SegmentBuilder is not accessible and TypeQueryableStringOperationCondtion is also not accessable. Please help, how to resolve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment