Try our conversational search powered by Generative AI!

Get active Visitor Group( and "print" to DOM.)


I am trying to get a hold of which Visitor Group is active. The ultimate goal is to send it as a dimension to Google Analytics. First step here though is just to see if I can catch which Visitor Group is triggered. On my front page I currently have a test Visitor Group that is always triggered so that is the one I am expecting to see.

I can get my visitor groups via this:

var visitorGroup = new VisitorGroupStore(this.DataStore, ServiceLocator.Current.GetInstance<>ISynchronizedObjectInstanceCache>()).List();

This show me all (5) visitor groups that I have made. I am now missing the link on how to see if a visitor group is active or not.

I am wondering if "PrincipalInfo" somehow should be used here?

I have a controller with a method that returns me a partial view with a string model that hopefully will contain the active visitor group(s).

Is there any of you that can point me in the right direction?

Apr 04, 2017 15:42


If you have your visitor group, something like this should be able to tell you if the visitor group is active for the current request:

VisitorGroupRole virtualRoleObject;
if (_visitorGroupRoleRepository.TryGetRole(visitorGroup.Name, out virtualRoleObject))
  return virtualRoleObject.IsMatch(httpContext.User, httpContext);


Per Gunsarfs

Apr 04, 2017 16:52

Thanks for the suggestion Per! I will try and implement this. Will come back and mark it if(when) I get it to work :-)

Apr 05, 2017 9:00

This part:

virtualRoleObject.IsMatch(httpContext.User, httpContext)

Is giving me some issues, I have implemented an IPrincipal called Principal which I use at the httpContext.User part - since it wants an IPrincipal. But the secondary part wants a HttpContextBase. But if I create one it wont accept it..

How do I use these? Do you have a working example I could take a look at - where you see if a Visitor Group is triggered or not?

I really appriciate any help.

Apr 05, 2017 11:06

This code was from an example where we check if a visitor group would be applied to a specific request. The easiest way to get those arguments is to use the HttpContext.Current. Something like:

var httpContext = new HttpContextWrapper(HttpContext.Current);

The HttpContextWrapper is simply there to wrap the HttpContext into an HttpContextBase.

Apr 05, 2017 11:19

Thanks for you help! It works now. I made your first suggestion work :-)

Apr 05, 2017 12:27

Great, thanks for the feedback!

Apr 05, 2017 12:32
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.