Using Sitecore FXM to update and track contacts
Sitecore Federated Experience Manager aka FXM is a great feature to utilize some of the Sitecore's CMS, marketing and analytics capabilities on non-Sitecore websites. If you are new to FXM you can read more about how to integrate FXM into your non-Sitecore website and utilize its capabilities on Sitecore's FXM documentation page.
You can read more about these methods in the documentation.
As you can see there is no out of the box API method to identify or update contact. In cases where you have a non-authenticated public website on Sitecore but an authenticated member portal on non-Sitecore website it would be really useful to pass the authenticated user information to Sitecore to identify and store the contact information which will be valuable for personalization.
As there is no out of the box API for this we can piggyback on one of the out of the box API methods, overwrite its processor and add our extra logic to identify and save contact. Out of the three methods, trackEvent is best suited for this purpose because, a page event can be excluded from activity in Experience Profile, given this event is not an actual page event we wouldn't want this event to show up in Experience Profile dashboard. If we set the points on the event to zero it won't affect the monitory value of the visit either.
Let's begin with creating a new page event folder called FXM under /sitecore/system/Settings/Analytics/Page Events in master database. Now create a new page event called Update Contact under the FXM folder.
Leave the points value to 0 and make sure to uncheck the Show in XFile and Show In Events checkboxes under Experience Profile Options section.
Now let's overwrite the page event processor to add our custom code to identify and save the contact information. The page event is processed by tracking.triggerpageevent pipeline. We will implement a custom processor that will override the TriggerEventsProcessor class as this is where most of the actual processing is done.
We will create a custom TriggerEventsProcessor class that inherits from the out of the box class and override the TriggerPageEvent method. Inside the method body will call the base class's TriggerPageEvent method to make sure the default functionality is not affected. We will then check if the current page event name matches the custom page event name and do our additional work of identifying and saving the contact. Assuming we get UserId, FirstName, LastName, JobTitle and Email from the FXM API call, the below example identifies the contact using UserId and saves the contact information into PersonalInfo facet.