0

I'm using Sitecore.Analytics.Tracker for personalizations on my site. I have added the ability for the user to manually change their zipcode, which changes what they see on certain parts of the site. The zipcode is retrieved with Tracker.CurrentVisit.PostalCode

public string GetCurrentZipCode()
{
    return Tracker.CurrentVisit.PostalCode.IsNotNullOrEmpty() ? Tracker.CurrentVisit.PostalCode : String.Empty;
}

public void SetCurrentZipCode(string zip)
{
    Tracker.CurrentVisit.PostalCode = zip;

}

This works but is finicky; every now and then when I reload the page, the zip code will have changed back to the default, which I discovered is because sometimes when the page loads PostalCode has changed back to an empty string.

Why is Tracker.CurrentVisit.PostalCode getting reset? Is this a setting in Sitecore? How can I stop it? Ideally the value of PostalCode should persist for the entire session.

Erica Stockwell-Alpert
  • 4,624
  • 10
  • 63
  • 130

1 Answers1

0

The contact has an Addresses collection that can be used to store this information.

Sitecore.Analytics.Model.config:

<facets>
  <facet name="Addresses" contract="Sitecore.Analytics.Model.Entities.IContactAddresses, Sitecore.Analytics.Model" />
</facets>

See IContactAddresses and IAddress for details on those interfaces. IContactAddresses has an Entries collection which is a list of IAddress objects.

Access the address info for the current contact:

private IContactAddresses _addressInfo;

protected IContactAddresses  AddressInfo
{
    get
    {
        return _addressInfo ?? (_addressInfo = Tracker.Current.Contact.GetFacet<IContactAddresses>("Addresses"));
    }
}

You can get this object representing the current contact address information and update the various properties to save that data in memory until the user session ends. At that time the data will be written back to xDB so you can get it back at a later time.

Nathan Hase
  • 649
  • 4
  • 11
  • Also I cannot use your method because Tracker does not have `Current` – Erica Stockwell-Alpert May 05 '16 at 17:16
  • Are you using Sitecore.Analytics; ? It's actually Sitecore.Analytics.Tracker.Current. If your tracker is not available then make sure that Analytics is enabled: – Nathan Hase May 05 '16 at 18:11
  • [Obsolete("Use Tracker.Current instead")] public static CurrentVisitContext CurrentVisit – Nathan Hase May 05 '16 at 18:14
  • To clarify: Tracker.CurrentVisit is obsolete. If you're Tracker.Current is not available then there may be a bug present somewhere, depending on what other add ons and configuration changes you have in place. – Nathan Hase May 06 '16 at 18:58