From 00649a588f2e52c3e17b156b1b6455b67d6f3410 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Mon, 9 Dec 2024 15:15:26 +0530 Subject: [PATCH 1/5] fix: microsoft clarity load and handle promise for identify --- .../integrations/MicrosoftClarity/browser.js | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 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..b1196853e9 100644 --- a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js +++ b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js @@ -33,7 +33,7 @@ class MicrosoftClarity { } isLoaded() { - return !!window.clarity; + return !!window.clarity && window.clarity('identify') instanceof Promise; } isReady() { @@ -55,14 +55,17 @@ class MicrosoftClarity { if (context?.traits?.customPageId) { customPageId = context.traits.customPageId; } - window.clarity('identify', userId, sessionId, customPageId); - if (context?.traits) { - const { traits } = context; - const keys = Object.keys(traits); - keys.forEach(key => { - window.clarity('set', key, traits[key]); - }); - } + window.clarity('identify', userId, sessionId, customPageId).then(() => { + if (context?.traits) { + const { traits } = context; + const keys = Object.keys(traits); + keys.forEach(key => { + window.clarity('set', key, traits[key]); + }); + } + }).catch(error => { + logger.error('[MicrosoftClarity] Error in identify call', error); + }); } } From d122011b824d6c8bccb5c1592e5f681bd2d9b747 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Tue, 10 Dec 2024 10:39:34 +0530 Subject: [PATCH 2/5] fix: loading logic --- .../src/integrations/MicrosoftClarity/browser.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js index b1196853e9..309896675c 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 && window.clarity('identify') instanceof Promise; + // queue would be undefined if the Clarity script is loaded + return !!window.clarity && !window.clarity.q; } isReady() { From d8208470988c6771fb955951bf34471db96561d5 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 12 Dec 2024 18:11:43 +0530 Subject: [PATCH 3/5] fix: send set calls to clarity while identify is called before load --- .../integrations/MicrosoftClarity/browser.js | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js index 309896675c..ce46102b93 100644 --- a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js +++ b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js @@ -56,17 +56,18 @@ class MicrosoftClarity { if (context?.traits?.customPageId) { customPageId = context.traits.customPageId; } - window.clarity('identify', userId, sessionId, customPageId).then(() => { - if (context?.traits) { - const { traits } = context; - const keys = Object.keys(traits); - keys.forEach(key => { - window.clarity('set', key, traits[key]); - }); - } - }).catch(error => { - logger.error('[MicrosoftClarity] Error in identify call', error); - }); + try { + window.clarity('identify', userId, sessionId, customPageId); + } catch (error) { + logger.error('[MicrosoftClarity] Error in identify call: ', error); + } + if (context?.traits) { + const { traits } = context; + const keys = Object.keys(traits); + keys.forEach(key => { + window.clarity('set', key, traits[key]); + }); + } } } From c355ca10bec730932bf5e232a402addfbfe58285 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 12 Dec 2024 20:45:56 +0530 Subject: [PATCH 4/5] fix: error handling --- .../src/integrations/MicrosoftClarity/browser.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js index ce46102b93..d98b2f29ba 100644 --- a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js +++ b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js @@ -56,10 +56,10 @@ class MicrosoftClarity { if (context?.traits?.customPageId) { customPageId = context.traits.customPageId; } - try { - window.clarity('identify', userId, sessionId, customPageId); - } catch (error) { - logger.error('[MicrosoftClarity] Error in identify call: ', error); + + const identifyPromise = window.clarity('identify', userId, sessionId, customPageId); + if (!!identifyPromise) { // Clarity SDK is ready + identifyPromise.catch(logger.error); } if (context?.traits) { const { traits } = context; From dc5553a71fa416f1814d46fc880a1ff9f11f54a7 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 26 Dec 2024 11:05:47 +0530 Subject: [PATCH 5/5] chore: address review comments --- .../src/integrations/MicrosoftClarity/browser.js | 6 ++++-- 1 file changed, 4 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 d98b2f29ba..9f765e1d96 100644 --- a/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js +++ b/packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js @@ -58,8 +58,10 @@ class MicrosoftClarity { } const identifyPromise = window.clarity('identify', userId, sessionId, customPageId); - if (!!identifyPromise) { // Clarity SDK is ready - identifyPromise.catch(logger.error); + 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;