Surendra Sharma

Surendra Sharma

Search This Blog

Friday, October 6, 2017

Converting WFFM captured details to Sitecore experience contact profile



Some people see data as facts and figures. But it’s more than that, it’s the lifeblood and it contains history. And it’s trying to tell you something. 

So it’s very important for any organization if they are able to capture visitors’ details via some form and convert these visitors’ details into Sitecore contacts.

For this, create WFFM form with basic fields like first name, last name, email id etc.

Apply this WFFM form to any item which can render it on browser as

WFFM Form
WFFM Form

On submit, we can fire custom action on WFFM form. This action saves filled details into Sitecore as contact. Additionally we can also create a cookie with same details for smart tracking of visitors’ activities on any page.

Here is a ready-made code for WFFM custom action

public class CreateMongoContacts : ISaveAction
{
    public ID ActionID { get; set; }
    public string UniqueKey { get; set; }
    public ActionType ActionType { get; private set; }
    public ActionState QueryState(ActionQueryContext queryContext)
    {
        return ActionState.Enabled;
    }

    public void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
    {
        try
        {
            string firstName = adaptedFields.GetValueByFieldID("{E46B75C1-BF08-4A87-9BE7-776BD65F25C4}");
            string lastName = adaptedFields.GetValueByFieldID("{966EDDC4-B907-4DDA-82C7-F3A8853514AF}");
            string emailAddress = adaptedFields.GetValueByFieldID("{709521AA-D63B-430F-8928-DF35FD7238EE}");

            MongoContacts(firstName, lastName, emailAddress);
        }
        catch (Exception ex)
        {
            Log.Error("Execute Error", ex, this);
        }
    }


    private void MongoContacts(string FirstName, string LastName, string EmailAddress)
    {
        try
        {
            string visitorEmail = EmailAddress;
            string visitorName = FirstName;
            Contact contact = Tracker.Current.Contact;
            string contactId = contact.ContactId.ToString();
            //Email Address
            if (Sitecore.Analytics.Tracker.Current.Contact != null)
            {
                var emailFacet = Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");

                if (!emailFacet.Entries.Contains("Work Email"))
                {
                    IEmailAddress email = emailFacet.Entries.Create("Work Email");
                    email.SmtpAddress = visitorEmail;
                    emailFacet.Preferred = visitorEmail;
                }
                else
                {
                    IElementDictionary<IEmailAddress> entries = emailFacet.Entries;
                    var emailAddress = entries["Work Email"];
                    emailAddress.SmtpAddress = visitorEmail;
                }
            }
            //Personal Information
            if (Sitecore.Analytics.Tracker.Current.Contact != null || visitorName != null || visitorName != "")
            {
                var personalFacet = Tracker.Current.Contact.GetFacet<IContactPersonalInfo>("Personal");
                personalFacet.FirstName = FirstName;
            }

            if (Sitecore.Analytics.Tracker.Current.Contact != null || visitorName != null || visitorName != "")
            {
                var personalFacet = Tracker.Current.Contact.GetFacet<IContactPersonalInfo>("Personal");
                personalFacet.Surname = LastName;
            }

            //Identify a unKnown Visitor
            var identifiers = Sitecore.Analytics.Tracker.Current.Contact.Identifiers;
            if (identifiers.IdentificationLevel == ContactIdentificationLevel.Anonymous)
            {
                Sitecore.Analytics.Tracker.Current.Session.Identify(visitorEmail);
            }
            ContactManager contactManager = (ContactManager)Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true);
            contactManager.SaveAndReleaseContactToXdb(contact);

            HttpCookie formCookies = new HttpCookie("contactprofile");
            formCookies.Values["ContactId"] = contactId;
            formCookies.Values["EmailId"] = EmailAddress;
            formCookies.Values["FirstName"] = FirstName;
            formCookies.Values["LastName"] = LastName;
            formCookies.Expires = DateTime.Now.AddYears(1);
            HttpContext.Current.Response.Cookies.Remove("contactprofile");
            HttpContext.Current.Response.SetCookie(formCookies);
        }
        catch (Exception ex)
        {
            Log.Error("MongoContacts Error", ex, this);
        }
    }
}


To get this analytics data, login to Sitecore and click on “Experience Profile” icon on dashboard

Experience Profile
Experience Profile


Find contact by entering email id or first name or last name in search box. Click on selected contact.

Note:- You will get contacts only when current session is expired. To expire session forcefully, you can either delete cookies or close browser or restart IIS. Wait for some time or have a coffee break :) .

Contacts in Experience Profile
Contacts in Experience Profile
 
This should open visitor’s profile where voilaaa one can get visitor interaction history data in terms of first name, last name and email id.

Contact in Sitecore
Contact in Sitecore

In conclusion, WFFM form only store user entered data as a single record. But Sitecore experience profile collect all user activities on website. Hence it’s good place to convert any anonymous profile with captured user information for easy identification and tracking.

I hope you like this Sitecore article. Stay tuned for more Sitecore related details.

Till that happy Sitecoring :)

Please leave your comments or share this article if it’s useful for you. Waiting for your feedback.

Wednesday, October 4, 2017

Presentation Rendering Reporter: New module on Sitecore Marketplace

I was checking my daily mail and received mail from Sitecore Marketplace Team that my new module is approved and published at Marketplace.

This is my third module on Sitecore marketplace where I spend almost 14 hours on this new module which I named as “Presentation Rendering Reporter”.

This module is used for analysis, comparing and reporting purpose for getting item’s presentation rendering details.

It allows you to enter GUID of an item and give option of displaying its children rendering details. You can limit the number of children to be displayed on screen as

UI Option in Presentation Rendering Reporter
UI Option in Presentation Rendering Reporter


It will display items rendering details in two column
1.    Shared Layout details

2.    Item’s final layout presentation rendering details in all languages in all versions in single cell. This is a very smart way to present details in combination of horizontal and vertical tabs. Just select any language tab, a related version tab and you will get its rendering details which include

* Rendering Name
* Placeholder
* Datasource path
* Item Path at bottom

Single view of rendering details is the unique feature of this module.


Shared and Final Rendering details
Shared and Final Rendering details

You can download this module from Sitecore Marketplace at https://marketplace.sitecore.net/Modules/P/Presentation_Rendering_Reporter.aspx

Installed this Sitecore module and access it as http://<sitecoreinstancename>/PresentationRenderingReporter.aspx

I have uploaded its code at Github, which you are free to modify at, https://github.com/alexksharma/Presentation-Rendering-Reporter

I hope you like this Sitecore module. Stay tuned for more Sitecore related details.

Till that happy Sitecoring :)
 
Please leave your comments or share this module if it’s useful for you. Waiting for your feedback.