Data Persistence in the ITP world

Update: since Apple announced ITP 2.3 the below solution is not functional any more.

With the introduction of ITP 2.1 recently (Webkit blog), it becomes even more complicated to maintain consistency in Web Analytics and make sure reporting accuracy is maintained. Furthermore sometimes it is not just Web Analytics that we care about; Digital Marketing and other forms of data lie within our realm.

A lot of the times we want to store in cookies information that is not related to identifying customers i.e. actions the customers did and reference them later, capture sequence of actions and process them at different point in the journey (the list goes on). With the introduction of ITP 2.1, this is a good reminder that cookies are not meant to store information that the server does not care about:

  • Previous page name
  • Name of links/buttons
  • Page load timings
  • Marketing channels tracking codes & related combinations
  • Rolling e-commerce basket information

However, we often tend to store this information in cookies because “it is easy”. Yes it is, but also comes with a cost. We tend to overload the HTTP requests with useless data which and – if you really want to be nit-picky – consume resources and customers pay more for on their PAYG phone plans.

At least let’s try

I was experimenting with an idea similar to the below. Store in localStorage or sessionStorage data while also having the option to sync between cookies and storage critical data (i.e. Adobe Marketing Cloud ID). Have drafted the below small piece of code.

The above defines a handy object with convenient methods to save and retrieve values from your storage method of choice. The key things to know are:

  • storeValue & getAndSyncValue are the key methods (example below).
  • Switch between localStorage and sessionStorage options
  • When syncing is used, a value will be set in both storage and cookie.

Now it is up to you to decide what is critical and must persist in cookies versus what can be saved in storage and make internet a bit nicer place!

How to use it

When a value is saved (i.e. user_id = 123), then the value ends up both in cookies and also the storage namespace used as part of the package.

Cookies
Local Storage

If the 3rd parameter was set to false (i.e. “storageManagement.storeValue(‘user_id’, 123, true)” ) then the user_id would be stored only in localStorage.

Hopefully the above will give you an indication of how easy it is to use an alternative storage method, make your work more elegant and better utilise Web APIs.

Check the following Stack Overflow post for more details about the API differences. Differences between Cookies and localStorage/sessionStorage