Skip to main content

Creating a session ID (e.g. in Google Tag Manager) for the Klar Pixel

Klar Pixel requires a unique cart- and checkoutId. Follow this instruction if your shop system doesn't provide one.

Frank Birzle avatar
Written by Frank Birzle
Updated this week

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:

Did this answer your question?