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?
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:
if (_visitorGroupRoleRepository.TryGetRole(visitorGroup.Name, out virtualRoleObject))
return virtualRoleObject.IsMatch(httpContext.User, httpContext);
Thanks for the suggestion Per! I will try and implement this. Will come back and mark it if(when) I get it to work :-)
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.
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.
Thanks for you help! It works now. I made your first suggestion work :-)
Great, thanks for the feedback!