1

In my website I have "contact us form" and 1 goal is trigger after custom-save-action executes.

When a contact fill the form I am saving its details into XDB by using email-id as identity, if record already exists by that email then I am updating his record (facets).I can see newly added or updated contact in Experience Profile . Ref:- https://doc.sitecore.com/developers/93/sitecore-experience-platform/en/add-contacts.html

Now suppose , I came first time to site and visit the site and fill the form by name as "a" and email as "[email protected]" then in experience profile user should be display with triggered goal but User is created In XP but it not displaying goals, now suppose we refill the form with old session and name as "b" and email "[email protected]" then "[email protected]" session should be end and "[email protected]" session should start and after submitting the form triggered goal must be assign to "b".

The problem is : Goal is not attaching with newly added or updated contact .. It attached at the time we refill the contact-form with old session.

means if "b" fill the form then its goal assigning to "a".

Code :-

   using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
        {


            //XDBGenerateJsonSchema.JSONSchema(); //Uncomment this line if new schema needs to generate

            string phoneAsChannel = contactus.telephone;
            Contact getContact = client.Get<Contact>(new IdentifiedContactReference(phoneAsChannel, contactus.email), new ContactExpandOptions(CustomFacetInformation.DefaultFacetKey));
            if (getContact != null)
            {
                //update contact
                var facet = getContact.GetFacet<CustomFacetInformation>(CustomFacetInformation.DefaultFacetKey);
                if (facet != null)
                {
                    facet.Company = contactus.companyname;
                    facet.Market = contactus.market;
                    facet.Interest = contactus.intrestedinText;
                    facet.LeadSource = contactus.LeadSource;
                    facet.SignUpForNewsLetter = contactus.SignUpForNewsLetter;

                    facet.SourceURL = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
                    facet.Field1 = string.Empty;
                    facet.Field2 = string.Empty;
                    facet.Field3 = string.Empty;
                    client.SetFacet(getContact, CustomFacetInformation.DefaultFacetKey, facet);
                }
                else
                {
                    CustomFacetInformation customFacetInformation = new CustomFacetInformation()
                    {
                        Company = contactus.companyname,
                        Market = contactus.market,
                        Interest = contactus.intrestedinText,
                        LeadSource = contactus.LeadSource,
                        SignUpForNewsLetter = contactus.SignUpForNewsLetter,

                        SourceURL = System.Web.HttpContext.Current.Request.Url.AbsolutePath,
                        Field1 = string.Empty,
                        Field2 = string.Empty,
                        Field3 = string.Empty
                    };
                    client.SetFacet(getContact, CustomFacetInformation.DefaultFacetKey, customFacetInformation);
                }



            }
            else
            {
                Contact contact = new Sitecore.XConnect.Contact(new ContactIdentifier(phoneAsChannel, contactus.email, ContactIdentifierType.Known));
                client.AddContact(contact);
                PersonalInformation personalInfoFacet = new PersonalInformation()
                {
                    FirstName = contactus.customername
                };

                FacetReference reference = new FacetReference(contact, PersonalInformation.DefaultFacetKey);
                client.SetFacet(reference, personalInfoFacet);

                EmailAddressList emails = new EmailAddressList(new EmailAddress(contactus.email, true), "Email");
                client.SetFacet(contact, emails);

                PhoneNumberList phones = new PhoneNumberList(new PhoneNumber("", contactus.telephone), "Phone");
                client.SetFacet(contact, phones);


                //Custom Facet
                CustomFacetInformation customFacetInformation = new CustomFacetInformation()
                {
                    Company = contactus.companyname,
                    Market = contactus.market,
                    Interest = contactus.intrestedinText,
                    LeadSource = contactus.LeadSource,
                    SignUpForNewsLetter = contactus.SignUpForNewsLetter,

                    SourceURL = System.Web.HttpContext.Current.Request.Url.AbsolutePath,
                    Field1 = string.Empty,
                    Field2 = string.Empty,
                    Field3 = string.Empty
                };

                client.SetFacet(contact, CustomFacetInformation.DefaultFacetKey, customFacetInformation);

            }


            client.Submit();
        }
2
  • Are you doing this work in Sitecore? Or calling xConnect remotely?
    – Chris Auer
    Jan 16, 2020 at 19:41
  • Sitecore, in my development environment
    – Shailesh
    Jan 17, 2020 at 5:12

3 Answers 3

3

from your code getContact != null, if this condition is false, then you are creating new contact which is incorrect.

Contact contact = new Sitecore.XConnect.Contact(new ContactIdentifier(phoneAsChannel, contactus.email, ContactIdentifierType.Known));

Sitecore has already created an anonymous contact and set it in the device cookie SC_ANALYTICS_GLOBAL_COOKIE. Instead of creating new contact, you should call Sitecore.Analytics.Tracker.Current.Session.IdentifyAs method to let sitecore know that this isn't anymore an anonymous contact.

To end the session, in order to capture new contact information, you should call Session Abondon method at the end of your custom save action. This will log out current sitecore session (in your case visitor "A")

It may take some time to reflect the Goal in experience profile. Try submitting contact-us form in incognito window and check experience profile.

2
  • Thanks for the suggestion , it works as you suggested
    – Shailesh
    Jan 17, 2020 at 9:26
  • 1
    I have marked your ans as Accepted
    – Shailesh
    Jan 17, 2020 at 9:31
0

Could you please try below code ?

 var manager = Factory.CreateObject("tracking/contactManager", true) as ContactManager;
if(manager!=null)
{
///your code
     client.Submit();

manager.RemoveFromSession(CurrentTracker.Contact.ContactId);
 CurrentTracker.Session.Contact = manager.LoadContact(CurrentTracker.Contact.ContactId);
}

0
0

It works after adding Abandon and IdentifyAs in else condition and remove client.AddContact()

 public class XDBHelper
{
    public void CreateContact(Contactus contactus)
    {
        // dont delete this   
        //XDBGenerateJsonSchema.JSONSchema(); //Uncomment this line if new schema needs to generate
        try
        {

            using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                bool matchCurrentContact = ValidateCurrentUser(contactus);

                string phoneAsChannel = contactus.telephone;
                Contact getContact = client.Get<Contact>(new IdentifiedContactReference(phoneAsChannel, contactus.email), new ContactExpandOptions(CustomFacetInformation.DefaultFacetKey));


                if (getContact != null)
                {
                    if (!matchCurrentContact)
                    {
                        System.Web.HttpContext.Current.Session.Abandon();
                        Sitecore.Analytics.Tracker.Current.Session.IdentifyAs(contactus.telephone, contactus.email);

                    }

                    //update contact
                    var facet = getContact.GetFacet<CustomFacetInformation>(CustomFacetInformation.DefaultFacetKey);
                    if (facet != null)
                    {
                        facet.Company = contactus.companyname;
                        facet.Market = contactus.market;
                        facet.Interest = contactus.intrestedinText;
                        facet.LeadSource = contactus.LeadSource;
                        facet.SignUpForNewsLetter = contactus.SignUpForNewsLetter;

                        facet.SourceURL = GetCurrentPageURL();
                        facet.Field1 = string.Empty;
                        facet.Field2 = string.Empty;
                        facet.Field3 = string.Empty;
                        client.SetFacet(getContact, CustomFacetInformation.DefaultFacetKey, facet);
                    }
                    else
                    {
                        CustomFacetInformation customFacetInformation = new CustomFacetInformation()
                        {
                            Company = contactus.companyname,
                            Market = contactus.market,
                            Interest = contactus.intrestedinText,
                            LeadSource = contactus.LeadSource,
                            SignUpForNewsLetter = contactus.SignUpForNewsLetter,

                            SourceURL = GetCurrentPageURL(),
                            Field1 = string.Empty,
                            Field2 = string.Empty,
                            Field3 = string.Empty
                        };
                        client.SetFacet(getContact, CustomFacetInformation.DefaultFacetKey, customFacetInformation);
                    }


                    client.Submit();


                }
                else
                {
                    System.Web.HttpContext.Current.Session.Abandon();
                    Sitecore.Analytics.Tracker.Current.Session.IdentifyAs(contactus.telephone, contactus.email);

                    var contact = client.Get<Contact>(new IdentifiedContactReference(phoneAsChannel, contactus.email), new Sitecore.XConnect.ContactExpandOptions());

                    PersonalInformation personalInfoFacet = new PersonalInformation()
                    {
                        FirstName = contactus.customername
                    };

                    FacetReference reference = new FacetReference(contact, PersonalInformation.DefaultFacetKey);
                    client.SetFacet(reference, personalInfoFacet);

                    EmailAddressList emails = new EmailAddressList(new EmailAddress(contactus.email, true), "Email");
                    client.SetFacet(contact, emails);

                    PhoneNumberList phones = new PhoneNumberList(new PhoneNumber("", contactus.telephone), "Phone");
                    client.SetFacet(contact, phones);


                    //Custom Facet
                    CustomFacetInformation customFacetInformation = new CustomFacetInformation()
                    {
                        Company = contactus.companyname,
                        Market = contactus.market,
                        Interest = contactus.intrestedinText,
                        LeadSource = contactus.LeadSource,
                        SignUpForNewsLetter = contactus.SignUpForNewsLetter,

                        SourceURL = GetCurrentPageURL(),
                        Field1 = string.Empty,
                        Field2 = string.Empty,
                        Field3 = string.Empty
                    };

                    client.SetFacet(contact, CustomFacetInformation.DefaultFacetKey, customFacetInformation);
                    client.Submit();
                }


                //
            }

        }
        catch (Exception ex)
        {
            new Logger().LogError("Error in XDBHelper CreateContact :- " + ex.Message);

        }
    }

    private string GetCurrentPageURL()
    {
        if (System.Web.HttpContext.Current.Request.Cookies["currentpageurl"] != null)
        {
            return System.Web.HttpContext.Current.Request.Cookies["currentpageurl"].Value.Replace('*', '/');
        }

        return "";
    }
    private bool ValidateCurrentUser(Contactus contactus)
    {
        if (Tracker.Current.Contact != null)
        {
            var xConnectFacet = Tracker.Current.Contact.GetFacet<Sitecore.Analytics.XConnect.Facets.IXConnectFacets>("XConnectFacets");
            var allFacets = xConnectFacet.Facets;

            if (allFacets != null && allFacets.ContainsKey("Emails") && allFacets.ContainsKey("PhoneNumbers"))
            {
                EmailAddressList EmailAddressList = allFacets["Emails"] as EmailAddressList;
                PhoneNumberList PhoneNumberList = allFacets["PhoneNumbers"] as PhoneNumberList;

                if (EmailAddressList != null && PhoneNumberList != null)
                {
                    var currentEmail = EmailAddressList.PreferredEmail.SmtpAddress;
                    var currentPhone = PhoneNumberList.PreferredPhoneNumber.Number;

                    if (currentEmail.ToLower().Equals(contactus.email.ToLower()) && currentPhone.ToLower().Equals(contactus.telephone.ToLower()))
                    {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.