Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label wffm. Show all posts
Showing posts with label wffm. Show all posts

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.

Sunday, September 3, 2017

Another way to render WFFM Form



WFFM is a nice module to capture details from visitors. But when developers try to render WFFM forms in web page, then she has to face CSS and JS conflict with webpage. However this is good for testers and QA team as they will able to find lots of cosmetic issues ;)

Most of the time, WFFM’s CSS and JS stop or change the UI and functionality of the web page.
However there is an alternative way to render WFFM form – Iframe.

Follow below steps use it
* Create WFFM form
* Create item having separate page layout and MVC rendering for WFFM form only.
* Create one parameter rendering IFrame with three field - IFrame Url, Width and Height as shown below

WFFM form via IFRAME
WFFM form via IFRAME


Specify relative path of above item page rendering path in IFrame URL field

Specify below line in Iframe.cshtml as 

<iframe src="@Model.Url" width="@Model.Width" height="@Model.Height" scrolling="auto" frameborder="0" align="left" name=""></iframe>


When you render web page, your WFFM form will be rendered in IFRAME and all WFFM form CSS and JS are also load into IFRAME. In this way we separated CSS and JS of web page with WFFM form CSS and JS. So they will never conflict.

However there are two issues that I encountered with this approach

* If your website is running on HTTPS and you have entered the WFFM item path as HTTP then you will face issue. To fix it, instead of using full URL, you must use relative path.

* How to handle multi lingual WFFM forms? Let’s suppose your page is in French and now if you try to render your WFFM form with relative path then it try to render by default in English language. To fix it specify your relative path with language query string as "/wffm-forms/contact-form?sc_lang=fr-fr"

That’s it. 

I hope you will try this alternative approach. Stay tuned for more Sitecore related articles.

Till that happy Sitecoring :)

Please leave your comments or share this trick if it’s useful for you.