3

I'm working on a site design that has a set of news headlines in the "hero" area, and when you click an article you don't get taken to that page, you're jumped down to an anchor that has the article in a detail area. However, we want to track it as if the news article page had been visited.

Looking at the tracker code and the documentation in Sitecore, there are references to Sitecore.Analytics.Tracker.Current.CurrentPage, but I don't see how to specify that I want to track to something other than the current page (so I could do like Sitecore.Analytics.Tracker.Current.[SpecifiedPage].RegisterPageEvent).

Is there a entry point to pass in the item ID I want to track to, and then do the page event registration there?

1 Answer 1

4

You can use the code below to add a page to the interactions collection. The pageGuid is the Item ID of the page you want to simulate.

NOTE: we are canceling the current page because I am calling all this from an API. And the API page is being tracked as a page in the interactions.

private void TrackSection(ID pageGuid)
{
    if (Tracker.Current.Session.Interaction.Pages.All(x => x.Item.Id != pageGuid.Guid))
    {
        Tracker.Current?.CurrentPage?.Cancel();
        Tracker.Current.Session.Interaction.AcceptModifications();

        var page = Tracker.Current.Session.Interaction.CreatePage();
        page.SetItemProperties(pageGuid.Guid, "en-us", 1);
        page.SetUrl(Sitecore.Context.Database.GetItem(pageGuid).Url());
    }
}

Then you can trigger the goal on the "current page" which is the page you added above.

public void TriggerGoal(ID goalId)
{
    var goalDefinition = this.goalDefinitionManager.Get(goalId.Guid, CultureInfo.InvariantCulture);
    if (goalDefinition == null)
    {
        Log.Warn($"Cannot find goal: {goalId}", this);
        return;
    }

    Log.Info($"Goal Triggered: Contact: {Tracker.Current.Contact.ContactId} : {goalId}", this);

    var page = Tracker.Current.CurrentPage;

    page.RegisterGoal(goalDefinition);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.