2

Is there a way in c# to get the name of the last goal triggered by the current visitor?

A simple conceptual example: Bob visitors the homepage and clicks on a button (triggering a goal called "my button click" Bob then fills out a contact form (triggering a goal called "contact form submitted")

Is there a good way in my back-end code (given I know Bob is current looking at the site) to get the fact that Bob's last triggered goal was "contact form submitted"?

I figure if that's not possible, I might be able to store a custom field against the visitor when the goal is triggered - but that seems unnecessary as the information should already be in the Mongo DB?

Solution is on Sitecore 7.5.

5

You can use the tracker api to retrieve the latest Page Event:

    Tracker.Current.Session.Interaction.Pages.SelectMany(x=>x.PageEvents)
            .OrderByDescending(x=>x.Timestamp).FirstOrDefault()

This is not tested code, let me know whether it works for you.

Update:

If you want to get only goals, they can be filtered by adding this LINQ condition:

.Where(x => x.IsGoal)

2
  • Thanks Andrey, I had to make a small change to filter out only the Goals. I'm also worried that a session where the user visits loads of pages will make this inefficient - considering using a different approach but will keep this as a fallback :) var latestGoal = Tracker.Current.Session.Interaction.Pages. SelectMany(x => x.PageEvents).Where(x => x.IsGoal).OrderByDescending(x => x.Timestamp).FirstOrDefault(); Mar 13 '17 at 13:19
  • @OwenNiblock :) i have added the goals condition to the initial answer Mar 15 '17 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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