Created
March 14, 2021 00:45
-
-
Save agehlot/3380d550a35216c72572a15a63e4cdce to your computer and use it in GitHub Desktop.
Triggering a Sitecore goal programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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