Sitecore Live Profile access programmatically
To access on going contact triggered Profile without waiting till session end.
public class ProfileScoreAndPointsList
{
public ID ProfileID { get; set; }
public string
ProfileKeyName { get; set; }
public double
ProfileKeyScore { get; set; }
}
public
Dictionary<ID, List<ProfileScoreAndPointsList>>
GetTrackerInteractionProfiles()
{
Dictionary<ID, List<ProfileScoreAndPointsList>> allcategoryProfiles = new Dictionary<ID, List<ProfileScoreAndPointsList>>();
if (Tracker.Current?.Interaction != null)
{
VisitData visitdata = Tracker.Current.Interaction.ToVisitData();
Dictionary<string, ProfileData> data = visitdata.Profiles;
if (data.Any())
{
foreach (KeyValuePair<string, ProfileData> profileCollections in data)
{
List<ProfileScoreAndPointsList> profileScoreLists = new List<ProfileScoreAndPointsList>();
//Profiles example:(Sector)
ProfileData value = profileCollections.Value;
Dictionary<string, double> profileKeyValue = value.Values;
foreach (KeyValuePair<string, double> profile in
profileKeyValue)
{
//Profile Keys example:(Aviation)
{
public ID ProfileID { get; set; }
Dictionary<ID, List<ProfileScoreAndPointsList>> allcategoryProfiles = new Dictionary<ID, List<ProfileScoreAndPointsList>>();
VisitData visitdata = Tracker.Current.Interaction.ToVisitData();
Dictionary<string, ProfileData> data = visitdata.Profiles;
foreach (KeyValuePair<string, ProfileData> profileCollections in data)
List<ProfileScoreAndPointsList> profileScoreLists = new List<ProfileScoreAndPointsList>();
ProfileData value = profileCollections.Value;
Dictionary<string, double> profileKeyValue = value.Values;
//Profile Keys example:(Aviation)
///sitecore/system/Marketing Control Panel/Profiles
string query = $"{SitecoreSettings.ProfilePath}/descendant::*[@@key ='{profile.Key.ToLower()}']";
Item profileKeyItem
= Sitecore.Context.Database.SelectSingleItem(query);
if (profileKeyItem != null)
{
string profilename = profile.Key;
double profilevalue = profile.Value;
if (!string.IsNullOrEmpty(profilename))
{
//If already have the Profile key data then appened Score
ProfileScoreAndPointsList profileItem = profileScoreLists.FirstOrDefault(p => p.ProfileKeyName.Equals(profile.Key));
if (profileItem != null)
{
//Getting the profile max score for contact's profile from latest interaction
int index = profileScoreLists.IndexOf(profileItem);
ProfileScoreAndPointsList profileDetail =
profileScoreLists.ElementAt(index);
profileDetail.ProfileKeyName = profilename;
profileDetail.ProfileKeyScore = profileItem.ProfileKeyScore + profilevalue;
profileDetail.ProfileID = profileKeyItem.ID;
}
else
{
//First time add the Score for the profile key
profileScoreLists.Add(new ProfileScoreAndPointsList()
{
//ProfileID = ID.Parse(profile.Key),
ProfileKeyScore = profilevalue,
ProfileKeyName = profilename,
ProfileID = profileKeyItem.ID
});
}
}
}
}
string query = $"{SitecoreSettings.ProfilePath}/descendant::*[@@key ='{profile.Key.ToLower()}']";
if (profileKeyItem != null)
string profilename = profile.Key;
//If already have the Profile key data then appened Score
ProfileScoreAndPointsList profileItem = profileScoreLists.FirstOrDefault(p => p.ProfileKeyName.Equals(profile.Key));
if (profileItem != null)
//Getting the profile max score for contact's profile from latest interaction
int index = profileScoreLists.IndexOf(profileItem);
profileDetail.ProfileKeyName = profilename;
profileDetail.ProfileKeyScore = profileItem.ProfileKeyScore + profilevalue;
profileDetail.ProfileID = profileKeyItem.ID;
}
else
{
//First time add the Score for the profile key
profileScoreLists.Add(new ProfileScoreAndPointsList()
//ProfileID = ID.Parse(profile.Key),
ProfileKeyScore = profilevalue,
ProfileKeyName = profilename,
ProfileID = profileKeyItem.ID
});
}
}
}
//sitecore/system/Marketing Control Panel/Profiles
string queryProfile = $"{ SitecoreSettings.ProfilePath}/descendant::*[@@key ='{profileCollections.Key.ToLower()}']";
Item profileRootItem =
Sitecore.Context.Database.SelectSingleItem(queryProfile);
if (profileRootItem != null)
{
allcategoryProfiles.Add(profileRootItem.ID, profileScoreLists);
}
}
}
}
return allcategoryProfiles;
}
string queryProfile = $"{ SitecoreSettings.ProfilePath}/descendant::*[@@key ='{profileCollections.Key.ToLower()}']";
if (profileRootItem != null)
allcategoryProfiles.Add(profileRootItem.ID, profileScoreLists);
}
}
}
}
return allcategoryProfiles;
Comments
Post a Comment