Skip to content

Instantly share code, notes, and snippets.

@agehlot
Created March 14, 2021 00:45
Show Gist options
  • Save agehlot/3380d550a35216c72572a15a63e4cdce to your computer and use it in GitHub Desktop.
Save agehlot/3380d550a35216c72572a15a63e4cdce to your computer and use it in GitHub Desktop.
Triggering a Sitecore goal programmatically
namespace Core.Foundation.SitecoreExtensions.Helpers
{
/// <summary>
/// Class MarketingActivityHelper.
/// </summary>
public class MarketingActivityHelper
{
/// <summary>
/// Triggers the goal.
/// </summary>
/// <param name="goalId">The goal identifier.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
public static bool TriggerGoal(Sitecore.Data.ID goalId)
{
//Check if tracker is active or not
if (!Sitecore.Analytics.Tracker.IsActive)
{
Sitecore.Analytics.Tracker.StartTracking();
}
if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.Current.CurrentPage != null)
{
Sitecore.Data.Items.Item goalItem = Sitecore.Context.Database.GetItem(goalId);
if (goalItem != null)
{
var goalTrigger = Sitecore.Analytics.Tracker.MarketingDefinitions.Goals[goalItem.ID.ToGuid()];
var goalEventData = Sitecore.Analytics.Tracker.Current.CurrentPage.RegisterGoal(goalTrigger);
goalEventData.Data = goalItem["Name"];
goalEventData.ItemId = goalItem.ID.ToGuid();
goalEventData.DataKey = goalItem.Paths.Path;
goalEventData.Text = "Goal for Logic";
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
}
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment