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.
var storageManagement = {
NAMESPACE: 'webAnalytics',
LOCAL_STORAGE: 'localStorage',
SESSION_STORAGE: 'sessionStorage',
storeValue: function setValue(name, value, useCookie) {
this._setStorage(this.LOCAL_STORAGE, name, value);
if (useCookie)
this._setCookie(name, value, 365 * 2);
},
getAndSyncValue: function getValue(name, useCookie) {
var value = this._getCookie(name) || this._getStorage(this.LOCAL_STORAGE, name);
if (typeof (value) !== 'undefined' && useCookie)
this.syncValue(name, value);
return value;
},
_getCookie: function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
},
_setCookie: function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
// Remove the subdomain. Works on domains with both 2 or 3 parts i.e. co.uk and com.
domainName = document.location.hostname.split('.').splice(1).join('.');
document.cookie = name + "=" + (value || "") + expires + "; path=/;domain=" + domainName;
},
_isStorageAvailable: function _isStorageAvailable() {
var isAvailable = false;
try {
if (typeof (window.localStorage) !== 'undefined' && typeof (window.sessionStorage) !== 'undefined')
isAvailable = true;
} catch (error) { }
return isAvailable;
},
_setStorage: function setStorage(storageType, name, value) {
if (this._isStorageAvailable()) {
var jsonData = this._getNameSpaceData(storageType);
var item = { name: value };
jsonData[name] = value;
window[storageType].setItem(this.NAMESPACE, JSON.stringify(jsonData));
}
},
_getStorage: function getStorage(storageType, name) {
if (this._isStorageAvailable()) {
var jsonData = this._getNameSpaceData(storageType);
return jsonData[name];
}
},
_deleteStorage: function deleteStorage(storageType, name) {
if (this._isStorageAvailable()) {
var jsonData = this._getNameSpaceData(storageType);
delete jsonData[name];
window[storageType].setItem(this.NAMESPACE, JSON.stringify(jsonData));
}
},
_getNameSpaceData: function getNameSpaceData(storageType) {
var nameSpacedData = window[storageType].getItem(this.NAMESPACE);
return JSON.parse(nameSpacedData) || {};
}
};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
/*
Core methods:
- getAndSyncValue( name, cookieSyncFlag )
-- name: the name of the parameter to retrieve. Cookies take precendence.
-- cookieSyncFlag: boolean. If true will update both storage and cookies.
*/
// Get the Google Analytics ID value from the _ga cookie
var google_analytics_id = storageManagement.getAndSyncValue('_ga',false);
// Store the user_id with value 123.
// The value will be stored in both storage and cookie API.
storageManagement.storeValue('user_id', 123, true);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.
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.
Do you have challenges in your analytics processes?
Do you need advice or just want to discuss something?
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