Skip to content

Instantly share code, notes, and snippets.

@islaytitans
Last active Dec 7, 2015
Embed
What would you like to do?
Sitecore MVC Controller used to regsiter Sitecore Goals and store interactions
public class PageEventController : Controller
{
private IKeyInteractionsRepository KeyInteractionsRepository = new KeyInteractionsRepository();
[System.Web.Mvc.HttpPost]
public JsonResult RegisterGoal(string goalId, string goalDescription)
{
Item eventItem = Sitecore.Context.Database.GetItem(goalId);
var goal = new PageEventItem(eventItem);
if (!Tracker.IsActive)
Tracker.StartTracking();
if (!Tracker.IsActive || Tracker.CurrentPage == null || goal == null)
return Json(new PageEventRequestResult()
{
Success = false,
Message = "Sitecore is unable to track this goal",
});
Sitecore.Analytics.Model.PageEventData eventData = Tracker.Current.CurrentPage.Register(goal);
eventData.Data = goal["Description"] + " " + goalDescription;
Tracker.Current.Interaction.AcceptModifications();
return Json(new PageEventRequestResult()
{
Success = true,
Message = "Successfully registered goal",
});
}
[System.Web.Mvc.HttpPost]
public JsonResult RegisterSwatchDownload(string rangeCode, string decorCode)
{
if (!Tracker.IsActive)
Tracker.StartTracking();
if (!Tracker.IsActive || Tracker.Current.Contact == null)
return Json(new PageEventRequestResult()
{
Success = false,
Message = "Sitecore is unable to track at this time",
});
KeyInteractionsRepository.Write(Tracker.Current.Contact, new KeyInteractionsModel()
{
SwatchesDownloaded = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>(rangeCode, decorCode)
}
});
return Json(new PageEventRequestResult()
{
Success = true,
Message = "Successfully registered interaction"
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment