Goal registration heavily relies on the current session. I am not aware of a good way to properly register a goal outside of actual session, page, and request being available.
To enroll a contact into a specific state in an engagement plan, you can try using this API:
bool success = AutomationContactManager.AddContact(
new Dictionary<string, object>());
This method will return false in the following cases:
- The contact with given ID does not exist;
- The database is unavailable;
- The contact is locked by another process;
- The contact is already in the given state;
- The given automation state does not exist;
- An exception occurs (it will be logged).
To check if a contact is already enrolled in a certain plan, you can use the following code:
Contact contact = ...
var manager = AutomationStateManager.Create(contact);
bool isInPlan = manager.IsInEngagementPlan(planId);