0

I have a form at my page and postback action to controller rendering, sitecore 8.2 I wounder why Sitecore.Analytics.Tracker.Current.Contact is null at postback ? at all other parts it works correctly including WebApi,

[HttpPost]
public ActionResult CPostback(CPostbackModel postbackData)
{
    Sitecore.Analytics.Tracker.Current.Contact - null 
}

how to fix it ?

@using(Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post, new { @class="comp-entry-form comp-entry-full-width" }))
{
    @Html.Sitecore().FormHandler("Comp","CPostback")
}
  • Do you have @Html.Sitecore().VisitorIdentification() in your main layout? – Chris Auer Aug 4 '17 at 18:07
  • There is a good guide I am trying to find you for what could be wrong. But here is a start. sitecorehacker.com/2017/02/12/… – Chris Auer Aug 4 '17 at 18:16
  • Here you go. See Pete's answer. And the link above. sitecore.stackexchange.com/questions/3666/… – Chris Auer Aug 4 '17 at 18:18
  • @ChrisAuer yes I have... tracking not working at postback action only – Arbejdsglæde Aug 5 '17 at 19:21
  • @ChrisAuer tracking is not working only at this postback action (on local environment) , all other cases works fine. it is only regarding post back – Arbejdsglæde Aug 7 '17 at 9:27
0

Unfortunately I did it but in manyal way,

    var contactCookie = new Sitecore.Analytics.Web.ContactKeyCookie();
  contact = GetXdbContact(contactCookie.ContactId);
 public static Contact GetXdbContact(Guid identifier)
        {
            if (Tracker.Current != null && Tracker.Current.Contact != null)
            {
                //This is the current user
                return Tracker.Current.Contact;
            }

            //There is no Tracker.Current.Contact, so this must be running in some non-user context
            var repository = Factory.CreateObject("tracking/contactRepository", true) as ContactRepositoryBase;
            var sessionManager = Factory.CreateObject("tracking/sharedSessionState/manager", true) as SharedSessionStateManager;
            if (repository != null)
            {
                var dbContact = repository.LoadContactReadOnly(identifier);
                if (dbContact != null)
                {
                    //Now we have the Contact Id.  Use that Id to actually get the contact from shared session
                    var contactId = dbContact.ContactId;
                    if (sessionManager != null)
                    {
                        var sessionContact = sessionManager.LoadContactReadonly(contactId);
                        if (sessionContact != null)
                        {
                            return sessionContact;
                        }
                    }
                    return dbContact;
                }
            }

            return null;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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