From 11e890ecff15b6206e197c32f3ab611b338ddfec Mon Sep 17 00:00:00 2001 From: Sankeerth Date: Thu, 26 Dec 2024 13:05:39 +0530 Subject: [PATCH] fix: microsoft clarity load and handle promise for identify (#1964) * fix: microsoft clarity load and handle promise for identify * fix: loading logic * fix: send set calls to clarity while identify is called before load * fix: error handling * chore: address review comments --------- Co-authored-by: Sai Sankeerth --- .../src/integrations/MicrosoftClarity/browser.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js index 68dced8b85..9f765e1d96 100644 --- a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js +++ b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js @@ -33,7 +33,8 @@ class MicrosoftClarity { } isLoaded() { - return !!window.clarity; + // queue would be undefined if the Clarity script is loaded + return !!window.clarity && !window.clarity.q; } isReady() { @@ -55,7 +56,13 @@ class MicrosoftClarity { if (context?.traits?.customPageId) { customPageId = context.traits.customPageId; } - window.clarity('identify', userId, sessionId, customPageId); + + const identifyPromise = window.clarity('identify', userId, sessionId, customPageId); + if (typeof identifyPromise?.then === 'function') { // Clarity SDK is ready + identifyPromise.catch(error => { + logger.error('The "identify" promise was rejected', error); + }); + } if (context?.traits) { const { traits } = context; const keys = Object.keys(traits);