Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Visitor group tracking

Describes how to setup visitor group tracking.

If you want to see a report about users who "visited the Contact Us page," you can create a visitor group with that criteria, and then track the visitor group. To filter by visitor groups, you need to update your solution’s tracking events to send the user’s visitor group information, because the standard installation currently does not add this tracking event by default.

EPiServer.Tracking.Cms 1.9.0 provides a new extension method: ITrackingService.TrackVisitorGroup(TrackingData<VisitorGroupTrackingDataWrapper>, HttpContextBase). Calling this method sends a track event with visitor group track data in the payload.

📘

Note

This is similar to calling the ITrackingService.Track<TPayload>(TrackingData<TPayload>, HttpContextBase) method with TPayload is VisitorGroupTrackingData (which is also exposed in EPiServer.Tracking.Cms 1.9.0).

Visitor groups tracking data validation notes

  • If an error occurs during validation of visitor group track data, then the track event is dropped when processing, rather than logging the error.
  • By calling the extension method, it sets (and overrides) the track EventType to epiVisitorGroup (which is a reserved event type name for visitor group tracking data), and event processing is based on this value to consolidate visitor group data into Profile.
  • By calling the extension method, it sets (and overrides) the track Value to Include: [Include item]. Exclude: [Exclude item]. For example: Include: VG1, VG2, VG3. Exclude: VG4, VG5.

Optimizely allows checking only if an IPrincipal is matched to a specific visitor group. Optimizely does not allow getting all visitor groups of an IPrincipal.

Visitor group track data contains the following information items:

  • IncludeVisitorGroups contains newly matched visitor groups of an IPrincipal (of a user or anonymous who is being track).
  • ExcludeVisitorGroups contains visitor groups that recently did not match an identity.

For example, we created a new visitor group named: "Sample Visitor" if you check (under an IPrincipal) whether a user belongs to a visitor group (using VisitorGroupHelper.IsPrincipalInGroup), a match places the user on the IncludeVisitorGroups list; no match places the user on the ExcludeVisitorGroups list.

So, when a user is no longer considered a member of "Sample Visitor" , then the user is tracked again with "Sample Visitor" being on the ExcludeVisitorGroups list, removing new status from the user's Profile when the tracking event is later processed.

Sample code:

public class StartController : PageController<StartPage>
{
    public StartController()
    {
    
    }

    public async Task<ViewResult> Index()
    {
        var user = HttpContext.User;
        var visitorGroupHelper = new VisitorGroupHelper();

        var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
        var trackingService = ServiceLocator.Current.GetInstance<ITrackingService>();

        var visitorGroups = visitorGroupRepository.List();
        if (visitorGroups.Any())
        {
            //Get first item as a sample VisitorGroup
            var sampleVisitorGroup = visitorGroups.ToList().First();
        
            var isMatch = visitorGroupHelper.IsPrincipalInGroup(user, sampleVisitorGroup.Name);
            if (isMatch)
            {
                var trackData = new TrackingData<VisitorGroupTrackDataWrapper>
                {
                    Payload = new VisitorGroupTrackDataWrapper
                    {
                        Epi = new VisitorGroupTrackingData
                        {
                            IncludeVisitorGroups = new [] { sampleVisitorGroup.Name }, //matched group
                            ExcludeVisitorGroups = Enumerable.Empty<string>()
                        }
                    }
                };

                await trackingService.TrackVisitorGroupAsync(trackData, HttpContext);
            }
        }

        var viewModel = new StartPageViewModel();
        return View(viewModel);
    }
}