Sitecore Trigger Goal programmatically using Ajax and with out Ajax
Sitecore provides different ways to collect the user interactions like goals,outcomes,profiles, event etc
There are different ways to trigger these. one of the most commonly used scenario is to trigger Goals programmatically.
- Ajax way
- Non Ajax
The below code triggers goals through ajax function and non ajax function
public void TriggerGoals(string GoalName, bool IsAjaxRequest)
{
if (!Sitecore.Analytics.Tracker.IsActive)
{
Sitecore.Analytics.Tracker.StartTracking();
}
if (Sitecore.Analytics.Tracker.Current.CurrentPage != null)
{
{
if (!Sitecore.Analytics.Tracker.IsActive)
Sitecore.Analytics.Tracker.StartTracking();
}
if (Sitecore.Analytics.Tracker.Current.CurrentPage != null)
///sitecore/system/Marketing Control Panel/Goals/
string query = $"fast:{SitecoreSettings.GoalPath}/descendant::*[@@key ='{ GoalName.ToLower()}']";
Item goalitem =
Sitecore.Context.Database.SelectSingleItem(query);
if (goalitem != null)
{
Sitecore.Marketing.Definitions.PageEvents.IPageEventDefinition ev1 = Sitecore.Analytics.Tracker.MarketingDefinitions.PageEvents[goalitem.ID.ToGuid()];
Sitecore.Analytics.Data.PageEventData pageData = new Sitecore.Analytics.Data.PageEventData(ev1.Alias, ev1.Id)
{
Data = goalitem["Name"],
ItemId =
goalitem.ID.ToGuid(),
DataKey = goalitem.Paths.Path,
Text = goalitem["Description"]
};
if (IsAjaxRequest)
{
IPageContext page = Tracker.Current.Session.Interaction?.PreviousPage;
if (page == null)
{
string localPath = HttpContext.Current.Request.UrlReferrer?.LocalPath;
page =
Sitecore.Analytics.Tracker.Current.Interaction.GetPages().LastOrDefault(n =>
n.Url.Path == localPath);
page.Register(pageData);
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
Sitecore.Analytics.Tracker.Current.CurrentPage.Cancel();
}
else
{
page?.Register(pageData);
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
}
}
else
{
Sitecore.Analytics.Tracker.Current.CurrentPage.Register(pageData);
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
}
}
}
}
Happy programming :)
string query = $"fast:{SitecoreSettings.GoalPath}/descendant::*[@@key ='{ GoalName.ToLower()}']";
if (goalitem != null)
Sitecore.Marketing.Definitions.PageEvents.IPageEventDefinition ev1 = Sitecore.Analytics.Tracker.MarketingDefinitions.PageEvents[goalitem.ID.ToGuid()];
Sitecore.Analytics.Data.PageEventData pageData = new Sitecore.Analytics.Data.PageEventData(ev1.Alias, ev1.Id)
Data = goalitem["Name"],
DataKey = goalitem.Paths.Path,
Text = goalitem["Description"]
if (IsAjaxRequest)
IPageContext page = Tracker.Current.Session.Interaction?.PreviousPage;
if (page == null)
string localPath = HttpContext.Current.Request.UrlReferrer?.LocalPath;
page.Register(pageData);
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
Sitecore.Analytics.Tracker.Current.CurrentPage.Cancel();
}
else
{
page?.Register(pageData);
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
}
}
else
{
Sitecore.Analytics.Tracker.Current.CurrentPage.Register(pageData);
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
}
}
}
}
Happy programming :)
Comments
Post a Comment