Accessing the Quantum Metric session ID for integrations
Kai Omonijo
What to do if you want to send the user/session id value to other analytics systems such as your a/b testing platform or Adobe Analytics, Google Analytics, Mixpanel?
Quantum Metric does have the session id in a cookie. However you can run into timing issues; where the cookie is not yet set and therefore you aren’t sure when to grab the value.
Where possible I like to rely on the vendor itself to provide id values instead of polling directly for cookies and sessionstorage. I often find this is more future proof.
For Quantum Metric my approach is to attach a listener directly to the API. Handling the likely scenario where the API may not yet exist due to asynchronous loading of the library.
Quantum Metric will trigger your callback function if you attach an event listener to their “start” event.
const waitForQuantumMetric = () => {
return new Promise(testQuantumMetricExist);
function testQuantumMetricExist(resolve, reject) {
if (typeof window.QuantumMetricAPI !== "undefined") {
resolve(true);
} else if (document.hidden) {
window.setTimeout(() => testQuantumMetricExist(resolve, reject), 66);
} else {
window.requestAnimationFrame(() =>
testQuantumMetricExist(resolve, reject)
);
}
}
};
waitForQuantumMetric().then(() => {
window.QuantumMetricAPI.addEventListener("start", function (event) {
const { sessionID, userID, hitID } = event;
console.log(">> event sessionID", sessionID);
console.log(">> event userID", userID);
console.log(">> event hitID", hitID);
});
});
I recommend saving this type of approach for when you are certain the library you are waiting for is going to load.