Accessing Current Visitor Information

In order to access Visitor data in the Engagement Analytics API, you primarily use the following classes:

  • Sitecore.Analytics.Tracker
  • Sitecore.Analytics.TrackerDataContext

The following namespace using directives will be helpful when accessing this data.

  • using Sitecore.Analytics;
  • using Sitecore.Analytics.DataAccess;
  • using Sitecore.Analytics.DataAccess.DataSets;

How to Access GeoIP Data

The Sitecore.Analytics.Tracker is a static class that is used to track each visitor. It provides access to the Analytics context, exposing the visitors IP address, country, city, zip code, latitude, longitude and more. The follow example demonstrates how to access the current visitors IP address.

  1. using Sitecore.Analytics;
  2. using Sitecore.Analytics.DataAccess;
  3. using Sitecore.Analytics.DataAccess.DataSets;
  4. public class GeoIPTracker : Sitecore.Web.UI.WebControl
  5. {
  6. protected override void DoRender(System.Web.UI.HtmlTextWriter output)
  7. {
  8. string ip = new IPAddress(Tracker.CurrentVisit.Ip).ToString();
  9. if (Tracker.CurrentVisit == null)
  10. return;
  11. if (!Tracker.CurrentVisit.UpdateGeoIpData())
  12. output.Write("GeoIP information not " + "available within prescribed time.<br/>");
  13. else if (Tracker.CurrentVisit.BusinessName == "IP_NOT_FOUND" || Tracker.CurrentVisit.BusinessName == "N/A")
  14. output.Write("GeoIP information not avaialble for " + ip + ".<br/>");
  15. else if (String.IsNullOrEmpty(Tracker.CurrentVisit.BusinessName))
  16. output.Write("No business name in GeoIP data for " + ip + " (error contacting provider).<br/>");
  17. else
  18. output.Write("Business name from GeoIP record: " + Tracker.CurrentVisit.BusinessName + ".<br/>");
  19. }
  20. }

How to Access Session Data

Beyond Geo IP data, Sitecore stores valuable session data that is accessible via the Analytics API. The following code demonstrates how to access information about any search engine keywords that are associated with the Visitor's current session or previous 10 sessions, if any:

  1. using Sitecore.Analytics;
  2. using Sitecore.Analytics.Data.DataAccess;
  3. using Sitecore.Analytics.Data.DataAccess.DataSets;
  4. public class ExampleTracker : Sitecore.Web.UI.WebControl
  5. {
  6. protected override void DoRender(System.Web.UI.HtmlTextWriter output)
  7. {
  8. if (Tracker.CurrentVisit == null)
  9. return;
  10. if (Tracker.CurrentVisit.Keywords != null && !String.IsNullOrEmpty(Tracker.CurrentVisit.Keywords.Text))
  11. {
  12. output.WriteLine("Search keywords for current visit: " + Tracker.CurrentVisit.Keywords.Text + ".<br/>");
  13. return;
  14. }
  15. const int checkVisits = 10;
  16. Sitecore.Analytics.Data.DataAccess.VisitorLoadOptions vOptions = new Sitecore.Analytics.Data.DataAccess.VisitorLoadOptions
  17. {
  18. Start = Tracker.CurrentVisit.VisitorVisitIndex - 1,
  19. Count = Tracker.CurrentVisit.VisitorVisitIndex - checkVisits,
  20. VisitLoadOptions = VisitLoadOptions.Visits
  21. };
  22. foreach (VisitorDataSet.VisitsRow visit in Tracker.Visitor.GetVisits(vOptions)
  23. .Where(visit => visit.VisitId != Tracker.CurrentVisit.VisitId).OrderByDescending(visit => visit.VisitorVisitIndex))
  24. {
  25. if (visit.Keywords != null && !String.IsNullOrEmpty(visit.Keywords.Text))
  26. {
  27. output.WriteLine("Last search keywords from " + visit.StartDateTime + " visit: " + visit.Keywords.Text + "<br/>");
  28. return;
  29. }
  30. }
  31. output.WriteLine("No search keywords for current or last " + checkVisits + " visits.<br/>");
  32. }
  33. }