Sometimes our customers are facing the situation that they don't have a sessionID in their Google Tag Manager data layer or in the frontend templates where they want to implement the Klar Pixel.
Here's an easy solution to this problem:
We're generating a unique ID that we store in session storage which can be used as cart- or checkoutID.
function() {
try {
// 1. Define the key name for storage
var storageKey = 'my_gtm_session_id';
// 2. Check if the value exists in sessionStorage
var storedId = cal ca sessionStorage.getItem(storageKey);
// 3. If it exists, return the existing value
if (storedId) { return storedId; }
// 4. If not, generate the new ID
var newId = new Date().getTime() + '.' + Math.random().toString(36).substring(5);
// 5. Save it to sessionStorage
sessionStorage.setItem(storageKey, newId);
// 6. Return the new value
return newId;
} catch (e) {
// Fallback: If sessionStorage is disabled or full, generate a temporary ID
return new Date().getTime() + '.' + Math.random().toString(36).substring(5);
}
}
In GTM you can place this code in a Custom JavaScript Variable and use it in all Klar Events:

