Skip to content

Instantly share code, notes, and snippets.

@hishaamn
Created May 17, 2018 17:12
Show Gist options
  • Select an option

  • Save hishaamn/3bd115e682f9decfba3743bfc15f7c4d to your computer and use it in GitHub Desktop.

Select an option

Save hishaamn/3bd115e682f9decfba3743bfc15f7c4d to your computer and use it in GitHub Desktop.
public void CreateClickContentEvent(Guid itemId)
{
if (Tracker.IsActive && Tracker.Current != null)
{
Assert.IsNotNull(Tracker.Current, "Tracker.Current");
if (Tracker.Current != null)
{
Assert.IsNotNull(Tracker.Current.Session, "Tracker.Current.Session");
CurrentInteraction interaction = Tracker.Current.Session.Interaction;
Assert.IsNotNull(interaction, "Tracker.Current.Session.Interaction");
Assert.IsNotNull(interaction.CurrentPage, "Tracker.Current.Session.Interaction.CurrentPage");
}
var clickEventName = $"{itemId}_click";
var itemPath = $"/sitecore/system/Settings/Analytics/Page Events/Content Events Click/{clickEventName}";
var clickEventItem = Context.Database.GetItem(itemPath, Language.Parse("en"));
if (clickEventItem != null)
{
PageEventData pageEventData = new PageEventData(clickEventName, clickEventItem.ID.ToGuid())
{
ItemId = itemId,
Data = itemId.ToString(),
DataKey = itemId.ToString("B"),
PageEventDefinitionId = clickEventItem.ID.ToGuid(),
Text = "Clicked",
Name = clickEventName
};
if (Tracker.Current.Contact != null)
{
try
{
Tracker.Current.Interaction.CurrentPage.Register(pageEventData);
}
catch (Exception)
{
Log.Error($"Error on register tracking... ", "CreatePageEventWithGoal");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment