OK I have got it working for anyone else that comes into this problem:
// Signs out the current user
AuthenticationManager.SignOut(
DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
Session.Abandon();
//Removes current contact (will explain this below)
ContactManagementContext.Clear();
//Manage my consent and set the current contact to visitor
var consent = ConsentInfoProvider.GetConsentInfo(ResourceConstants.ConsentPolicyName);
currentCookieLevelProvider.SetCurrentCookieLevel((int)KenticoHelpers.GetCookieLevel(1));
//create new anonymous contact
var newAnomContact = new ContactInfo()
{
ContactLastName = $"Anonymous - {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}",
ContactMonitored = true
};
//assign anonymous contact to current contact and accepts t&c's
ContactInfoProvider.SetContactInfo(newAnomContact);
mcurrentContactProvider.SetCurrentContact(newAnomContact); //Instance of ICurrentContactProvider
consentAgreementService.Agree(newAnomContact, consent);
So while very frustratingly working my way through the contact management I eventually solved the creation of the anonymous contact and setting them to current.
There were 3 major problems
-
The lack of info on the connections between all the contact management portions e.g. ContactManagementContext being able to get the current contact but not set it or ICurrentContactProvider being able to set the current contact but not get it without a user.
-
The problem that if a ContactManagementContext.Clear() removes the current contact but is only required because if the current contact isn't cleared ICurrentContactProvider.SetCurrentContact() does not take importance and while it appears to create a new contact fine all the activities are still logged as the previous contact
-
The fact that if ContactMonitored is not set to true the activities aren't logged for that contact (you would quite reasonably assume that it is either dependent on a separate value the consentagreementservice sets or that the consentagreementservice would set the contactmonitored to true)
I cannot believe that it is actually taking so much individual actions and about a day or two of research to mostly luck my way through what should really be a pretty reasonable action.